当前位置: 首页 > news >正文

使用aspose.Words更新表格列宽

public static bool UpdateTableStyle(string filePath)

        {

            try

            {

                Document doc = new Document();

                //样式

                Style style = doc.Styles.Add(StyleType.Paragraph, "cellStyle");

                style.Font.Name = "simsun";

                PageSetup pageSet = doc.Sections[0].PageSetup;

                pageSet.PaperSize = PaperSize.A4;

                pageSet.LeftMargin = ConvertUtil.MillimeterToPoint(5);

                pageSet.RightMargin = ConvertUtil.MillimeterToPoint(5);

                //pageSet.PageStartingNumber = 1;

                //pageSet.PageNumberStyle = NumberStyle.Arabic;

                DocumentBuilder builder = new DocumentBuilder(doc);

                builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

                //builder.InsertField("PAGE", "");

                //builder.Write("/");

                //builder.InsertField("NUMPAGES", "");

                double cellWidth = 18;

                double portraitMinWidth = ConvertUtil.MillimeterToPoint(cellWidth);//纵向最小列宽

                double pageWidth = 200d;//A4纵向可编辑区域宽度200毫米//A4横向向可编辑区域宽度290毫米

                List<Document> docItemList = new List<Document>();

                bool IsLandscape = false;//是否为横向

                //先计算是使用横向还是纵向的A4

                if (File.Exists(filePath))

                {

                    Document docItem = new Document(filePath);

                    docItemList.Add(docItem);

                    var tableList = docItem.GetChildNodes(NodeType.Table, true);

                    bool isExist = false;

                    foreach (Table table in tableList)

                    {

                        if(table.Range != null && table.Range.Bookmarks != null)

                        {

                            foreach (Bookmark bkName in table.Range.Bookmarks)

                            {

                                if (bkName.Name == aa)

                                {

                                    isExist = true;

                                    break;

                                }

                            }

                        }

                        if (isExist)

                        {

                            //先计算是否符合纵向A4纸要求的列宽

                            double width = ConvertUtil.MillimeterToPoint(200d / table.Rows[2].Cells.Count);

                            if (width < portraitMinWidth)

                            {

                                portraitMinWidth = width;

                            }

                        }

                    }

                    //不包含指标书签时,退出

                    if (!isExist)

                        return false;

                }

                //如果列宽小于一定宽度那么就将文档变为横向cellWidth

                if (portraitMinWidth < ConvertUtil.MillimeterToPoint(cellWidth))

                {

                    IsLandscape = true;

                    pageWidth = 150d;

                }

                else

                {

                    IsLandscape = false;

                    pageWidth = 120d;

                }

                int i = 1;

                foreach (Document docItem in docItemList)

                {

                    builder.MoveToDocumentEnd();

                    // Document docItem = new Document(path);

                    PageSetup pageSet1 = docItem.Sections[0].PageSetup;

                    pageSet1.PaperSize = PaperSize.A4;

                    pageSet1.LeftMargin = ConvertUtil.MillimeterToPoint(20);

                    pageSet1.RightMargin = ConvertUtil.MillimeterToPoint(20);

                    var tableList = docItem.GetChildNodes(NodeType.Table, true);

                    foreach (Table table in tableList)

                    {

                        table.AutoFit(AutoFitBehavior.FixedColumnWidths);

                        foreach (Row row in table.Rows)

                        {

                            if (table.Range != null && table.Range.Bookmarks != null)

                            {

                                foreach (Bookmark bkName in table.Range.Bookmarks)

                                {

                                    if (bkName.Name == aa)

                                    {

                                        for (int j = 0; j < row.Cells.Count; j++)

                                        {

                                            row.Cells[j].CellFormat.WrapText = true;

                                            double width = ConvertUtil.MillimeterToPoint((pageWidth) / row.Cells.Count);

                                            row.Cells[j].CellFormat.Width = width;

                                        }

                                        break;

                                    }

                                }

                            }

                        }

                    }

                    docItem.FirstSection.HeadersFooters.LinkToPrevious(true);

                    builder.InsertDocument(docItem, ImportFormatMode.UseDestinationStyles);

                    i++;

                }

                if (IsLandscape)

                {

                    //文档变为横向

                    pageSet.Orientation = Aspose.Words.Orientation.Landscape;

                }

                //重新整理页面布局

                doc.UpdatePageLayout();

                doc.Save(filePath);

                return true;

            }

            catch (Exception ex)

            {

                LogHelper.WriteLog("更新表格列宽异常,", ex);

                return false;

            }

        }

http://www.lryc.cn/news/268328.html

相关文章:

  • pycharm 工具栏不见了
  • 图灵日记之java奇妙历险记--类和对象
  • Kotlin 枚举类
  • 可运营的Leadshop开源商城小程序源码 +H5公众号+带视频教程
  • Qt底层机制之对象树总结
  • QT C++ TCP Socket 请求心知天气
  • 双向链表的实现及头尾插入删除
  • C语言—每日选择题—Day62
  • 基于 Sentry 的前端监控系统搭建(Linux)
  • 【C++入门到精通】Lock_guard与Unique_lock C++11 [ C++入门 ]
  • 电路设计(8)——计时器的multism仿真
  • Jmeter测试实践:文件下载接口
  • PyQt5实现学生管理系统第三天(下)
  • 第4章 | 安徽某高校《统计建模与R软件》期末复习
  • localforage本地存储(融合Web Storage,Web SQL Database,ndexedDB三种前端存储)
  • 【JavaWeb学习笔记】17 - ThreadLocal
  • 【ARMv8M Cortex-M33 系列 1 -- SAU 介绍】
  • sklearn 逻辑回归Demo
  • 什么是众创空间?他有什么特点?
  • 什么是数据分析思维
  • 利用Milvus Cloud和LangChain构建机器人:一种引人入胜且通俗易懂的方法
  • 数据结构-如何实现一个队列?逐步解析与代码示例(超详细)
  • 爬虫工作量由小到大的思维转变---<第二十三章 Scrapy开始很快,越来越慢(医病篇)>
  • .Net7.0 或更高版本 System.Drawing.Common 上传图片跨平台方案
  • 【MySQL】InnoDB和MyISAM区别
  • 3分钟了解安全数据交换系统有什么用!
  • 记录汇川:MODBUS TCP-梯形图
  • electron + sqlite3 解决打包后无法写入数据库
  • 【uniapp小程序-生成二维码+多个图片文字合并一张图】
  • Text-to-SQL小白入门(十)RLHF在Text2SQL领域的探索实践