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

Aspose.words关于builder.CellFormat.Width、row.Cells[0].CellFormat.Width的设置单元格宽度区别

row.Cells[0].CellFormat.Width():统一使用遍历每行单元格来设置对应的单元格宽度

#region 宽度赋值
foreach (Aspose.Words.Tables.Row row in tableNow.Rows)
{row.Cells[0].CellFormat.Width = 80;row.Cells[1].CellFormat.Width = 60;row.Cells[2].CellFormat.Width = 70;row.Cells[3].CellFormat.Width = 105;row.Cells[4].CellFormat.Width = 70;row.Cells[5].CellFormat.Width = 70;row.Cells[6].CellFormat.Width = 115;
}
#endregion

优点就是比较集中,方便设置修改。但是在表格构造完后会出现问题就是无论怎么调整width的数值,表格的整体长度是不变的,只是单元格比例的变化(设置数值占数值总和的比例)

builder.CellFormat.Width():在创建单元格时设置宽度

builder.InsertCell();
builder.CellFormat.Width = 80;
builder.Write("检测项目");
builder.InsertCell();
builder.CellFormat.Width = 60;
builder.Write("单位");
builder.InsertCell();
builder.CellFormat.Width = 70;
builder.Write("检测结果");
builder.InsertCell();
builder.CellFormat.Width = 105;
builder.Write("检测依据");
builder.InsertCell();
builder.CellFormat.Width = 70;
builder.Write("标准要求");
builder.InsertCell();
builder.CellFormat.Width = 70;
builder.Write("单项判定");
builder.InsertCell();
builder.CellFormat.Width = 115;
builder.Write("判断依据");
builder.EndRow();

这样设置的单元格宽度才会随数值变化而变化,但是就是不够统一,不美观也不方便修改。

row.Cells[0].CellFormat.Width()为什么会出现这种问题?

在Aspose.Words中,表格的布局行为取决于:

  1. 表格的自动调整设置Table.AllowAutoFit

  2. 设置宽度的时机 - 在创建时设置 vs 创建后设置

  3. 表格的布局算法 - 默认会按比例分配剩余空间

为什么builder.InsertCell()能成功?

因为在使用DocumentBuilder创建单元格时:

  • 宽度设置是立即生效

  • 表格布局算法会尊重创建时的设置

  • 没有后续的自动调整干扰

所以想要构建表后使用row.Cells[0].CellFormat.Width()来设置单元格宽度有两种方法:

方案1:禁用表格自动调整(推荐)
// 在设置宽度之前,禁用表格的自动调整
tableNow.AllowAutoFit = false;#region 宽度赋值
foreach (Aspose.Words.Tables.Row row in tableNow.Rows)
{row.Cells[0].CellFormat.Width = 80;row.Cells[1].CellFormat.Width = 60;row.Cells[2].CellFormat.Width = 70;row.Cells[3].CellFormat.Width = 105;row.Cells[4].CellFormat.Width = 70;row.Cells[5].CellFormat.Width = 70;row.Cells[6].CellFormat.Width = 115;
}
#endregion
方案2:设置表格首选宽度
//设置表格的首选宽度
tableNow.PreferredWidth = PreferredWidth.FromPoints(570); // 首选宽度,根据总宽度调整,没有设置就会导致只会按下面比例生成不可控的宽度#region 宽度赋值
foreach (Aspose.Words.Tables.Row row in tableNow.Rows)
{row.Cells[0].CellFormat.Width = 80;row.Cells[1].CellFormat.Width = 60;row.Cells[2].CellFormat.Width = 70;row.Cells[3].CellFormat.Width = 105;row.Cells[4].CellFormat.Width = 70;row.Cells[5].CellFormat.Width = 70;row.Cells[6].CellFormat.Width = 115;
}
#endregion

结果如图:

http://www.dtcms.com/a/512004.html

相关文章:

  • 罗湖网站建设的公司哪家好阳泉做网站公司
  • 口碑好的共晶贴片机公司
  • 挑战概率直觉:蒙提霍尔问题的解密与应用
  • 网站域名哪些后缀更好给自己公司做个网站
  • 算法笔记 07
  • Steps + Input.TextArea + InfiniteScroll 联调优化
  • /dev/mem 原理及使用
  • 机关网站建设 方案泰安新闻完整版
  • Endpoint
  • 阿里巴巴双11微服务智能监控体系:从全链路追踪到AI自愈的技术实践
  • 在ros2 humble版本上安装D455相机并获取图像和深度信息
  • C++DirectX9坐标系与基本图元之渲染状态(RenderState)_0304
  • 网站建设app长春seo技术
  • 【C++】力扣hot100错误总结
  • C++中的vector讲解
  • 笔记【字符串,转义字符,注释】
  • visual studio安装本地帮助手册
  • 北京市基础建设质量监督局网站wordpress 插件怎么看
  • 大模型技术分析与演进逻辑
  • 苏州模板网站建站长沙网站建设推广
  • 从零起步学习MySQL || 第六章:MySQL数据库中的一条数据是如何存储的?(结合源码深度解析)
  • 微信小程序页面配置,基本语法,页面切换,tabbar全局配置
  • 数据结构 07
  • 18.基本的ACL
  • 网站后台编程语言创业中文网站模板
  • 从“刘易斯拐点”到“骑手拐点”,即时零售3.0时代还有多远?
  • 有没有一种app类似网站建设开发定制软件开发
  • 沈阳网站建设建设公司普洱网站建设
  • 蓝桥杯题库——部分简单题题解(Java)
  • 新民电商网站建设程序wordpress淘宝发货插件