`
妮子321
  • 浏览: 85396 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

PageOffice,word常用接口对象--Table类

阅读更多

在做项目的过程中,经常会遇到要把报表导出到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<Cell> 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的内容更加的丰富和完善。
分享到:
评论

相关推荐

    PageOffice 基础教程.pdf

    在Excel篇中,教程介绍了Workbook类、Sheet类、Table类和Cell类等常用接口对象,并指导如何动态填充Excel文档、实现单元格点击事件、用户自定义Excel模板、读取数据以及隐藏行号和列标的高级操作。 Java篇中,主要...

    java打印word的分享

    一个常用的库是Apache POI,它是一个强大的API,用于读写Microsoft Office格式的文件,包括Word文档。 在`testprint2.java`这个文件中,我们可以推测其内容可能包含了一个Java类或方法,用于演示如何使用Apache POI...

    北大青鸟软件课程全部单词

    - **office**:指微软Office套件,包括Word、Excel、PowerPoint等应用。 - **copy**:复制,将选定的内容复制到剪贴板上,以便粘贴到其他位置。 - **find**:查找,搜索文档中的特定文本或数据。 - **table**:表格...

    LotusDomino学习笔记.doc

    - **结合方式**: 通过COM接口允许Notes应用程序调用MS Office组件的功能,如创建Word文档或发送Excel表格数据。 #### 25. 读写关系数据库资料 - **实现方法**: 通过ODBC或JDBC接口连接到关系数据库,并使用...

    java学习之英文词汇

    9. **Office**: 办公室,也指微软Office套件,包括Word、Excel等多个办公应用软件。 10. **Copy**: 复制,将文件或数据的副本制作出来。 11. **Find**: 查找,在文档或文件夹中搜索特定的信息。 12. **Table**: 表格...

Global site tag (gtag.js) - Google Analytics