1

我是 OpenXML 的新手,并且在添加一行(也包含单元格数据)之后,一直在努力在 A1 中添加一个包含单元格数据的新行。

所以基本上我想在“第 1 行”列“A1”中插入“测试”,在“第 2 行”列“A1”中插入“测试”。

这是我的代码,它看起来不错并创建了文件,但是 Excel 没有打开它。我在 OpenOffice 中打开它,它只显示一行而不是两行。当我注释掉将 row2 附加到工作表数据时,它工作正常。所以我想错误地创建第二行。任何帮助表示赞赏。先感谢您。这是代码:

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");
}
4

0 回答 0