1
我使用OpenXML來操作Excel文件。以編程方式對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.我正在尋找關於如何將我新創建的表單設置爲表單nr的提示。 1.
是的,確實如此。感謝您的提示。這個技巧: sheets.InsertAt(sheet,0); – Ilyas