办公室开放为excel设置单个decimla位置百分比

时间:2016-07-12 12:10:43

标签: c# excel openxml

我正在使用OPENXML创建excel文档,我希望以百分比格式显示单个小数位的百分比,如“85.5%”。 我无法找到任何适当的解决方案。

以下是我创建样式表的功能。

private Stylesheet GenerateStyleSheet(NumberingFormat nf2decimal)
    {
        return new Stylesheet(
            new Fonts(
                new Font(new FontSize() { Val = 11 }, new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }, new FontName() { Val = "Calibri" }),
                new Font(new Bold(), new FontSize() { Val = 12 }, new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }, new FontName() { Val = "Calibri" }),
                new Font(new Bold(), new FontSize() { Val = 11 }, new Color() { Rgb = new HexBinaryValue() { Value = "FCCB02" } }, new FontName() { Val = "Calibri" }),
                new Font(new Bold(), new FontSize() { Val = 11 }, new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }, new FontName() { Val = "Calibri" }),
                new Font(new Italic(), new FontSize() { Val = 11 }, new Color() { Rgb = new HexBinaryValue() { Value = "008000" } }, new FontName() { Val = "Calibri" }),
                new Font(new Bold(), new FontSize() { Val = 14 }, new Color() { Rgb = new HexBinaryValue() { Value = "008000" } }, new FontName() { Val = "Calibri" })
            ),
            new Fills(
                new Fill(new PatternFill() { PatternType = PatternValues.None }),
                new Fill(new PatternFill() { PatternType = PatternValues.Gray125 }),
                new Fill(new PatternFill(new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "6E6E6E" } }) { PatternType = PatternValues.Solid }),
                new Fill(new PatternFill(new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "FFB3B3" } }) { PatternType = PatternValues.Solid }),
                new Fill(new PatternFill(new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "B3FFB3" } }) { PatternType = PatternValues.Solid }),
                new Fill(new PatternFill(new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "FFDB99" } }) { PatternType = PatternValues.Solid }),
                new Fill(new PatternFill(new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "EAEAEA" } }) { PatternType = PatternValues.Solid })
            ),
            new Borders(
                new Border(new LeftBorder(), new RightBorder(), new TopBorder(), new BottomBorder(), new DiagonalBorder()),
                new Border(new LeftBorder() { Style = BorderStyleValues.Thick }, new RightBorder() { Style = BorderStyleValues.Thick }, new TopBorder() { Style = BorderStyleValues.Thick }, new BottomBorder() { Style = BorderStyleValues.Thick }, new DiagonalBorder()),
                new Border(new LeftBorder() { Style = BorderStyleValues.Thin }, new RightBorder() { Style = BorderStyleValues.Thin }, new TopBorder() { Style = BorderStyleValues.Thin }, new BottomBorder() { Style = BorderStyleValues.Thin }, new DiagonalBorder())
                , new Border(new LeftBorder(), new RightBorder(), new TopBorder(), new BottomBorder(), new DiagonalBorder())
            ),

            new CellFormats(
            //0
                new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 },
            //1
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center, WrapText = true }) { FontId = 1, FillId = 0, BorderId = 1, ApplyBorder = true, },
            //2
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center, WrapText = true }) { FontId = 3, FillId = 0, BorderId = 2, ApplyFill = false, ApplyBorder = true },
            //3
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center, WrapText = true }) { FontId = 5, FillId = 6, BorderId = 2, ApplyBorder = true, ApplyFill = true },
            //4
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center, WrapText = true }) { FontId = 2, FillId = 2, BorderId = 2, ApplyFill = true, ApplyBorder = true },
            //5
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center, WrapText = true }) { FontId = 3, FillId = 0, BorderId = 2, ApplyFill = false, ApplyBorder = true },
            //6
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 0, FillId = 0, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 3 },
            //7
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 0, FillId = 0, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 10 },
            //8
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 0, FillId = 0, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 9 },
            //9
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 4, FillId = 3, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 3 },
            //10
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 4, FillId = 3, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 9 },
            //11
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 4, FillId = 4, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 3 },
            //12
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 4, FillId = 4, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 9 },
            //13
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 4, FillId = 5, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 3 },
            //14
                new CellFormat(new Alignment() { WrapText = true }) { FontId = 4, FillId = 5, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = nf2decimal.NumberFormatId },
            //15
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center, WrapText = true }) { FontId = 0, FillId = 0, BorderId = 2, ApplyFill = false, ApplyBorder = true },
            //16
                new CellFormat(new Alignment() { Vertical = VerticalAlignmentValues.Center, WrapText = true }) { FontId = 0, FillId = 0, BorderId = 2, ApplyFill = false, ApplyBorder = true, ApplyNumberFormat = true, NumberFormatId = 3 }
            )

        ); // return
    }

有人可以帮助我吗?

0 个答案:

没有答案