Writing Data to an Existing Excel File in C#

前端 未结 3 794
生来不讨喜
生来不讨喜 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:07

    This is my way to add data to existing excel file: (Its very simple and efficient)

    1 - Add Microsoft.Office.Interop.Excel component as a reference to your application You can find it in .Net FrameWork in Extensions section

    2- then add:

    using Excel = Microsoft.Office.Interop.Excel;
    using System.Reflection;
    

    3- Now I have a simple class with 3 methods (openExcel, addDataToExcel, closeExcel)

    public class ExcelFile
    {
    
        private string excelFilePath = string.Empty;
        private int rowNumber = 1; // define first row number to enter data in excel
    
        Excel.Application myExcelApplication;
        Excel.Workbook myExcelWorkbook;
        Excel.Worksheet myExcelWorkSheet;
    
        public string ExcelFilePath
        {
            get { return excelFilePath; }
            set { excelFilePath = value; }
        }
    
        public int Rownumber
        {
            get { return rowNumber; }
            set { rowNumber = value; }
        }
    
        public void openExcel()
        {
            myExcelApplication = null;
    
            myExcelApplication = new Excel.Application(); // create Excell App
            myExcelApplication.DisplayAlerts = false; // turn off alerts
    
    
            myExcelWorkbook = (Excel.Workbook)(myExcelApplication.Workbooks._Open(excelFilePath, System.Reflection.Missing.Value,
               System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
               System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
               System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
               System.Reflection.Missing.Value, System.Reflection.Missing.Value)); // open the existing excel file
    
            int numberOfWorkbooks = myExcelApplication.Workbooks.Count; // get number of workbooks (optional)
    
            myExcelWorkSheet = (Excel.Worksheet)myExcelWorkbook.Worksheets[1]; // define in which worksheet, do you want to add data
            myExcelWorkSheet.Name = "WorkSheet 1"; // define a name for the worksheet (optinal)
    
            int numberOfSheets = myExcelWorkbook.Worksheets.Count; // get number of worksheets (optional)
        }
    
        public void addDataToExcel(string firstname, string lastname, string language, string email, string company)
        {
    
            myExcelWorkSheet.Cells[rowNumber, "H"] = firstname;
            myExcelWorkSheet.Cells[rowNumber, "J"] = lastname;
            myExcelWorkSheet.Cells[rowNumber, "Q"] = language;
            myExcelWorkSheet.Cells[rowNumber, "BH"] = email;
            myExcelWorkSheet.Cells[rowNumber, "CH"] = company;
            rowNumber++;  // if you put this method inside a loop, you should increase rownumber by one or wat ever is your logic
    
        }
    
        public void closeExcel()
        {
            try
            {
                myExcelWorkbook.SaveAs(excelFilePath, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                               System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange,
                                               System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                               System.Reflection.Missing.Value, System.Reflection.Missing.Value); // Save data in excel
    
    
                myExcelWorkbook.Close(true, excelFilePath, System.Reflection.Missing.Value); // close the worksheet
    
    
            }
            finally
            {
                if (myExcelApplication != null)
                {
                    myExcelApplication.Quit(); // close the excel application
                }
            }
    
        }
    }
    

提交回复
热议问题