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

相关文章

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

GORM中Model和Table的区别及使用

《GORM中Model和Table的区别及使用》Model和Table是两种与数据库表交互的核心方法,但它们的用途和行为存在著差异,本文主要介绍了GORM中Model和Table的区别及使用,具有一... 目录1. Model 的作用与特点1.1 核心用途1.2 行为特点1.3 示例China编程代码2. Tab

Java利用poi实现word表格转excel

《Java利用poi实现word表格转excel》这篇文章主要为大家详细介绍了Java如何利用poi实现word表格转excel,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、每行对象类需要针对不同的表格进行对应的创建。package org.example.wordToEx

Python如何在Word中生成多种不同类型的图表

《Python如何在Word中生成多种不同类型的图表》Word文档中插入图表不仅能直观呈现数据,还能提升文档的可读性和专业性,本文将介绍如何使用Python在Word文档中创建和自定义各种图表,需要的... 目录在Word中创建柱形图在Word中创建条形图在Word中创建折线图在Word中创建饼图在Word

Python批量调整Word文档中的字体、段落间距及格式

《Python批量调整Word文档中的字体、段落间距及格式》这篇文章主要为大家详细介绍了如何使用Python的docx库来批量处理Word文档,包括设置首行缩进、字体、字号、行间距、段落对齐方式等,需... 目录关键代码一级标题设置  正文设置完整代码运行结果最近关于批处理格式的问题我查了很多资料,但是都没

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

通过Python脚本批量复制并规范命名视频文件

《通过Python脚本批量复制并规范命名视频文件》本文介绍了如何通过Python脚本批量复制并规范命名视频文件,实现自动补齐数字编号、保留原始文件、智能识别有效文件等功能,听过代码示例介绍的非常详细,... 目录一、问题场景:杂乱的视频文件名二、完整解决方案三、关键技术解析1. 智能路径处理2. 精准文件名

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

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

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

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