我尝试使用openXML使用以下代码在C#中创建文件xlsx文件:
if (!File.Exists(@SpreadSheetFile))
{
try
{
// Create a spreadsheet document by supplying the filepath.
// By default, AutoSave = true, Editable = true, and Type = xlsx.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(SpreadSheetFile, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = sheetName };
sheets.Append(sheet);
workbookpart.Workbook.Save();
// Close the document.
spreadsheetDocument.Close();
isNewFileAndSheetCreated = true;
}
catch (Exception ee)
{
throw new System.InvalidOperationException(SpreadSheetFile + " tidak dapat dibuat" + Environment.NewLine + ee.Message);
}
无法在2016年的Microsoft Office中打开,但在wps office中打开。 请帮忙,代码有什么问题吗?
答案 0 :(得分:0)
解决了,我在github上找到后将openXML更新到2.7版。它说:
新闻:此repo上的Office2016分支包含Open-Xml-Sdk v2.7的预览版,该版本支持文件格式的Office 2016扩展。
我使用的是从微软网站下载的2.5(我认为)不是支持办公室2016.它已经解决了。