使用样式在现有 Excel 文档中添加行

发布于 2024-09-10 15:08:01 字数 245 浏览 7 评论 0原文

我正在使用 C# 2008 (FW 3.5)、OpenXML SDK 2.0 和 Excel 2007 文档。

该程序从数据库中获取值并创建一个动态表,并将这些值粘贴到 Excel 模板中。

所有这些工作都很好,但我需要其他东西:

我需要在模板中创建指定数量的新行,但具有样式(边框、字体、背景色等),但我不知道如何制作它。

有人可以帮我提供一个示例代码来实现这个吗?

非常感谢,请原谅我的英语。

I'm working with C# 2008 (FW 3.5), OpenXML SDK 2.0 and a Excel 2007 document's.

The program take's values from a Database and create a dynamic table, and the values are pasted in a Excel template.

All this work's fine, but i need something else:

I need create a specified number of new row's in the template, but with styles (border, font, backcolor, etc.) and I don't how to make it.

Someone can help me with a example code for make this?

Thanks a lot and excuse my english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-09-17 15:08:02

Rastro,

您可以使用以下方法来执行此操作,

private UInt32Value createBorder(Stylesheet styleSheet,bool buttomBorderDouble)
        {
            Border border;
            //set borders of header
            if (buttomBorderDouble)
            {


  border = new Border
                (
                new BottomBorder { Style = BorderStyleValues.Double },
                new DiagonalBorder());
        }
        else
        {

             border = new Border
                (
                new BottomBorder {Style = BorderStyleValues.Thin},
                new DiagonalBorder());
        }




        styleSheet.Borders.Append(border);
        UInt32Value result = styleSheet.Borders.Count;
        styleSheet.Borders.Count++;
        return result;

    }
    private UInt32Value createFont(Stylesheet styleSheet, string fontName, Nullable<double> fontSize, bool isBold, System.Drawing.Color foreColor, bool isUnderLine)
    {

        Font font = new Font();

        if (!string.IsNullOrEmpty(fontName))
        {
            FontName name = new FontName()
            {
                Val = fontName
            };
            font.Append(name);
        }

        if (fontSize.HasValue)
        {
            FontSize size = new FontSize()
            {
                Val = fontSize.Value
            };
            font.Append(size);
        }

        if (isBold == true)
        {
            Bold bold = new Bold();
            font.Append(bold);
        }
        if (isUnderLine == true)
        {
            Underline underline = new Underline();
            font.Append(underline);
        }

        if (foreColor != null)
        {
            Color color = new Color()
            {
                Rgb = new HexBinaryValue()
                {
                    Value =
                        System.Drawing.ColorTranslator.ToHtml(
                            System.Drawing.Color.FromArgb(
                                foreColor.A,
                                foreColor.R,
                                foreColor.G,
                                foreColor.B)).Replace("#", "")
                }
            };
            font.Append(color);
        }
        styleSheet.Fonts.Append(font);
        UInt32Value result = styleSheet.Fonts.Count;
        styleSheet.Fonts.Count++;
        return result;
    }

private UInt32Value createCellFormat(Stylesheet styleSheet, UInt32Value fontIndex, UInt32Value fillIndex, UInt32Value numberFormatId, UInt32Value borderId)
        {
            CellFormat cellFormat = new CellFormat();

            if (fontIndex != null)
                cellFormat.FontId = fontIndex;

            if (fillIndex != null)
                cellFormat.FillId = fillIndex;

            if (numberFormatId != null)
            {
                cellFormat.NumberFormatId = numberFormatId;
                cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true);
            }
            if (borderId != null)
                cellFormat.BorderId = borderId;

            styleSheet.CellFormats.Append(cellFormat);

            UInt32Value result = styleSheet.CellFormats.Count;
            styleSheet.CellFormats.Count++;
            return result;
        }

以及调用这些方法的一段代码

Stylesheet styleSheet = workbook.WorkbookStylesPart.Stylesheet;

UInt32Value headerFontIndex =
                createFont(
                    styleSheet,
                    "MS Sans Seif",
                    10,
                    true,
                    System.Drawing.Color.Black, false);

 UInt32Value doubleBorderIndex = createBorder(styleSheet, true);

            UInt32Value headerStyleIndexWithDoubleBottomBorder =
               createCellFormat(
                   styleSheet,
                   headerFontIndex,
                   0,
                   null, doubleBorderIndex);
 Cell _Cell = createTextCell(1, 1, "Intercompany Reconciliation Summary", headerStyleIndexWithDoubleButtomBorder, null);

其输出是用粗体和双线编写Intercompany Reconciliation Summary

希望它对您有帮助

Thanks,
Mohammed Thabet Zaky
Software Developer
Cairo,Egypt

Rastro,

You can use the following method to do that

private UInt32Value createBorder(Stylesheet styleSheet,bool buttomBorderDouble)
        {
            Border border;
            //set borders of header
            if (buttomBorderDouble)
            {


  border = new Border
                (
                new BottomBorder { Style = BorderStyleValues.Double },
                new DiagonalBorder());
        }
        else
        {

             border = new Border
                (
                new BottomBorder {Style = BorderStyleValues.Thin},
                new DiagonalBorder());
        }




        styleSheet.Borders.Append(border);
        UInt32Value result = styleSheet.Borders.Count;
        styleSheet.Borders.Count++;
        return result;

    }
    private UInt32Value createFont(Stylesheet styleSheet, string fontName, Nullable<double> fontSize, bool isBold, System.Drawing.Color foreColor, bool isUnderLine)
    {

        Font font = new Font();

        if (!string.IsNullOrEmpty(fontName))
        {
            FontName name = new FontName()
            {
                Val = fontName
            };
            font.Append(name);
        }

        if (fontSize.HasValue)
        {
            FontSize size = new FontSize()
            {
                Val = fontSize.Value
            };
            font.Append(size);
        }

        if (isBold == true)
        {
            Bold bold = new Bold();
            font.Append(bold);
        }
        if (isUnderLine == true)
        {
            Underline underline = new Underline();
            font.Append(underline);
        }

        if (foreColor != null)
        {
            Color color = new Color()
            {
                Rgb = new HexBinaryValue()
                {
                    Value =
                        System.Drawing.ColorTranslator.ToHtml(
                            System.Drawing.Color.FromArgb(
                                foreColor.A,
                                foreColor.R,
                                foreColor.G,
                                foreColor.B)).Replace("#", "")
                }
            };
            font.Append(color);
        }
        styleSheet.Fonts.Append(font);
        UInt32Value result = styleSheet.Fonts.Count;
        styleSheet.Fonts.Count++;
        return result;
    }

private UInt32Value createCellFormat(Stylesheet styleSheet, UInt32Value fontIndex, UInt32Value fillIndex, UInt32Value numberFormatId, UInt32Value borderId)
        {
            CellFormat cellFormat = new CellFormat();

            if (fontIndex != null)
                cellFormat.FontId = fontIndex;

            if (fillIndex != null)
                cellFormat.FillId = fillIndex;

            if (numberFormatId != null)
            {
                cellFormat.NumberFormatId = numberFormatId;
                cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true);
            }
            if (borderId != null)
                cellFormat.BorderId = borderId;

            styleSheet.CellFormats.Append(cellFormat);

            UInt32Value result = styleSheet.CellFormats.Count;
            styleSheet.CellFormats.Count++;
            return result;
        }

and its a piece of code that call these methods

Stylesheet styleSheet = workbook.WorkbookStylesPart.Stylesheet;

UInt32Value headerFontIndex =
                createFont(
                    styleSheet,
                    "MS Sans Seif",
                    10,
                    true,
                    System.Drawing.Color.Black, false);

 UInt32Value doubleBorderIndex = createBorder(styleSheet, true);

            UInt32Value headerStyleIndexWithDoubleBottomBorder =
               createCellFormat(
                   styleSheet,
                   headerFontIndex,
                   0,
                   null, doubleBorderIndex);
 Cell _Cell = createTextCell(1, 1, "Intercompany Reconciliation Summary", headerStyleIndexWithDoubleButtomBorder, null);

the output of that is to write Intercompany Reconciliation Summary with bold and double lines under it

I hope it help you

Thanks,
Mohammed Thabet Zaky
Software Developer
Cairo,Egypt
不必了 2024-09-17 15:08:01

我遇到了同样的情况,没有找到更好的方法,但是在 Excel 文档中使用 vb 宏来添加新行并从 .net 调用它。

I had same situation and found nothing better, but use a vb macros in excel document which adds new row and invoke it from .net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文