Сломанный XSLX-файл после генерации их с помощью OpenXmlSdk - PullRequest
0 голосов
/ 18 марта 2020

Я просто пытаюсь сгенерировать XLSX-файлы, используя Open XML SDK 2.5. Для их чтения я использую Excel 365.

Я пытаюсь сгенерировать файл следующим образом:

private void CreateSpreadsheetWorkbook(string filepath)
    {
        // Create a spreadsheet document by supplying the filepath.
        // By default, AutoSave = true, Editable = true, and Type = xlsx.
        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, 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());

        //https://social.msdn.microsoft.com/Forums/office/en-US/4ae9ba85-d5d2-4ce8-a0ba-dece26ed7d2a/open-xml-sdk-for-making-font-bold?forum=oxmlsdk
        //https://stackoverflow.com/questions/34011985/add-style-to-excel-in-openxml
        WorkbookStylesPart stylesPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorkbookStylesPart>();
        stylesPart.Stylesheet = new Stylesheet();
        var stylesheet1 = spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet;

        DocumentFormat.OpenXml.Spreadsheet.Font font1 = new DocumentFormat.OpenXml.Spreadsheet.Font(
           new Bold(),
           new FontSize() { Val = 11 },
           new Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
           new FontName() { Val = "Calibri" });
        stylesPart.Stylesheet.AppendChild<Fonts>(new Fonts());
        stylesPart.Stylesheet.Fonts.Append(font1);


        UInt32Value fontId = Convert.ToUInt32(stylesPart.Stylesheet.Fonts.ChildElements.Count - 1);
        CellFormat cf = new CellFormat() { FontId = fontId, FillId = 0, BorderId = 0, ApplyFont = true };
        stylesPart.Stylesheet.AppendChild<CellFormats>(new CellFormats());
        stylesPart.Stylesheet.CellFormats.Append(cf);

        //stylesPart.Stylesheet.Save();

        // Append a new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
        sheets.Append(sheet);

        // Get the sheetData cell table.
        SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

        // Add a row to the cell table.
        Row row;
        row = new Row() { RowIndex = 1 };
        sheetData.Append(row);

        // In the new row, find the column location to insert a cell in A1.  
        Cell refCell = null;
        foreach (Cell cell in row.Elements<Cell>())
        {
            if (string.Compare(cell.CellReference.Value, "A1", true) > 0)
            {
                refCell = cell;
                break;
            }
        }

        // Add the cell to the cell table at A1.
        Cell newCell = new Cell() { CellReference = "A1" };
        row.InsertBefore(newCell, refCell);

        // Set the cell value to be a numeric value of 100.
        newCell.CellValue = new CellValue("100");
        newCell.DataType = new EnumValue<CellValues>(CellValues.Number);

        // Close the document.
        spreadsheetDocument.Close();
    }

Открывая сгенерированный XLSX-файл, я получил сообщение: «Хотите восстановить? файл?" - Если я это сделаю, это будет показано правильно. Что не так? Я что-то забыл?

С уважением:)

...