How to insert data to XLSX file with some restrictions | Open-XML-SDK , C#

Viewed 7

I'm try to insert data in column B row 12 in this file. All it works, but when i try to open the new file i get this error

We found a problem with some content in 'NEW FILE.xlsx'. Do you want us to try to recover as much as we can? if you trust the source of this workbook, click yes

This is my code

        public static void Main(string[] args)
        {
            string basePath = @"C:\Users\pupy frias\Desktop\";
            string path = basePath + "TEMPLATE.xlsx";

            //Reading file
            byte[] bytes = File.ReadAllBytes(path);
            MemoryStream ms = new MemoryStream();
            ms.Write(bytes, 0, bytes.Length);

            SpreadsheetDocument document = SpreadsheetDocument.Open(ms, true);
            WorkbookPart workbookPart = document.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            Worksheet worksheet = worksheetPart.Worksheet;
            SheetData sheetData = worksheet.GetFirstChild<SheetData>();

            var row = new Row { RowIndex = 12 };
            var RNC = new Cell { CellReference = "B12", CellValue = new CellValue("123456789"), DataType = CellValues.Number };

            row.Append(RNC);
            sheetData.Append(row);

            workbookPart.Workbook.Save();
            document.Close();
            string rute = @$"{basePath}NEW FILE.xlsx";

            //SAVE FILE
            File.WriteAllBytes(rute, ms.ToArray());
        }

If i try to insert data to another file all it works.

I'm using Open-XML-SDK version 2.9.1

0 Answers
Related