Извините за мой английский, пожалуйста.Я новичок в OPEN XML SDK.Мой вопрос: как создать файл Excel с несколькими листами.Сердечно
Мой пример кода:
public void CreateExcelDoc(string fileName)
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
int i = 0;
for (i = 0; i < 3; i++)
{
WorksheetPart worksheetPart = workbookPart.AddNewPart();
worksheetPart.Worksheet = new Worksheet();
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = Convert.ToUInt32(i), Name = "Employees" + i.ToString() };
sheets.Append(sheet);
workbookPart.Workbook.Save();
List<Employee> employees = Employees.EmployeesList;
SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
// Constructing header
Row row = new Row();
row.Append(
ConstructCell("Id", CellValues.String),
ConstructCell("Name", CellValues.String),
ConstructCell("Birth Date", CellValues.String),
ConstructCell("Salary", CellValues.String));
// Insert the header row to the Sheet Data
sheetData.AppendChild(row);
// Inserting each employee
foreach (var employee in employees)
{
row = new Row();
row.Append(
ConstructCell(employee.Id.ToString(), CellValues.Number),
ConstructCell(employee.Name, CellValues.String),
ConstructCell(employee.DOB.ToString("yyyy/MM/dd"), CellValues.String),
ConstructCell(employee.Salary.ToString(), CellValues.Number));
sheetData.AppendChild(row);
}
worksheetPart.Worksheet.Save();
}
}
}
private Cell ConstructCell(string value, CellValues dataType)
{
return new Cell()
{
CellValue = new CellValue(value),
DataType = new EnumValue<CellValues>(dataType)
};
}