Home > database >  Modify excel file with c# without deleting elements
Modify excel file with c# without deleting elements

Time:10-25

I am using the Syncfusion.XlsIO.WinForms dependency to develop with C# but when generating a file a sheet is generated that says "Created with a trial version of Syncfusion Essential XlsIO" so I think it is paid. I have been looking for a free NuGet package to modify an excel file however many dependencies remove objects like macros and textboxes on save with a function like SaveAs or Save (Macros and textboxes are part of the document). excel and I don't generate them by code, they belong to the file). I have been using a free option like ClosedXML but it deletes the elements, I have been using this code:

XLWorkbook workbook = XLWorkbook.OpenFromTemplate("C://template//from123.xlsx");
        var hoja = workbook.Worksheets.Worksheet(1).Worksheet;
        hoja.Cell("B11").Value = "5";
        //hoja.Cell("CN").Style.Fill.SetBackgroundColor(XLColor.Black); 
        workbook.SaveAs(@"C://template"   "//"   "XMLCopy"   ".xlsx");

The result is as follows:

Before modifying the file

After modifying the file with c# and closedXml and saving

Do you know how I can make a modification for free and without deleting my elements?

CodePudding user response:

You should open the original excel file by creating a new workbook object, modify it and then save it as a new file:

         using (XLWorkbook workbook = new XLWorkbook("C://template//from123.xlsx"))
         {
            var hoja = workbook.Worksheets.Worksheet(1);
            hoja.Cell("B11").Value = "5";
            //hoja.Cell("CN").Style.Fill.SetBackgroundColor(XLColor.Black);
            workbook.SaveAs(@"C://template"   "//"   "XMLCopy"   ".xlsx");
         }
  • Related