-->

How to add a row to Excel using OpenXML SDK 2.0?

2020-08-01 04:44发布

问题:

I am new to OpenXML and have been struggling with adding a new row with cell data in A1 after a row (with cell data as well) has been added.

So essentially I want to insert "Test" in "row 1" column "A1" and "test" in "row 2" column "A1".

Here is my code, it looks sound and creates the file however Excel does not open it. I opened it in OpenOffice and it only shows one row instead of two. When I comment out appending row2 to the sheetdata, it works fine. So I am thinking am creating the 2nd row incorrectly. Any help is appreciated. Thank you in advance. Here is the code:

using (SpreadsheetDocument spreadSheetDocument =
                SpreadsheetDocument.Create("generated.xlsx", SpreadsheetDocumentType.Workbook))
            {
     //Add a WorkbookPart to the document.
                WorkbookPart workbookpart = spreadSheetDocument.AddWorkbookPart();
                //create new workbook
                workbookpart.Workbook = new Workbook();

                // Add a WorksheetPart to the WorkbookPart.
                WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                //instantiate new worksheet with new sheetdata
                worksheetPart.Worksheet = new Worksheet(new SheetData());

                //Add Sheets to the Workbook.
                Sheets sheets = spreadSheetDocument.WorkbookPart.Workbook.
                    AppendChild<Sheets>(new Sheets());

                DocumentFormat.OpenXml.UInt32Value sheetId = 1;

                //Append a new worksheet and associate it with the workbook.                
                Sheet sheet = new Sheet();

                sheet.Id = spreadSheetDocument.WorkbookPart.GetIdOfPart(worksheetPart);
                sheet.SheetId = sheetId;
                sheet.Name = new StringValue("test_" + 1);

                sheets.Append(sheet);

                //Get the sheetData cell table.
                SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

                UInt32Value rowindex = 1;
                UInt32Value rowindex2 = 2;

                // Add a row to the cell table.
                Row row = new Row() { RowIndex = rowindex };
                Cell newCell = new Cell();
                newCell.DataType = CellValues.InlineString;
                newCell.CellReference = "A1";
                InlineString inlineString = new InlineString();
                Text t = new Text();
                t.Text = "test";
                inlineString.Append(t);
                newCell.AppendChild(inlineString);
                row.AppendChild(newCell);

                sheetData.AppendChild(row);

                rowindex++;
                // Add a row to the cell table.
                Row row2 = new Row() { RowIndex = rowindex2 };
                Cell newCell2 = new Cell();
                newCell2.DataType = CellValues.InlineString;
                newCell2.CellReference = "A1";
                InlineString inlineString2 = new InlineString();
                Text t2 = new Text();
                t2.Text = "test";
                inlineString2.Append(t2);
                newCell2.AppendChild(inlineString2);
                row2.AppendChild(newCell2);

                sheetData.AppendChild(row2);

                workbookpart.Workbook.Save();

                // Close the document.
                spreadSheetDocument.Close();

                MessageBox.Show("Success");
}