如何使用OpenXML SDK 2.0向Excel添加行?

时间:2012-09-30 00:14:22

标签: excel openxml openxml-sdk

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

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

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

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

0 个答案:

没有答案