1

I've been using OPEN XML to create a spread sheet which can then be downloaded. I want to set the column widths and also change the header to bold. I've done this using Office.Interop.Excel but I'm struggling with this format. My code is below, you can see I've set up a font but have been unable to assign it to my spread sheet, I have got messages saying it's not possible to assign to a tree.

        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook);    //Open(fileName, true);
        try
        {

            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData());

            DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>(new DocumentFormat.OpenXml.Spreadsheet.Sheets());
            Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Models" };
            sheets.Append(sheet);

            DocumentFormat.OpenXml.Spreadsheet.Font boldFont = new DocumentFormat.OpenXml.Spreadsheet.Font();
            Bold bFontBold = new Bold();
            boldFont.Append(bFontBold);

            DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();
            SheetData sheetData = new SheetData();

 ........

                Row row = new Row();

                Cell cell = new Cell()
                {
                    CellReference = "A" + (intI + 1),
                    DataType = CellValues.String,
                    CellValue = new CellValue(ModelBookrowDetail[0])

                };
                row.Append(cell);

                Cell cell2 = new Cell()
                {
                    CellReference = "B" + (intI + 1),
                    DataType = CellValues.String,
                    CellValue = new CellValue(ModelBookrowDetail[1])
                };
                row.Append(cell2);

 .........

                sheetData.Append(row);
            }

            worksheet.Append(sheetData);
            worksheetPart.Worksheet = worksheet;

            spreadsheetDocument.Close();
        }
        catch
        {
            spreadsheetDocument.Close();
        }
4

1 回答 1

4

I don't know how set you are on using OpenXml. I might suggest looking into ClosedXml. Up until now it has saved me a lot of manhours. It only has one downside to it, and that is that it doesn't manage Word documents like OpenXml does.

ClosedXml is a more object oriented Open Source Library, and is far more well documented in contrast to what i have experienced with OpenXml.

You can find ClosedXml here: https://github.com/ClosedXML/ClosedXML

Best of luck

Olliver

于 2013-09-06T08:09:36.577 回答