如何将数据附加到Excel工作表列?

时间:2021-12-17 16:27:55

I have existing Excel file with some data and I want to append data to it... ![enter image description here][1]

我有一些包含一些数据的Excel文件,我想将数据附加到它...![在此输入图像描述] [1]

try
{
    xlApp = new Excel.Application();
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
    xlWorkBook.Save();
    Object newpath = path + "Chat_Competitors.xls";
    xlWorkBook.SaveAs(newpath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();
}
catch (Exception ex)
{
    MessageBox.Show("Error" + ex.ToString());
}

this is what I done. but I want to append..

这就是我所做的。但是我想追加..

2 个解决方案

#1


3  

To append to an existing excel file, find the last row and increment it by 1

要附加到现有excel文件,请查找最后一行并将其递增1

int _lastRow = xlWorkSheet.Cells.Find(
                              "*",
                              xlWorkSheet.Cells[1,1],
                              Excel.XlFindLookIn.xlFormulas,
                              Excel.XlLookAt.xlPart,
                              Excel.XlSearchOrder.xlByRows,
                              Excel.XlSearchDirection.xlPrevious,
                              misValue,
                              misValue,
                              misValue
                              ).Row + 1;

and then simply write to that row

然后简单地写入该行

#2


1  

You must open the existing file instead of create a new.

您必须打开现有文件,而不是创建新文件。

var xlApp = new Excel.Application();
xlApp.Workbooks.Open(path + "Chat_Competitors.xls");

#1


3  

To append to an existing excel file, find the last row and increment it by 1

要附加到现有excel文件,请查找最后一行并将其递增1

int _lastRow = xlWorkSheet.Cells.Find(
                              "*",
                              xlWorkSheet.Cells[1,1],
                              Excel.XlFindLookIn.xlFormulas,
                              Excel.XlLookAt.xlPart,
                              Excel.XlSearchOrder.xlByRows,
                              Excel.XlSearchDirection.xlPrevious,
                              misValue,
                              misValue,
                              misValue
                              ).Row + 1;

and then simply write to that row

然后简单地写入该行

#2


1  

You must open the existing file instead of create a new.

您必须打开现有文件,而不是创建新文件。

var xlApp = new Excel.Application();
xlApp.Workbooks.Open(path + "Chat_Competitors.xls");