简体   繁体   English

如何使用C#将数据附加到现有的Excel文件

[英]How to append a data to existing excel file using c#

I want to append a data to a existing excel file using c#.I Tried it using below codes,but it is giving wrong column value after opening of the excel file(if my excel file contains 4 columns,but here it is returning column count as 1 only). 我想使用c#将数据追加到现有的excel文件中,我使用以下代码进行了尝试,但是打开excel文件后它给出了错误的列值(如果我的excel文件包含4列,但是这里返回的是列数仅1)。

xlApp = new Excel.Application();
            xlApp.Visible = true;
            xlWorkBook = xlApp.Workbooks.Open(fileName, 0, false, 5, "", "", false,
            Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);   //@"H:\TestFile.xlsx"
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets.get_Item(sheetNum);

            rng = xlWorkSheet.UsedRange;

            int colCount = rng.Columns.Count;
            int rowCount = rng.Rows.Count;
            rng = (Excel.Range)xlWorkSheet.Cells[rowCount, colCount];
            Excel.Range newColumn = rng.EntireColumn;
            xlWorkSheet.Cells[1, colCount + 3] = "Udupi";

            xlWorkBook.Save();
            xlWorkBook.Close(misValue, misValue, misValue);
            xlApp.Quit();

How can i append a data to excel file?Is there any other methods are available to achieve this one! 我如何将数据追加到excel文件中?是否有其他方法可以实现此目的!

Have you tried Epplus library for creating and editing Excel spreadsheets and workbooks in C#? 您是否尝试过Epplus库以C#创建和编辑Excel电子表格和工作簿? I have worked on it before some weeks ago. 几周前,我已经做过这项工作。 It is super. 超级好 I recommend to use it. 我建议使用它。

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

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