Excel在后面的代码中将数据添加到WorksheetPart

时间:2010-12-10 00:31:16

标签: c# .net excel openxml openxml-sdk

大家好我在以下代码中创建一个包含3个工作表的Excel文件。

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(path + @"\UrlReport.xlsx", SpreadsheetDocumentType.Workbook))
            {
                // create the workbook
                spreadSheet.AddWorkbookPart();
                spreadSheet.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

                // CREATE FIRST SHEET
                WorksheetPart newWorksheetPart1 = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();

                newWorksheetPart1.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();

                // create sheet data
                newWorksheetPart1.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                // save worksheet
                newWorksheetPart1.Worksheet.Save();

                // create the worksheet to workbook relation
                spreadSheet.WorkbookPart.Workbook.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheets());
                spreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
                {
                    Id = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart1),
                    SheetId = 1,
                    Name = "Sheet1"
                });

                //CREATE SECOND SHEET
                WorksheetPart newWorksheetPart2 = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
                newWorksheetPart2.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();

                // create sheet data
                newWorksheetPart2.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                // save worksheet
                newWorksheetPart2.Worksheet.Save();

                // create the worksheet to workbook relation
                spreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
                {
                    Id = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart2),
                    SheetId = 2,
                    Name = "Sheet2"

                });

                //CREATE THIRD SHEET
                WorksheetPart newWorksheetPart3 = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
                newWorksheetPart3.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();

                // create sheet data
                newWorksheetPart3.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                // save worksheet
                newWorksheetPart3.Worksheet.Save();

                // create the worksheet to workbook relation
                spreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
                {
                    Id = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart3),
                    SheetId = 3,
                    Name = "Sheet3"
                });

                spreadSheet.WorkbookPart.Workbook.Save();
            }

我需要知道如何填充3个数据表的单元格。我需要将一些字符串添加到某些可能位于某个数据表或多维数组中的单元格中。

1 个答案:

答案 0 :(得分:10)

private static void InsertValuesInWorksheet(WorksheetPart worksheetPart, IEnumerable<string> values)
{
    var worksheet = worksheetPart.Worksheet;
    var sheetData = worksheet.GetFirstChild<SheetData>();
    var row = new Row { RowIndex = 1 };  // add a row at the top of spreadsheet
    sheetData.Append(row);

    int i = 0;
    foreach (var value in values)
    {
        var cell = new Cell
                            {
                                CellValue = new CellValue(value),
                                DataType = new EnumValue<CellValues>(CellValues.String)
                            };

        row.InsertAt(cell, i);
        i++;
    }
}

此方法将向指定的工作表添加新行,并使用数组中的值填充行的单元格。在您的代码示例中,可以像这样调用它:

var values = new[] {"foo", "bar", "baz"};

InsertValuesInWorksheet(newWorksheetPart1, values);
InsertValuesInWorksheet(newWorksheetPart2, values);
InsertValuesInWorksheet(newWorksheetPart3, values);