/// <summary> /// 在固定表格里,按列填充数据 /// </summary> /// <param name="mark">书签名称</param> /// <param name="tableData">表格数据</param> /// <param name="builder">文档构造类</param> private void OnlyFillByColumn(Bookmark mark, string[,] tableData, DocumentBuilder builder) { Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table)mark.BookmarkStart.GetAncestor(NodeType.Table); Aspose.Words.Tables.Row row = (Aspose.Words.Tables.Row)mark.BookmarkStart.GetAncestor(NodeType.Row); Cell cell = (Cell)mark.BookmarkStart.GetAncestor(NodeType.Cell); NodeCollection allTables = this.doc.GetChildNodes(NodeType.Table, true); int tableIndex = allTables.IndexOf(table); int rowIndex = table.IndexOf(row); int cellIndex = row.IndexOf(cell); int columnIndex = cellIndex; for (int i = 0; i < tableData.GetLength(0) && rowIndex < table.Rows.Count; i++) { columnIndex = cellIndex; for (int j = 0; j < tableData.GetLength(1); j++) { if (columnIndex < row.Cells.Count) { builder.MoveToCell(tableIndex, rowIndex, columnIndex++, 0); // 垂直居中对齐 builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; // 水平居中对齐 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.Write(tableData[i, j]); } } rowIndex++; } }
Aspose.Words.Tables.Row CreateRow(int columnCount, object[] columnValues, Document doc, List <double> widthLst) { Aspose.Words.Tables.Row r2 = new Aspose.Words.Tables.Row(doc); for (int i = 0; i < columnCount; i++) { if (columnValues.Length > i) { var cell = CreateCell(columnValues[i].ToString(), doc); cell.CellFormat.Width = widthLst[i]; cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; if (String.IsNullOrEmpty(columnValues[i].ToString())) { cell.CellFormat.VerticalMerge = CellMerge.Previous; } else { cell.CellFormat.VerticalMerge = CellMerge.First; } r2.Cells.Add(cell); } else { var cell = CreateCell("", doc); r2.Cells.Add(cell); } } return(r2); }
/// <summary> /// 追加表格 /// </summary> /// <param name="tableIndex"></param> /// <param name="dt"></param> /// <param name="widthLst"></param> /// <returns></returns> public bool AddTable(int tableIndex, System.Data.DataTable dt, List <double> widthLst) { NodeCollection allTables = _Doc.GetChildNodes(NodeType.Table, true); //拿到所有表格 Aspose.Words.Tables.Table table = allTables[tableIndex] as Aspose.Words.Tables.Table; //拿到第tableIndex个表格 for (int row = 0; row < dt.Rows.Count; row++) { Aspose.Words.Tables.Row r = new Aspose.Words.Tables.Row(_Doc); var newRow = CreateRow(dt.Columns.Count, dt.Rows[row].ItemArray, _Doc, widthLst); //创建一行 table.Rows.Add(newRow); //添加一行 } return(true); }
protected Aspose.Words.Tables.Row CreateRow(string[] columnValues, Document doc) { Aspose.Words.Tables.Row r2 = new Aspose.Words.Tables.Row(doc); for (int i = 0; i < columnValues.Length; i++) { if (columnValues.Length > i) { var cell = CreateCell(columnValues[i], doc); r2.Cells.Add(cell); } else { var cell = CreateCell("", doc); r2.Cells.Add(cell); } } return(r2); }
/// <summary> /// 添加DataTable数据到Table对象(必须是已经有表头的表格) /// </summary> /// <param name="table"></param> /// <param name="table"></param> public void fillDataToTable(Table table, DataTable dataTable) { if (table.Rows.Count >= 1) { //获得列数 int cellCount = table.Rows[table.Rows.Count - 1].Cells.Count; //添加行 int addRowCount = dataTable.Rows.Count - (table.Rows.Count - 1); for (int kkk = 0; kkk < addRowCount; kkk++) { table.Rows.Add(table.Rows[table.Rows.Count - 1].Clone(true)); } //填充数据 int rowIndex = 0; foreach (DataRow dr in dataTable.Rows) { rowIndex++; //创建新行 Aspose.Words.Tables.Row rowObj = table.Rows[rowIndex]; for (int k = 0; k < dataTable.Columns.Count; k++) { if (k >= cellCount) { continue; } //创建列 Aspose.Words.Tables.Cell cellObj = rowObj.Cells[k]; cellObj.AppendChild(newParagraph(table.Document, dr[k] != null ? dr[k].ToString() : string.Empty)); } } } }
public void insertTable(int TableID, DataTable DT) { try { Exception exception; Table table = (Table)this._doc.GetChild(NodeType.Table, TableID, true); if (table == null) { exception = new Exception("Ko tim thay table " + TableID + " trong word!"); throw exception; } Aspose.Words.Tables.Row row = table.Rows[table.Rows.Count - 1]; table.Rows.RemoveAt(table.Rows.Count - 1); if (DT.Columns.Count != row.Cells.Count) { exception = new Exception("So cot cua DataTable & So cot cua table trong Word ko bang nhau!"); throw exception; } for (int i = 0; i < DT.Rows.Count; i++) { Aspose.Words.Tables.Row node = (Aspose.Words.Tables.Row)row.Clone(true); table.Rows.Add(node); int num2 = 0; foreach (Aspose.Words.Tables.Cell cell in node.Cells) { string text = cell.Paragraphs[0].Runs[0].Text; cell.Paragraphs[0].Runs[0].Text = DT.Rows[i][num2].ToString(); num2++; } } } catch (Exception exception2) { throw exception2; } }
/// <summary> /// This Method splits worksheet into a tables collection. /// </summary> /// <param name="excelWorksheet">Input Worksheet</param> /// <param name="doc">Parent document</param> /// <param name="tablePartList">ArrayList where tables will be stored</param> /// <param name="columnStartIndex">Index of the column in Excel worksheet that will be the first column of Word table</param> /// <param name="rowStartIndex">Index of the row in Excel worksheet that will be the first row of Word table</param> /// <param name="columnCount">Column index of a last cell that contains data in the Excel worksheet</param> /// <param name="rowCount">Row index of a last cell that contains data in the Excel worksheet</param> private void GetTablePart( Worksheet excelWorksheet, Document doc, ArrayList tablePartList, int columnStartIndex, int rowStartIndex, int columnCount, int rowCount) { if (columnCount != 0 && rowCount != 0) { //Calculate max width of Words table. //Then we will add columns to Word table while it's width is < maxWidth Aspose.Words.PageSetup setup = doc.FirstSection.PageSetup; double maxWidth = setup.PageWidth - setup.LeftMargin - setup.RightMargin; int newColumnStartIndex = 0; double currentWidth = 0; for (int columnIndex = columnStartIndex; columnIndex <= columnCount; columnIndex++) { //Calculate width of current Word table currentWidth += ConvertUtil.PixelToPoint(excelWorksheet.Cells.GetColumnWidthPixel(columnIndex)); newColumnStartIndex = columnIndex; //If width of table > maxWidth then break loop if (currentWidth > maxWidth && columnIndex != columnStartIndex) { break; } } //Create a new Word table Table wordsTable = new Table(doc); //Loop through rows in the Excel worksheet for (int rowIndex = rowStartIndex; rowIndex < rowCount; rowIndex++) { //Create new row Aspose.Words.Tables.Row wordsRow = new Aspose.Words.Tables.Row(doc); //Get cllection of Excel cells Aspose.Cells.Cells cells = excelWorksheet.Cells; //Set height of current row wordsRow.RowFormat.Height = ConvertUtil.PixelToPoint(cells.GetRowHeightPixel(rowIndex)); //Append current row to current table. wordsTable.AppendChild(wordsRow); //Loop through columns and add columns to Word table while table's width < maxWidth for (int columnIndex = columnStartIndex; columnIndex < newColumnStartIndex; columnIndex++) { //Convert Excel cell to Word cell Aspose.Words.Tables.Cell wordsCell = ImportExcelCell(doc, cells, rowIndex, columnIndex); //Insert cell into rhe row wordsRow.AppendChild(wordsCell); } } // We want the table to take only as much of the page as required. wordsTable.PreferredWidth = PreferredWidth.Auto; //Add Word table to ArrayList tablePartList.Add(wordsTable); if (newColumnStartIndex < columnCount) { //Start next table from newColumnStartIndex GetTablePart(excelWorksheet, doc, tablePartList, newColumnStartIndex, rowStartIndex, columnCount, rowCount); } } }