在作項目的過程當中,常常會遇到要把報表導出到Word文件中再打印的狀況,而既然是作報表那就免不了要用到表格,即須要在Word文件中插入table。所以,PageOffice開發平臺中就添加了此功能。服務器
Table類進行寫入操做時工具
Table類進行讀取操做時spa
Word中的table是要藉助數據區域(DataRegion)實現的,要求數據區域完整的包含了整個Table的內容,這樣才能夠經過數據區域控制和操做table。所以,要想使用table需在word文件中插入書籤(數據區域及書籤的添加使用與實現方法前面已經詳細介紹過了此處再也不贅述)。而table的插入,既能夠在Word模版中書籤處手動插入:工具欄「插入」→「表格」,亦能夠在程序中經過數據區域添加。
同時,Table既能夠對齊進行設置和賦值又能夠從Table中取值code
方法一:先在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的表格結構,若是用戶操做時破壞了表格的結構,那麼獲取表格數據的程序確定會出現異常。
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對象 ... ...
在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; //邊框線寬度