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

相关文章

禁止复制的网页怎么复制

禁止复制的网页怎么复制 文章目录 禁止复制的网页怎么复制前言准备工作操作步骤一、在浏览器菜单中找到“开发者工具”二、点击“检查元素(inspect element)”按钮三、在网页中选取需要的片段,锁定对应的元素四、复制被选中的元素五、粘贴到记事本,以`.html`为后缀命名六、打开`xxx.html`,优雅地复制 前言 在浏览网页的时候,有的网页内容无法复制。比如「360

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

解决Office Word不能切换中文输入

我们在使用WORD的时可能会经常碰到WORD中无法输入中文的情况。因为,虽然我们安装了搜狗输入法,但是到我们在WORD中使用搜狗的输入法的切换中英文的按键的时候会发现根本没有效果,无法将输入法切换成中文的。下面我就介绍一下如何在WORD中把搜狗输入法切换到中文。

vue2实践:el-table实现由用户自己控制行数的动态表格

需求 项目中需要提供一个动态表单,如图: 当我点击添加时,便添加一行;点击右边的删除时,便删除这一行。 至少要有一行数据,但是没有上限。 思路 这种每一行的数据固定,但是不定行数的,很容易想到使用el-table来实现,它可以循环读取:data所绑定的数组,来生成行数据,不同的是: 1、table里面的每一个cell,需要放置一个input来支持用户编辑。 2、最后一列放置两个b

Excel和Word日常使用记录:

Excel使用总结 表格颜色填充: 合并单元格: 选中你要合并的单元格区域。按下快捷键 Alt + H,然后松开这些键。再按下 M,接着按 C。这个组合键执行的操作是:Alt + H:打开“主页”选项卡。M:选择“合并单元格”选项。C:执行“合并并居中”操作。 插入行: 在Excel中,插入一行的快捷键是:Windows:选择整行(可以点击行号)。按下 Ctrl + Sh

word转PDF后mathtype公式乱码以及图片分辨率降低等一系列问题|完美解决

word转PDF后mathtype公式乱码以及图片分辨率降低等一系列问题|完美解决 问题描述 最近在投一篇期刊论文,直接提交word文档,当时没有查看提交预览,一审审稿意见全是:公式乱码、公式乱码、乱码啊!!!是我大意了,第二次提交,我就决定将word文档转成PDF后再提交,避免再次出现公式乱码的问题。接着问题又来了,我利用‘文件/导出’或‘文件/另存为’的方式将word转成PDF后,发现公式

【信创建设】信息系统信创建设整体技方案(word原件完整版)

信创,即“信息技术应用创新”。我国自主信息产业聚焦信息技术应用创新,旨在通过对IT硬件、软件等各个环节的重构,基于我国自有IT底层架构和标准,形成自有开放生态,从根本上解决本质安全问题,实现信息技术可掌控、可研究、可发展、可生产。信创发展是一项国家战略,也是当今形势下国家经济发展的新功能。信创产业发展已经成为各行各业数字化转型、提升产业链发展的关键。 软件全套资料部分文档清单: 工作安排任

在项目开发中,jsp页面不会少了,如何公用页面(添加页面和修改页面)和公用样式代码(css,js)?

在项目开发中,如何公用添加页面和修改页面? <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html><head><title>岗位设置</title><%@ include file="/WEB-INF/jsp/public/common.jspf"%></head><body> <!-- 标

通过Ajax请求后台数据,返回JSONArray(JsonObject),页面(Jquery)以table的形式展示

点击“会商人员情况表”,弹出层,显示一个表格,如下图: 利用Ajax和Jquery和JSONArray和JsonObject来实现: 代码如下: 在hspersons.html中: <!DOCTYPE html><html><head><meta charset="UTF-8"><title>会商人员情况表</title><script type="text/javasc

node快速复制文件或文件夹,排除部分文件(node_modules)

const fs = require('fs')const path = require('path')/*** @description: 获取完整的文件路径* @param {*} url 路径* @return {*} 返回完整的文件路径*/const getPath = (url) => {return path.join(__dirname, url)}/*** @descr