8

こんにちは、次のコードで 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 つのデータシートのセルにデータを入力する方法を知る必要があります。おそらくいくつかのデータテーブルまたは多次元配列になるいくつかのセルに文字列を追加する必要があります。

4

1 に答える 1

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);
于 2011-02-10T18:27:08.167 に答える