我使用OpenXML来操作Excel文件。
我将Excel文件作为内存流发送,编辑它们然后将它们发送回浏览器,以便它们在客户端办公程序中打开。我使用以下代码创建新的电子表格:
public static void InsertWorksheet(string docName)
{
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
{
// Add a blank WorksheetPart.
WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);
// Get a unique ID for the new worksheet.
uint sheetId = 1;
if (sheets.Elements<Sheet>().Count() > 0)
{
sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
// Give the new worksheet a name.
string sheetName = "Sheet" + sheetId;
// Append the new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
}
}
我的问题是此工作表中的工作表中最后添加了此工作表。我希望它是表单nr 1.我正在寻找有关如何将我新创建的工作表设置为工作表的提示。 1.
答案 0 :(得分:2)
也许代替sheets.Append
你会在一开始就调用插入它的东西?有Insert
或Prepend
方法吗?