博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PageOffice,word常用接口对象--Table类
阅读量:7153 次
发布时间:2019-06-29

本文共 6164 字,大约阅读时间需要 20 分钟。

hot3.png

在做项目的过程中,经常会遇到要把报表导出到Word文件中再打印的情况,而既然是做报表那就免不了要用到表格,即需要在Word文件中插入table。因此,PageOffice开发平台中就添加了此功能。

Table类的命名空间

Table类进行写入操作时

  • Java开发时命名空间为:com.zhuozhengsoft.pageoffice.wordwriter
  • ASP.NET开发时命名空间为:PageOffice.WordWriter

Table类进行读取操作时

  • Java开发时命名空间为:com.zhuozhengsoft.pageoffice.wordreader
  • ASP.NET开发时命名空间为:PageOffice.WordReader

Table类的使用

Word中的table是要借助数据区域(DataRegion)实现的,要求数据区域完整的包含了整个Table的内容,这样才可以通过数据区域控制和操作table。因此,要想使用table需在word文件中插入书签(数据区域及书签的添加使用与实现方法前面已经详细介绍过了此处不再赘述)。而table的插入,既可以在Word模版中书签处手动插入:工具栏“插入”→“表格”,亦可以在程序中通过数据区域添加。

同时,Table既可以对齐进行设置和赋值又可以从Table中取值

  • 对Table进行设置和赋值(写入操作)的方法如下:

方法一:先在Word模版文件中手动插入书签,如命名为“PO_regTable”,然后在此处手动插入table。

Java部分代码如下:

WordDocument doc = new WordDocument();	DataRegion dataRegion = doc.openDataRegion("PO_regTable");	//打开table,openTable(index)方法中的index代表Word文档中要打开的table位置的索引,从1开始	Table table = dataRegion.openTable(1);	//给table中的单元格赋值, openCellRC(int,int)中的参数分别代表第几行、第几列,下标从1开始	table.openCellRC(3, 1).setValue("A公司");	table.openCellRC(3, 2).setValue("开发部");	table.openCellRC(3, 3).setValue("李四");		//在某个个单元格下面插入一个空行	table.insertRowAfter(table.openCellRC(3, 3));		table.openCellRC(4, 1).setValue("B公司");	table.openCellRC(4, 2).setValue("销售部");	table.openCellRC(4, 3).setValue("张三");		poCtrl1.setWriter(doc);//不要忘记此句    ... ...

ASP.NET部分实现代码如下

PageOffice.WordWriter.WordDocument doc = new PageOffice.WordWriter.WordDocument();    PageOffice.WordWriter.DataRegion dataRegion = doc.OpenDataRegion("PO_regTable");    PageOffice.WordWriter.Table table = dataRegion.OpenTable(1);            table.OpenCellRC(3, 1).Value = "A公司";    table.OpenCellRC(3, 2).Value = "开发部";    table.OpenCellRC(3, 3).Value = "李清";            table.InsertRowAfter(table.OpenCellRC(3, 3)); //插入一行     table.OpenCellRC(4, 1).Value = "B公司";    table.OpenCellRC(4, 2).Value = "销售部";    table.OpenCellRC(4, 3).Value = "张三";    PageOfficeCtrl1.SetWriter(doc);//不要忘记此行	... ...

上述示例的详细代码可参见PageOffice示例包中高级功能中的第14个示例:“向Word文档中的Table插入新行并赋值(专业版、企业版)”。

方法二:先在Word模版文件中手动插入书签,如命名为“PO_regTable”,然后程序插入table。部分实现代码如下:

Java代码(命名空间同方法一中相同):

WordDocument doc = new WordDocument();	DataRegion dataRegion = doc.openDataRegion("PO_regTable");    	//插入table,createTable方法中的三个参数分别代表插入表格的列数、行数、自动调整表格大小的方式。    Table table=dataRegion.createTable(3,3,WdAutoFitBehavior.wdAutoFitFixed); 	table.openCellRC(3, 1).setValue("A公司");	table.openCellRC(3, 2).setValue("开发部");	table.openCellRC(3, 3).setValue("李清");	... ...	poCtrl1.setWriter(doc);//不要忘记此句	... ...

ASP.NET实现代码如下(命名空间同方法一中相同):

PageOffice.WordWriter.WordDocument doc = new PageOffice.WordWriter.WordDocument();    PageOffice.WordWriter.DataRegion dataRegion = doc.OpenDataRegion("PO_regTable");        //插入一个4行3列的表格    PageOffice.WordWriter.Table table = dataRegion.CreateTable(3, 4,                                          PageOffice.WordWriter.WdAutoFitBehavior.wdAutoFitFixed);     table.OpenCellRC(3, 1).Value = "A公司";    table.OpenCellRC(3, 2).Value = "开发部";    table.OpenCellRC(3, 3).Value = "李清";    ... ...    PageOfficeCtrl1.SetWriter(doc);//不要忘记此行    ... ...

以上是实现对Table的设置和赋值。

在PageOffice中不仅能对Table进行设置和赋值,还能从Table中取值。一般来说不推荐使用从Word表格中获取数据,因为实现此功能时,PageOffice并不能保护Table的表格结构,如果用户操作时破坏了表格的结构,那么获取表格数据的程序肯定会出现异常。

  • 从Table中取值(读取操作)方法如下:

Java代码:

WordDocument doc = new WordDocument(request,response); //注意Java中这句和设置Table时的不同	DataRegion dataRegion = doc.openDataRegion("PO_regTable"); 	Table table = dataRegion.openTable(1); //获取Table,参数为table的索引	Cell cell = table.openCellRC(2,3); //获取某个Cell对象,参数分别指table中的行和列索引	List
cells = table.getCells(); //获取Cell对象集合 Cell cell2 = (Cell)table.getCells().get(1); //获取Cell对象集合中的某个Cell对象 int columnCount = table.getColumnsCount(); //获取表格的列数 int rowCount = table.getRowsCount(); //获取表格的行数 int index = table.getIndex(); //Word中当前Table的索引 ... ...

ASP.NET代码:

WordDocument doc = new WordDocument();    PageOffice.WordReader.DataRegion dataRegion = doc.OpenDataRegion("PO_regTable");    PageOffice.WordReader.Table table = dataRegion.OpenTable(1); //获取Table,参数为table的索引    Cell cell = table.OpenCellRC(3,2); //获取某个Cell对象,参数分别指table中的行和列索引    ArrayList cells = table.Cells; //获取Cell对象集合    Cell cell2 = (Cell)table.Cells[2]; //获取Cell对象集合中的某个Cell对象    ... ...
  • Table的样式

在PageOffice开发平台下不仅能向Word中插入table,还能对插入的table进行样式的设置(写入操作)。

①、设置table的宽度,以磅为单位或以窗口宽度的百分比表示,取决于属性PreferredWidthType( wdPreferredWidthAuto: 基于当前所选内容自动选择要使用的度量单位、wdPreferredWidthPercent:使用指定的百分比测量当前项目的宽度、wdPreferredWidthPoints:使用指定的磅数测量当前项目的宽度)的值。

Java代码:

table.setPreferredWidth(0.8f);//参数类型:float	table.setPreferredWidthType(WdPreferredWidthType.wdPreferredWidthPercent);

ASP.NET代码:

table.PreferredWidth = 200f;//参数类型:float    table.PreferredWidthType= PageOffice.WordWriter.WdPreferredWidthType.wdPreferredWidthPoints;

②、设置行高、列宽

Java代码:

table.setRowsHeight(20f);// 设置所有行的行高    //设置某一行的行高,注意第二个参数对table的行高显示结果的影响,详细请参考服务器端的开发帮助手册。	table.openRow(2).setHeight(10f,WdRowHeightRule.wdRowHeightExactly);    //设置某一列的列宽,注意第二个参数对table的列宽显示结果的影响,详细请参考服务器端的开发帮助手册。	table.openColumn(1).setWidth(25f, WdRulerStyle.wdAdjustNone);

ASP.NET代码:

table.SetRowsHeight(60f, PageOffice.WordWriter.WdRowHeightRule.wdRowHeightExactly);    //设置某一行的行高,注意第二个参数对table的行高显示结果的影响,详细请参考服务器端的开发帮助手册。	table.OpenRow(2).SetHeight(100f,PageOffice.WordWriter.WdRowHeightRule.wdRowHeightExactly);    //设置某一列的列宽,注意第二个参数对table的列宽显示结果的影响,详细请参考服务器端的开发帮助手册。    table.OpenColumn(2).SetWidth(180f, PageOffice.WordWriter.WdRulerStyle.wdAdjustProportional);

③、设置边框的样式

Java代码:

table.getBorder().setBorderType(WdBorderType.wdAllEdges); //边框类型	table.getBorder().setLineColor(Color.RED); //边框颜色	table.getBorder().setLineStyle(WdLineStyle.wdLineStyleDashDot); //边框线样式	table.getBorder().setLineWidth(WdLineWidth.wdLineWidth225pt); //边框线宽度

ASP.NET代码:

table.Border.BorderType = PageOffice.WordWriter.WdBorderType.wdAllEdges; //边框类型    table.Border.LineColor = Color.Green; //边框颜色    table.Border.LineStyle = PageOffice.WordWriter.WdLineStyle.wdLineStyleDashDotStroked;//边线样式    table.Border.LineWidth = PageOffice.WordWriter.WdLineWidth.wdLineWidth225pt; //边框线宽度
  • Table、Column、Row、Cell之间的关联
    Column、Row、Cell是PageOffice开发平台中的类,这三者是Table的重要组成部分。Column、Row、Cell这三个类对象的定义都是通过Table类实现的,通过对这三个类对象的设置可以使Table的内容更加的丰富和完善。

转载于:https://my.oschina.net/u/3850288/blog/2046301

你可能感兴趣的文章
文本占用的高度和范围
查看>>
6.彻底搞懂javascript-作用域链
查看>>
MVVM架构:LiveData,ViewModel,kotlin,kotlin协程,DataBinding等
查看>>
【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)
查看>>
vue part3.2 小案例 todo 列表展示删除
查看>>
【CodeForces】915 E. Physical Education Lessons 线段树
查看>>
Vue 源码剖析 —— 数组变化侦测
查看>>
6月14日奋战es5基础-1
查看>>
Socket IO与NIO(六)
查看>>
一图胜千言,8张图理解Java
查看>>
[算法]动态规划之最长递增子序列
查看>>
好程序员告诉你Java架构师学习路线
查看>>
Redis之主从集群环境搭建
查看>>
tab切换小例子
查看>>
Java封装
查看>>
【快学springboot】9.使用 @Transactional 注解配置事务管理
查看>>
匿名对象方案与实体对象方案对比
查看>>
NTP服务放大攻击的解决办法
查看>>
SQL SERVER 占用资源高的SQL语句
查看>>
verdi使用
查看>>