我有一些包含一些数据的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());
}
这就是我所做的。但我想追加..
答案 0 :(得分:4)
要追加到现有的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;
然后只需写入该行
答案 1 :(得分:1)
您必须打开现有文件,而不是创建新文件。
var xlApp = new Excel.Application();
xlApp.Workbooks.Open(path + "Chat_Competitors.xls");