Writing Data to an Existing Excel File in C#

前端 未结 3 797
生来不讨喜
生来不讨喜 2020-12-02 01:20

I want to write data to an existing Excel file, whilst preserving the original data.

The file has sheet1; I want to write on sheet2, then save. The problem is that e

3条回答
  •  青春惊慌失措
    2020-12-02 02:06

    According to Programmatically Insert to Existing Excel File using C# by R Manimaran:

    Here is the code which will do the insertion in an already exists excel file.

    private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
    private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
    private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
    private static Microsoft.Office.Interop.Excel.Application oXL;
    public static void ReadExistingExcel()
    {
       string path = @"C:\Tool\Reports1.xls";
       oXL = new Microsoft.Office.Interop.Excel.Application();
       oXL.Visible = true;
       oXL.DisplayAlerts = false;
       mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
       //Get all the sheets in the workbook
      mWorkSheets = mWorkBook.Worksheets;
       //Get the allready exists sheet
       mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
       Microsoft.Office.Interop.Excel.Range range= mWSheet1.UsedRange;
       int colCount = range.Columns.Count;
       int rowCount= range.Rows.Count;
       for (int index = 1; index < 15; index++)
       {
          mWSheet1.Cells[rowCount + index, 1] = rowCount +index;
          mWSheet1.Cells[rowCount + index, 2] = "New Item"+index;
       }
       mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
       Missing.Value, Missing.Value, Missing.Value,    Missing.Value,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
       Missing.Value, Missing.Value, Missing.Value,
       Missing.Value, Missing.Value);
       mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
       mWSheet1 = null;
       mWorkBook = null;
       oXL.Quit();
       GC.WaitForPendingFinalizers();
       GC.Collect();
       GC.WaitForPendingFinalizers();
       GC.Collect();
    } 
    

    If you need to create a new Sheet use the following code.

    oSheet = (Excel.Worksheet)oWB.Sheets.Add(Missing.Value, Missing.Value, Missing.Value,      Missing.Value);
    
    oSheet.Name = SheetName;
    

提交回复
热议问题