使用open xml时,不同的excel列外观

时间:2014-05-17 19:18:16

标签: excel openxml openxml-sdk

这个问题经过多次编辑,我发现了问题及其解决方案。我编辑了这篇文章供其他用户使用。
我使用open xml创建了一个excel文件。创建的excel文件打开,没有错误。它有以下问题:

  1. 列的大小设置正确,但显示的大于我的预期(大于模板文件)。
    下图显示了模板文件: The template excel file
    下一个图像显示使用open xml创建的文件:
    enter image description here
    如您所见,第二个文件的列更宽,而值之间没有差异。
  2. 修改

    代码以以下行开头:

    using OpenXml=DocumentFormat.OpenXml;
    using ExcelSpreadSheet = DocumentFormat.OpenXml.Spreadsheet;
    using Packaging = DocumentFormat.OpenXml.Packaging;
    


            Packaging.SpreadsheetDocument spreadsheet = Packaging.SpreadsheetDocument.Create(filename, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook);
    
    
            Packaging.ExtendedFilePropertiesPart extendedFilePropertiesPart = spreadsheet.AddExtendedFilePropertiesPart();
            GenerateExtendedFilePropertiesPart(extendedFilePropertiesPart);
    
            Packaging.CoreFilePropertiesPart coreFilePropertiesPart = spreadsheet.AddCoreFilePropertiesPart();
            GenerateCoreFilePropertiesPart(coreFilePropertiesPart);
    
            Packaging.WorkbookPart workBookPart = spreadsheet.AddWorkbookPart();
            workBookPart.Workbook = new ExcelSpreadSheet.Workbook();
    
            //Setting Style Part
            SetSheetStyle(workBookPart);
    
            //Generated using xml tools
            Packaging.ThemePart themePart = workBookPart.AddNewPart<Packaging.ThemePart>();
            GenerateThemePart(themePart);
    
            Packaging.WorksheetPart workSheetPart = workBookPart.AddNewPart<Packaging.WorksheetPart>();
    
            workSheetPart.Worksheet = new ExcelSpreadSheet.Worksheet();
    
            ExcelSpreadSheet.SheetDimension sheetDimension1 = new ExcelSpreadSheet.SheetDimension() { Reference = "A12:AA17" };
            workSheetPart.Worksheet.Append(sheetDimension1);
    
            ExcelSpreadSheet.Columns columns = new ExcelSpreadSheet.Columns();
            for (uint col = 0; col < PropertiesOfColumns.Length; col++)
            {
                UInt32 rangeIndex = col + 1;
                ExcelSpreadSheet.Column column1 = new ExcelSpreadSheet.Column()
                {
                    Min = (OpenXml.UInt32Value)rangeIndex,
                    Max = (OpenXml.UInt32Value)rangeIndex,
                    Width = PropertiesOfColumns[col].Width,//Width of every column
                    CustomWidth = OpenXml.BooleanValue.FromBoolean(true)
                };
                columns.Append(column1);
            }
    
            workSheetPart.Worksheet.Append(columns);
    
            ExcelSpreadSheet.SheetData sheetData = new ExcelSpreadSheet.SheetData();
            workSheetPart.Worksheet.Append(sheetData);
    
            ExcelSpreadSheet.PageMargins pageMargins1 = new ExcelSpreadSheet.PageMargins() { Left = 0.7D, Right = 0.7D, Top = 0.75D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D };
            workSheetPart.Worksheet.Append(pageMargins1);
    
            ExcelSpreadSheet.Sheets sheets = workBookPart.Workbook.AppendChild<ExcelSpreadSheet.Sheets>(new ExcelSpreadSheet.Sheets());
    
            ExcelSpreadSheet.Sheet sheet = new ExcelSpreadSheet.Sheet()
            {
                Id = workBookPart.GetIdOfPart(workSheetPart),
                SheetId = 1,
                Name = "mySheet"
            };
            sheets.Append(sheet);
    
            workSheetPart.Worksheet.SheetViews = new ExcelSpreadSheet.SheetViews();
    
            ExcelSpreadSheet.SheetView sheetView = new ExcelSpreadSheet.SheetView() {
                RightToLeft = true,
                TabSelected = true,
                ZoomScale=(OpenXml.UInt32Value)60U,
                ZoomScaleNormal=(OpenXml.UInt32Value)60U,
                WorkbookViewId = (DocumentFormat.OpenXml.UInt32Value)0U };
    
            workSheetPart.Worksheet.SheetViews.Append(sheetView);
    
            //And so on
    

    我对SetSheetStyle更加怀疑:

    private static void SetSheetStyle(Packaging.WorkbookPart workBookPart )
    
        {
    
    
            //Fonts
            ExcelSpreadSheet.Fonts fonts = new ExcelSpreadSheet.Fonts() { Count = (OpenXml.UInt32Value)2U, KnownFonts = true };
    
            ExcelSpreadSheet.Font font1 = new ExcelSpreadSheet.Font();
            ExcelSpreadSheet.Bold bold1 = new ExcelSpreadSheet.Bold();
            ExcelSpreadSheet.FontSize fontSize1 = new ExcelSpreadSheet.FontSize() { Val = 16D };
            ExcelSpreadSheet.Color color1 = new ExcelSpreadSheet.Color() { Theme = (OpenXml.UInt32Value)1U };
            ExcelSpreadSheet.FontName fontName1 = new ExcelSpreadSheet.FontName() { Val = "B Nazanin" };
            ExcelSpreadSheet.FontFamilyNumbering fontFamilyNumbering1 = new ExcelSpreadSheet.FontFamilyNumbering() { Val = 2 };
            ExcelSpreadSheet.FontScheme fontScheme1 = new ExcelSpreadSheet.FontScheme() { Val = ExcelSpreadSheet.FontSchemeValues.Minor };
    
            font1.Append(fontSize1);
            font1.Append(bold1);
            font1.Append(color1);
            font1.Append(fontName1);
            font1.Append(fontFamilyNumbering1);
            font1.Append(fontScheme1);
    
            ExcelSpreadSheet.Font font2 = new ExcelSpreadSheet.Font();
            ExcelSpreadSheet.Bold bold2 = new ExcelSpreadSheet.Bold();
            ExcelSpreadSheet.FontSize fontSize2 = new ExcelSpreadSheet.FontSize() { Val = 11D };
            ExcelSpreadSheet.Color color2 = new ExcelSpreadSheet.Color() { Theme = (OpenXml.UInt32Value)1U };
            ExcelSpreadSheet.FontName fontName2 = new ExcelSpreadSheet.FontName() { Val = "Calibri" };
            ExcelSpreadSheet.FontFamilyNumbering fontFamilyNumbering2 = new ExcelSpreadSheet.FontFamilyNumbering() { Val = 2 };
            ExcelSpreadSheet.FontScheme fontScheme2 = new ExcelSpreadSheet.FontScheme() { Val = ExcelSpreadSheet.FontSchemeValues.Minor };
    
            //font2.Append(bold1);
            font2.Append(fontSize2);
            font2.Append(color2);
            font2.Append(fontName2);
            font2.Append(fontFamilyNumbering2);
            font2.Append(fontScheme2);
    
            fonts.Append(font1);
            fonts.Append(font2);
            //Fills
            ExcelSpreadSheet.Fills fills = new ExcelSpreadSheet.Fills() { Count = (OpenXml.UInt32Value)3U };
    
            ExcelSpreadSheet.Fill fill1 = new ExcelSpreadSheet.Fill();
            ExcelSpreadSheet.PatternFill patternFill1 = new ExcelSpreadSheet.PatternFill() { PatternType = ExcelSpreadSheet.PatternValues.None };
    
            fill1.Append(patternFill1);
    
            ExcelSpreadSheet.Fill fill2 = new ExcelSpreadSheet.Fill();
            ExcelSpreadSheet.PatternFill patternFill2 = new ExcelSpreadSheet.PatternFill() { PatternType = ExcelSpreadSheet.PatternValues.Gray125 };
    
            fill2.Append(patternFill2);
    
            ExcelSpreadSheet.Fill fill3 = new ExcelSpreadSheet.Fill();
    
            ExcelSpreadSheet.PatternFill patternFill3 = new ExcelSpreadSheet.PatternFill() { PatternType = ExcelSpreadSheet.PatternValues.Solid };
            ExcelSpreadSheet.ForegroundColor foregroundColor1 = new ExcelSpreadSheet.ForegroundColor() { Rgb = "FFFFCC66" };
            ExcelSpreadSheet.BackgroundColor backgroundColor1 = new ExcelSpreadSheet.BackgroundColor() { Indexed = (OpenXml.UInt32Value)64U };
    
            patternFill3.Append(foregroundColor1);
            patternFill3.Append(backgroundColor1);
    
            fill3.Append(patternFill3);
    
            fills.Append(fill1);
            fills.Append(fill2);
            fills.Append(fill3);
    
            //Borders
    
            ExcelSpreadSheet.Borders borders = new ExcelSpreadSheet.Borders() { Count = (OpenXml.UInt32Value)2U };
    
            ExcelSpreadSheet.Border border1 = new ExcelSpreadSheet.Border();
            ExcelSpreadSheet.LeftBorder leftBorder1 = new ExcelSpreadSheet.LeftBorder();
            ExcelSpreadSheet.RightBorder rightBorder1 = new ExcelSpreadSheet.RightBorder();
            ExcelSpreadSheet.TopBorder topBorder1 = new ExcelSpreadSheet.TopBorder();
            ExcelSpreadSheet.BottomBorder bottomBorder1 = new ExcelSpreadSheet.BottomBorder();
            ExcelSpreadSheet.DiagonalBorder diagonalBorder1 = new ExcelSpreadSheet.DiagonalBorder();
    
            border1.Append(leftBorder1);
            border1.Append(rightBorder1);
            border1.Append(topBorder1);
            border1.Append(bottomBorder1);
            border1.Append(diagonalBorder1);
    
            ExcelSpreadSheet.Border border2 = new ExcelSpreadSheet.Border();
    
            ExcelSpreadSheet.LeftBorder leftBorder2 = new ExcelSpreadSheet.LeftBorder() { Style = ExcelSpreadSheet.BorderStyleValues.Thin };
            color1 = new ExcelSpreadSheet.Color() { Indexed = (OpenXml.UInt32Value)64U };
    
            leftBorder2.Append(color1);
    
            ExcelSpreadSheet.RightBorder rightBorder2 = new ExcelSpreadSheet.RightBorder() { Style = ExcelSpreadSheet.BorderStyleValues.Thin };
            color2 = new ExcelSpreadSheet.Color() { Indexed = (OpenXml.UInt32Value)64U };
    
            rightBorder2.Append(color2);
    
            ExcelSpreadSheet.TopBorder topBorder2 = new ExcelSpreadSheet.TopBorder() { Style = ExcelSpreadSheet.BorderStyleValues.Thin };
            ExcelSpreadSheet.Color color3 = new ExcelSpreadSheet.Color() { Indexed = (OpenXml.UInt32Value)64U };
    
            topBorder2.Append(color3);
            ExcelSpreadSheet.BottomBorder bottomBorder2 = new ExcelSpreadSheet.BottomBorder();
            ExcelSpreadSheet.DiagonalBorder diagonalBorder2 = new ExcelSpreadSheet.DiagonalBorder();
    
            border2.Append(leftBorder2);
            border2.Append(rightBorder2);
            border2.Append(topBorder2);
            border2.Append(bottomBorder2);
            border2.Append(diagonalBorder2);
    
            borders.Append(border1);
            borders.Append(border2);
    
    
    
    
    
            ExcelSpreadSheet.CellFormats cellFormats = new ExcelSpreadSheet.CellFormats() { Count = (OpenXml.UInt32Value)3U };
            //Default Cell format (Not Applied)
            ExcelSpreadSheet.CellFormat cellFormat1 = new ExcelSpreadSheet.CellFormat()
            {
                NumberFormatId = (OpenXml.UInt32Value)0U,
                FontId = (OpenXml.UInt32Value)0U,
                FillId = (OpenXml.UInt32Value)0U,
                BorderId = (OpenXml.UInt32Value)0U,
                FormatId = (OpenXml.UInt32Value)0U
            };
    
            //Style for Header
            ExcelSpreadSheet.CellFormat cellFormat2 = new ExcelSpreadSheet.CellFormat()
            {
                NumberFormatId = (OpenXml.UInt32Value)0U,
                FontId = (OpenXml.UInt32Value)0U,
                FillId = (OpenXml.UInt32Value)2U,
                BorderId = (OpenXml.UInt32Value)1U,
                FormatId = (OpenXml.UInt32Value)0U,
                ApplyFont = true,
                ApplyFill = true,
                ApplyBorder = true,
                ApplyAlignment=true
            };
    
            ExcelSpreadSheet.Alignment alignment1 = new ExcelSpreadSheet.Alignment() { Horizontal = ExcelSpreadSheet.HorizontalAlignmentValues.Center, Vertical = ExcelSpreadSheet.VerticalAlignmentValues.Center, WrapText = true };
            cellFormat2.Append(alignment1);
    
    
            //Style for Rows
            ExcelSpreadSheet.CellFormat cellFormat3 = new ExcelSpreadSheet.CellFormat()
            {
                NumberFormatId = (OpenXml.UInt32Value)0U,
                FontId = (OpenXml.UInt32Value)1U,
                FillId = (OpenXml.UInt32Value)0U,
                BorderId = (OpenXml.UInt32Value)1U,
                FormatId = (OpenXml.UInt32Value)0U,
                ApplyFont = true,
                ApplyBorder = true
            };
    
            cellFormats.Append(cellFormat1);
            cellFormats.Append(cellFormat2);
            cellFormats.Append(cellFormat3);
            //Cell Style
            ExcelSpreadSheet.CellStyles cellStyles = new ExcelSpreadSheet.CellStyles() { Count = (OpenXml.UInt32Value)2U };
            ExcelSpreadSheet.CellStyle cellStyle1 = new ExcelSpreadSheet.CellStyle() { Name = "Normal", FormatId = (OpenXml.UInt32Value)0U, BuiltinId = (OpenXml.UInt32Value)0U };
    
            cellStyles.Append(cellStyle1);
    
            //ExcelSpreadSheet.CellStyle cellStyle2 = new ExcelSpreadSheet.CellStyle() { Name = "Normal", FormatId = (OpenXml.UInt32Value)3U, BuiltinId = (OpenXml.UInt32Value)0U };
    
            //cellStyles.Append(cellStyle2);
    
    
    
            workBookPart.AddNewPart<Packaging.WorkbookStylesPart>();
            workBookPart.WorkbookStylesPart.Stylesheet = new ExcelSpreadSheet.Stylesheet();
            ExcelSpreadSheet.Stylesheet stylesheet1 = workBookPart.WorkbookStylesPart.Stylesheet;
    
    
    
            stylesheet1.Append(fonts);
            stylesheet1.Append(fills);
            stylesheet1.Append(borders);
    
            stylesheet1.Append(cellFormats);
            stylesheet1.Append(cellStyles);
            //.Append(stylesheet1);
    
            workBookPart.WorkbookStylesPart.Stylesheet.Save();
    
    
        }
    

1 个答案:

答案 0 :(得分:0)

我发现了一个问题的线索。第二个问题是因为单元格样式设置为具有不同的字体。 这是帮助我制作代码的链接。 Font of the column HeadingMeasurement units and rulers in Excel
我为cellstyle的设置字体更改了以下代码:

ExcelSpreadSheet.CellStyle cellStyle1 = new ExcelSpreadSheet.CellStyle() { Name = "Normal", FormatId = (OpenXml.UInt32Value)1U, BuiltinId = (OpenXml.UInt32Value)0U };

我设置FormatId =(OpenXml.UInt32Value)1U,这意味着为单元格样式设置第二个字体(“Calibri 11”)。