【问题标题】:How to append a data to existing excel file using c#如何使用c#将数据附加到现有的excel文件
【发布时间】:2012-06-27 11:12:21
【问题描述】:

我想使用 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();

如何将数据附加到 excel 文件中?还有其他方法可以实现这一点!

【问题讨论】:

标签: c# c#-4.0 c#-3.0


【解决方案1】:

您是否尝试过使用 Epplus 库在 C# 中创建和编辑 Excel 电子表格和工作簿?几周前我已经研究过了。这是超级的。我建议使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2021-05-05
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多