在.Net中使用openXml打开Excel文件时出现错误消息?

时间:2019-05-22 10:20:37

标签: c# .net excel

我正在尝试使用打开的xml下载大型excel文件。我可以下载它。我的Excel文件包含超过10万条记录。当我双击该文件时,出现以下错误信息。

enter image description here

下面是我的代码。

public MemoryStream WriteExcelUsingOpenXML(IEnumerable<string> headers, IEnumerable<IEnumerable<string>> values, string worksheetName)
    {
      var stream = new MemoryStream();
      var headersArray = headers.ToArray();
      using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
      {
        WorkbookPart workbookPart = document.AddWorkbookPart();
        workbookPart.Workbook = new Workbook();

        WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet();

        Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

        Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = Constants.TimephaseWorksheet };

        sheets.Append(sheet);

        workbookPart.Workbook.Save();

        SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

        // Constructing header
        Row row = new Row();
        foreach (var data in headersArray)
        {
          row.Append(ConstructCell(headersArray[0], CellValues.SharedString));
        }

        // Insert the header row to the Sheet Data
        sheetData.AppendChild(row);
        // Inserting each employee
        foreach (var valuesEnumeration in values)
        {
          row = new Row();
          var flattend = valuesEnumeration.ToArray();
          row.Append(
              ConstructCell(flattend[0], CellValues.Number),
              ConstructCell(flattend[1], CellValues.SharedString),
              ConstructCell(flattend[2], CellValues.SharedString),
              ConstructCell(flattend[3], CellValues.Number),
              ConstructCell(flattend[4], CellValues.SharedString));

          sheetData.AppendChild(row);
        }

        worksheetPart.Worksheet.Save();
      }

      return stream;
    }

    private Cell ConstructCell(string value, CellValues dataType)
    {
      return new Cell()
      {
        CellValue = new CellValue(value),
        DataType = new EnumValue<CellValues>(dataType)
      };
    }

下面是excel文件的示例数据。

enter image description here

如何解决该错误?

0 个答案:

没有答案