Modify excel cell

后端 未结 5 1052
轮回少年
轮回少年 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条回答
  •  悲&欢浪女
    2020-12-31 19:32

    The problem in both cases is that the modified workbook is not saved back to the stream:

    MemoryStream ms = new MemoryStream();
    using (FileStream fs = File.OpenRead(@"Path\Test.xlsx"))
    using (ExcelPackage excelPackage = new ExcelPackage(fs))
    {
        ExcelWorkbook excelWorkBook = excelPackage.Workbook;
        ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
        excelWorksheet.Cells[1, 1].Value = "Test";
        excelWorksheet.Cells[3, 2].Value = "Test2";
        excelWorksheet.Cells[3, 3].Value = "Test3";
    
        excelPackage.SaveAs(ms); // This is the important part.
    }
    
    ms.Position = 0;
    return new FileStreamResult(ms, "application/xlsx")
    {
        FileDownloadName = "Tester.xlsx"
    };
    

提交回复
热议问题