我正在尝试将标头行格式添加到我的OpenXML电子表格文档中,并且出现了上述错误。我很确定这与我添加样式有关,但是我无法弄清楚我做错了什么。
在尝试添加样式之前,文档已正确打开。
非常感谢您的帮助。
private void AddStyles()
{
stylesPart = doc.WorkbookPart.AddNewPart<WorkbookStylesPart>();
stylesPart.Stylesheet = new Stylesheet();
stylesPart.Stylesheet.Fonts = new Fonts();
stylesPart.Stylesheet.Fills = new Fills();
stylesPart.Stylesheet.Borders = new Borders();
stylesPart.Stylesheet.CellFormats = new CellFormats();
stylesPart.Stylesheet.Fonts.Append(new Font()); // normal
stylesPart.Stylesheet.Fonts.Append(new Font(new Bold())); // header
stylesPart.Stylesheet.Fills.Append(new Fill()); //default fill
stylesPart.Stylesheet.Borders.Append(new Borders()); // default border
stylesPart.Stylesheet.CellFormats.Append(new CellFormat() { FormatId = 0, FillId = 0, BorderId = 0, FontId = 0 });
stylesPart.Stylesheet.CellFormats.Append(new CellFormat() { FormatId = 1, FillId = 0, BorderId = 0, FontId = 1 });
}
当我打开它时,它会提示您对其进行正确的修复。但是,如果我在NotePad中打开它,则会发现一些疯狂的东西。
PK ÄCSM5øiº % xl/workbook.xml ¢ ( Á‚0†_eé]‡F !€/^õ æè`dÊã;1‘«·¯›æû«ÓìFñÄÀ–|
下面包含的其余代码供参考:
public OpenXMLExcelDoc(string sheetName = "Sheet 1")
{
ms = new MemoryStream();
doc = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook);
workBookPart = doc.AddWorkbookPart();
workBookPart.Workbook = new Workbook();
workSheetPart = workBookPart.AddNewPart<WorksheetPart>();
workSheetPart.Worksheet = new Worksheet();
sheets = workBookPart.Workbook.AppendChild(new Sheets());
sheet = new Sheet() { Id = workBookPart.GetIdOfPart(workSheetPart), SheetId = 1, Name = sheetName };
sheetData = workSheetPart.Worksheet.AppendChild(new SheetData());
sheets.Append(sheet);
AddStyles();
doc.Save();
}
public void StartRow()
{
row = new Row();
}
public void FinishRow()
{
sheetData.AppendChild(row);
}
public void AddCellData(string data, CellValues dataType, FontStyles style = FontStyles.Normal)
{
var newCell = new Cell()
{
CellValue = new CellValue(data),
DataType = new EnumValue<CellValues>(dataType),
StyleIndex = Convert.ToUInt32((int)style)
};
row.Append(newCell);
}
public void AddDataGrid(DataGrid dg)
{
StartRow();
for (var i = 0; i < dg.Columns.Count; i++)
{
AddCellData(dg.Columns[i].HeaderText, CellValues.String, FontStyles.Header);
}
FinishRow();
for (var i = 0; i < dg.Items.Count; i++)
{
var gridRow = dg.Items[i];
StartRow();
for(var j = 0; j < gridRow.Cells.Count; j++)
{
var gridCell = gridRow.Cells[j];
AddCellData(gridCell.Text, CellValues.String);
}
FinishRow();
}
}
public void Finish(string title, HttpResponse Response)
{
doc.Close();
Response.AddHeader("content-disposition", "attachment;filename=" + title + ".xlsx");
ms.Position = 0;
ms.CopyTo(Response.OutputStream);
Response.End();
}
答案 0 :(得分:0)
好吧,我仍然不明白我的代码为什么“不起作用”,但是基于this post的代码却可以。
private void AddStyles()
{
stylesPart = workBookPart.AddNewPart<WorkbookStylesPart>();
Fonts fonts = new Fonts(
new Font(),
new Font(new Bold())
);
Fills fills = new Fills(new Fill());
Borders borders = new Borders(new Border());
CellFormats cellFormats = new CellFormats(
new CellFormat(), // default
new CellFormat { FontId = 0, FillId = 0, BorderId = 0 }, // body
new CellFormat { FontId = 1, FillId = 0, BorderId = 0 } // header
);
stylesPart.Stylesheet = new Stylesheet(fonts, fills, borders, cellFormats);
stylesPart.Stylesheet.Save();
}