ClosedXML生成格式错误的xlsx

时间:2015-08-07 03:19:12

标签: excel openxml malformed closedxml

当我打开文件时,似乎无法输出一个包含ClosedXML的好文件

我们发现了' filename.xlsx'中某些内容的问题。你想让我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击“是”。

using (var workbook = new XLWorkbook())
{
    var worksheet = workbook.AddWorksheet("name");
    worksheet.Row(1).Cell(i + 1).SetValue("test");
    worksheet.Row(k + 2).Cell(column.Order + 1).SetValue("test value");
    using (var memoryStream = new MemoryStream())
    {
        workbook.SaveAs(memoryStream);
        memoryStream.Seek(0, SeekOrigin.Begin);
        return memoryStream.GetBuffer();
    }
}

1 个答案:

答案 0 :(得分:7)

它使用ToArray()而不是GetBuffer()来解决问题。干杯

using (var workbook = new XLWorkbook())
{
    var worksheet = workbook.AddWorksheet("name");
    worksheet.Row(1).Cell(i + 1).SetValue("test");
    worksheet.Row(k + 2).Cell(column.Order + 1).SetValue("test value");
    using (var memoryStream = new MemoryStream())
    {
        workbook.SaveAs(memoryStream);
        memoryStream.Position = 0;
        return memoryStream.ToArray();
    }
}