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

在作项目的过程当中,常常会遇到要把报表导出到Word文件中再打印的状况,而既然是作报表那就免不了要用到表格,即须要在Word文件中插入table。所以,PageOffice开发平台中就添加了此功能。服务器

Table类的命名空间

Table类进行写入操做时工具

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

Table类进行读取操做时spa

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

Table类的使用

Word中的table是要借助数据区域(DataRegion)实现的,要求数据区域完整的包含了整个Table的内容,这样才能够经过数据区域控制和操做table。所以,要想使用table需在word文件中插入书签(数据区域及书签的添加使用与实现方法前面已经详细介绍过了此处再也不赘述)。而table的插入,既能够在Word模版中书签处手动插入:工具栏“插入”→“表格”,亦能够在程序中经过数据区域添加。
同时,Table既能够对齐进行设置和赋值又能够从Table中取值code

  • 对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实现代码以下(命名空间同方法一中相同):get

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的设置和赋值。it

在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的内容更加的丰富和完善。
相关文章
相关标签/搜索