Popular blog tags
用Aspose.Words来创建和操作Word文档。 以C#创建一个类似的表格模型从而稍后当读取文档的时候我们可以用上它。 如下所示,你可以看到创建的名为WordDocumentTable的类,带有三个属性:TableID,RowID和ColumnID,如之前所说的,我们没有支持TableID/RowIDs,这些属性仅仅暗示着Word文档的位置。开始索引假定为0。     01.public class WordDocumentTable 02.{ 03.public WordDocumentTable(int PiTableID) 04.{  05.MiTableID = PiTableID; 06.} 07.public WordDocumentTable(int PiTableID, int PiColumnID) 08.{  09.MiTableID = PiTableID;  10.MiColumnID = PiColumnID; 11.} 12.public WordDocumentTable(int PiTableID, int PiColumnID, int PiRowID) 13.{  14.MiTableID = PiTableID;  15.MiColumnID = PiColumnID;  16.MiRowID = PiRowID; 17.} 18.private int MiTableID = 0; 19.public int TableID 20.{  21.get { return MiTableID; }  22.set { MiTableID = value; } 23.}        24.private int MiRowID = 0;    25.public int RowID 26.{  27.get { return MiRowID; }  28.set { MiRowID = value; } 29.} 30.private int MiColumnID = 0;    31.public int ColumnID 32.{  33.get { return MiColumnID; }  34.set { MiColumnID = value; } 35.} 36.} 现在来到提取环节。如下所示,你将看到我想要从文档中读取的表格单元格的集。  view sourceprint? 01.private List<WordDocumentTable> WordDocumentTables 02.{  03.get 04.{    05.List<WordDocumentTable> <a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a>DocTable = new List<WordDocumentTable>();      06.//Reads the data from the first Table of the document.    07.<a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a>DocTable.Add(new WordDocumentTable(0));      08.//Reads the data from the second table and its second column. 09.//This table has only one row.    10.wordDocTable.Add(new WordDocumentTable(1, 1));      11.//Reads the data from third table, second row and second cell.    12.wordDocTable.Add(new WordDocumentTable(2, 1, 1));  13.return wordDocTable;  14.} 15.} 下面是从基于表格、行和单元格的Aspose.Words文档提取数据。  view sourceprint? 01.public void ExtractTableData(byte[] PobjData) 02.{          03.using (MemoryStream LobjStream = new MemoryStream(PobjData)) 04.{  05.Document LobjAsposeDocument = new Document(LobjStream);     06.foreach(WordDocumentTable wordDocTable in WordDocumentTables)  07.{   08.Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table) 09.LobjAsposeDocument.GetChild 10.(NodeType.Table, wordDocTable.TableID, true);   11.string cellData = table.Range.Text; 12.if (wordDocTable.ColumnID > 0)   13.{    14.if (wordDocTable.RowID == 0)    15.{     16.NodeCollection LobjCells = 17.table.GetChildNodes(NodeType.Cell, true);     18.cellData = LobjCells[wordDocTable.ColumnID].ToTxt(); 19.}    20.else   21.{     22.NodeCollection LobjRows = 23.table.GetChildNodes(NodeType.Row, true);     24.cellData = ((Row)(LobjRows[wordDocTable.RowID])). 25.Cells[wordDocTable.ColumnID].ToTxt();    26.}   27.} 28.Console.WriteLine(String.Format("Data in Table {0}, 29.Row {1}, Column {2} : {3}",           30.wordDocTable.TableID,          31.wordDocTable.RowID,          32.wordDocTable.ColumnID,          33.cellData);              34.} 35.} 36.} .