列宽未反映在Excel工作表中

问题描述

我正在使用DocumentFormat.OpenXml用C#创建Excel工作表。

我已经在开始时定义了defaultColumn宽度,但是没有反映在结果中。我已经附上了我的代码

SpreadsheetDocument document = SpreadsheetDocument.Create(excelFile.fileMemoryStream,SpreadsheetDocumentType.Workbook);

WorkbookPart workbookpart = document.AddWorkbookPart();
workbookpart.Workbook = new Workbook();

WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();

var sheetData = new SheetData();
SheetFormatProperties properties = new SheetFormatProperties() { BaseColumnWidth = 0,DefaultColumnWidth = 20D,DefaultRowHeight = 0D };

Worksheet worksheet = new Worksheet();
WorkbookStylesPart stylePart = workbookpart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = GenerateStylesheet();
stylePart.Stylesheet.Save();

worksheet.AppendChild(properties);
worksheet.AppendChild(sheetData);

worksheetPart.Worksheet = worksheet;

// Add Sheets to the Workbook.
Sheets sheets = document.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

Sheet sheet = new Sheet()
                {
                    Id = document.WorkbookPart.
                    GetIdOfPart(worksheetPart),SheetId = 1,Name = "Invoice"
                };

sheets.Append(sheet);

sheetData = AppendDetailsToSheet(invoice);
worksheetPart.Worksheet = new Worksheet(sheetData);
workbookpart.Workbook.Save();
document.Close();
excelFile.fileMemoryStream.Position = 0;
return excelFile;

我的stylesheetClass

private static Stylesheet GenerateStylesheet()
{
            Stylesheet styleSheet = null;

            Fonts fonts = new Fonts(
                new DocumentFormat.OpenXml.Spreadsheet.Font( // Index 0 - default
                    new FontSize() { Val = 10 },new FontName() { Val = "Calibri" },new BackgroundColor() { Rgb = "#cfe2f3" }


                ),new DocumentFormat.OpenXml.Spreadsheet.Font( // Index 1 - header
                    new FontSize() { Val = 10 },new Bold(),new BackgroundColor() { Rgb = "#cfe2f3" }

                ),new DocumentFormat.OpenXml.Spreadsheet.Font( // Index 2  - header
                    new FontSize() { Val = 10 },new BackgroundColor() { Rgb = "#cfe2f3" }

                ));

            Fills fills = new Fills(
                    new Fill(new PatternFill() { PatternType = PatternValues.None }),// Index 0 - default
                    new Fill(new PatternFill(new ForegroundColor { Rgb = "#CFE2F3" }) { PatternType = PatternValues.MediumGray }),// Index 1 - default
                    new Fill(new PatternFill(new ForegroundColor { Rgb = new HexBinaryValue() { Value = "66666666" } })
                    { PatternType = PatternValues.solid }) // Index 2 - header
                );

            Borders borders = new Borders(
                    new Border(),// index 0 default
                    new Border( // index 1 black border
                        new LeftBorder(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Auto = true,Rgb = "#DCDCDC" }) { Style = BorderStyleValues.Thin },new RightBorder(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Auto = true,new TopBorder(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Auto = true,new BottomBorder(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Auto = true,new DiagonalBorder())
                );

            CellFormats cellFormats = new CellFormats(
                    new CellFormat(),// default
                    new CellFormat { FontId = 0,FillId = 0,BorderId = 1,ApplyBorder = false,ApplyFill = true,ApplyAlignment = true,Alignment = new Alignment() { WrapText = false } },// header },// body
                    new CellFormat { FontId = 1,BorderId = 0,ApplyFont = true,// header
                    new CellFormat { FontId = 1,ApplyBorder = true,ApplyFont = false,ApplyAlignment = false,Alignment = new Alignment() { WrapText = false } } // header
                );

            styleSheet = new Stylesheet(fonts,fills,borders,cellFormats);

            return styleSheet;
}

缩进到工作表中的数据即将到来。仅与列宽有关。为什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)