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

相关文章

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

Linux使用scp进行远程目录文件复制的详细步骤和示例

《Linux使用scp进行远程目录文件复制的详细步骤和示例》在Linux系统中,scp(安全复制协议)是一个使用SSH(安全外壳协议)进行文件和目录安全传输的命令,它允许在远程主机之间复制文件和目录,... 目录1. 什么是scp?2. 语法3. 示例示例 1: 复制本地目录到远程主机示例 2: 复制远程主

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

MySQL的ALTER TABLE命令的使用解读

《MySQL的ALTERTABLE命令的使用解读》:本文主要介绍MySQL的ALTERTABLE命令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、查看所建表的编China编程码格式2、修改表的编码格式3、修改列队数据类型4、添加列5、修改列的位置5.1、把列

Python实现一键PDF转Word(附完整代码及详细步骤)

《Python实现一键PDF转Word(附完整代码及详细步骤)》pdf2docx是一个基于Python的第三方库,专门用于将PDF文件转换为可编辑的Word文档,下面我们就来看看如何通过pdf2doc... 目录引言:为什么需要PDF转Word一、pdf2docx介绍1. pdf2docx 是什么2. by

如何Python使用设置word的页边距

《如何Python使用设置word的页边距》在编写或处理Word文档的过程中,页边距是一个不可忽视的排版要素,本文将介绍如何使用Python设置Word文档中各个节的页边距,需要的可以参考下... 目录操作步骤代码示例页边距单位说明应用场景与高级用China编程途小结在编写或处理Word文档的过程中,页边距是一个

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>