Modify excel cell

后端 未结 5 1055
轮回少年
轮回少年 2020-12-31 18:54

Good morning, I would like to edit some cells from already existing excell file. I tried use EPPlus and normal OpenXml classes. However I failed. In both situation program w

5条回答
  •  梦毁少年i
    2020-12-31 19:33

    You can use the Interop dll's from Microsoft to edit office documents http://msdn.microsoft.com/en-us/library/15s06t57.aspx. Add the "Microsoft.Office.Interop.Excel.dll" to your solution. With this code i've changed 2 cell values.

    static void Main(string[] args)
    {
            Application excel = new Application();
    
            Workbook workbook = excel.Workbooks.Open(@"C:\Users\Martijn\Documents\Test.xlsx", ReadOnly: false, Editable:true);
            Worksheet worksheet = workbook.Worksheets.Item[1] as Worksheet;
            if (worksheet == null)
                return;
    
            Range row1 = worksheet.Rows.Cells[1, 1];
            Range row2 = worksheet.Rows.Cells[2, 1];
    
            row1.Value = "Test100";
            row2.Value = "Test200";
    
    
            excel.Application.ActiveWorkbook.Save();
            excel.Application.Quit();
            excel.Quit();
        }
    

    I've started with Test1 and Test2 wich after the program running changed into the proper values.

    Situation before running code Situation after running code

提交回复
热议问题