openxml 给word添加table,复制样式和字体

2024-06-01 01:08

本文主要是介绍openxml 给word添加table,复制样式和字体,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 // 设置表格边框
                                TableProperties tblProp = new TableProperties(
                                new TableBorders(
                                new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
                                new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
                                new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
                                new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
                                new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
                                new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 }
                                )
                                );

                                Table table = new Table();
                                TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };
                                table.Append(tblProp);
                                string a = "100", b = "2000", c = "200", d = "2500";
                                for (int i = 0; i < costList.Length; i++)
                                {

                                    if (costList[i] != "")
                                    {
                                        
                                        RunFonts rf = r.RunProperties.RunFonts;
                                        //DocumentFormat.OpenXml

                                        DocumentFormat.OpenXml.Wordprocessing.FontSize fs = r.RunProperties.FontSize;

                                        TableRow row = new TableRow();

                                        TableCell cell = new TableCell();
                                        cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = a } };
                                        // cell.TableCellProperties = new TableCellProperties() { TableCellMargin = new TableCellMargin() { RightMargin = new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = "1000" } } };
                                        cell.Append(new Paragraph(new Run(new Text(""))));
                                        row.Append(cell);
                                        cell = new TableCell();
                                        cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = b } };
                                        //cell.TableCellProperties = new TableCellProperties() { TableCellMargin = new TableCellMargin() { RightMargin = new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = "2000" } } };
                                        Run rn = new Run(new Text(“”));
                                        rn.RunProperties = new RunProperties();
                                        rn.RunProperties.RunFonts = new RunFonts { Ascii = rf.Ascii };
                                        rn.RunProperties.FontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
                                        rn.RunProperties.FontSize.Val = fs.Val;
                                        cell.Append(new Paragraph(new Run(rn)));
                                        row.Append(cell);
                                        cell = new TableCell();

                                        //设置cell的宽度
                                        cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = c } };
                                        // cell.TableCellProperties = new TableCellProperties() { TableCellMargin = new TableCellMargin() { RightMargin = new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = "1000" } } };
                                        rn = new Run(new Text(":"));
                                        cell.Append(new Paragraph(new Run(rn)));
                                        row.Append(cell);
                                        cell = new TableCell();
                                        //cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "100" } };
                                        cell.TableCellProperties = new TableCellProperties() { TableCellMargin = new TableCellMargin() { RightMargin = new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = a } } };
                                        rn = new Run(new Text("$"));
                                        if ( i == costList.Length - 1)
                                        {

                                            //设置cell双下划线,单下划线为:Signle
                                            cell.TableCellProperties.TableCellBorders = new TableCellBorders() { BottomBorder = new BottomBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Double } };
                                        }
                                        cell.Append(new Paragraph(new Run(rn)));
                                        row.Append(cell);

                                        cell = new TableCell();
                                        cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = d } };

                                        rn = new Run(new Text(amt));
                                        rn.RunProperties = new RunProperties();
                                        if (i == costList.Length - 1)
                                        {
                                            //给table的cell 添加下划线
                                            //cell.TableCellProperties = new TableCellProperties() { TableCellBorders = new TableCellBorders() { BottomBorder = new BottomBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Single } } };
                                            cell.TableCellProperties.TableCellBorders = new TableCellBorders() { BottomBorder = new BottomBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Double } };
                                        }
                                        //cell.TableCellProperties.TableCellMargin = new TableCellMargin();
                                        //cell.TableCellProperties.TableCellMargin.RightMargin = new RightMargin();
                                        //内容靠右
                                        ParagraphProperties paragraphProperties = new ParagraphProperties();
                                        paragraphProperties.AppendChild<Justification>(new Justification() { Val = JustificationValues.Right });
                                        cell.TableCellProperties.AppendChild<ParagraphProperties>(paragraphProperties);

                                       //内容加粗

                                        rn.RunProperties.Bold = new Bold { Val = new OnOffValue() { Value = true } };

                                        //字体
                                        rn.RunProperties.RunFonts = new RunFonts { Ascii = rf.Ascii };
                                        rn.RunProperties.FontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
                                        rn.RunProperties.FontSize.Val = fs.Val;
                                        cell.Append(new Paragraph(rn));
                                        row.Append(cell);
                                        table.Append(row);

                                    }
                                }

                                //Table banktable = new Table();

                                //TableRow bankRow = new TableRow();
                                //TableCell bankCell = new TableCell();
                                //bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = a } };
                                //bankCell.Append(new Paragraph(new Run(new Text(""))));
                                //bankRow.Append(bankCell);
                                //bankCell = new TableCell();
                                //bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = b } };
                                //bankCell.Append(new Paragraph(new Run(new Text(""))));
                                //bankRow.Append(bankCell);
                                //bankCell = new TableCell();
                                //bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = c } };
                                //bankCell.Append(new Paragraph(new Run(new Text(""))));
                                //bankCell.TableCellProperties.TableCellBorders = new TableCellBorders() { TopBorder = new TopBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Single } };
                                //bankRow.Append(bankCell);
                                //bankCell = new TableCell();
                                //bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = a } };
                                //bankCell.Append(new Paragraph(new Run(new Text(""))));
                                //bankCell.TableCellProperties.TableCellBorders = new TableCellBorders() { TopBorder = new TopBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Single } };
                                //bankRow.Append(bankCell);
                                //bankCell = new TableCell();
                                //bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = d } };
                                //bankCell.Append(new Paragraph(new Run(new Text(""))));
                                //bankCell.TableCellProperties.TableCellBorders = new TableCellBorders() { BottomBorder = new BottomBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Single } };
                                //TableRowHeight trh = new TableRowHeight();
                                //trh.Val = 1;
                                //bankRow.Append(trh);
                                //bankRow.Append(bankCell);
                                //banktable.Append(bankRow);
                                //
                                //table.Append(banktable);
                                /
                                r.Elements<Text>().FirstOrDefault().Text = "";
                                r.AppendChild(new Paragraph(new Run(table)));

这篇关于openxml 给word添加table,复制样式和字体的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1019563

相关文章

linux如何复制文件夹并重命名

《linux如何复制文件夹并重命名》在Linux系统中,复制文件夹并重命名可以通过使用“cp”和“mv”命令来实现,使用“cp-r”命令可以递归复制整个文件夹及其子文件夹和文件,而使用“mv”命令可以... 目录linux复制文件夹并重命名我们需要使用“cp”命令来复制文件夹我们还可以结合使用“mv”命令总

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Java使用POI-TL和JFreeChart动态生成Word报告

《Java使用POI-TL和JFreeChart动态生成Word报告》本文介绍了使用POI-TL和JFreeChart生成包含动态数据和图表的Word报告的方法,并分享了实际开发中的踩坑经验,通过代码... 目录前言一、需求背景二、方案分析三、 POI-TL + JFreeChart 实现3.1 Maven

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

电脑没有仿宋GB2312字体怎么办? 仿宋GB2312字体下载安装及调出来的教程

《电脑没有仿宋GB2312字体怎么办?仿宋GB2312字体下载安装及调出来的教程》仿宋字体gb2312作为一种经典且常用的字体,广泛应用于各种场合,如何在计算机中调出仿宋字体gb2312?本文将为您... 仿宋_GB2312是公文标准字体之一,仿China编程宋是字体名称,GB2312是字php符编码标准名称(简

使用Python实现在Word中添加或删除超链接

《使用Python实现在Word中添加或删除超链接》在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能,本文将为大家介绍一下Python如何实现在Word中添加或... 在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一