简体   繁体   English

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

[英]How to append data into a excel sheet column?

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.. 但我想追加..

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 然后简单地写入该行

You must open the existing file instead of create a new. 您必须打开现有文件,而不是创建新文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM