public override VisitorAction VisitCellStart(Aspose.Words.Tables.Cell cell) { // Determone index of current table int tabIdx = mWordTables.IndexOf(cell.ParentRow.ParentTable); // Determine index of current row int rowIdx = cell.ParentRow.ParentTable.IndexOf(cell.ParentRow); // And determine index of current cell int cellIdx = cell.ParentRow.IndexOf(cell); // Determine colspan and rowspan of current cell int colSpan = 0; int rowSpan = 0; if (tabIdx < mTables.Count && rowIdx < mTables[tabIdx].Rows.Count && cellIdx < mTables[tabIdx].Rows[rowIdx].Cells.Count) { colSpan = mTables[tabIdx].Rows[rowIdx].Cells[cellIdx].ColSpan; rowSpan = mTables[tabIdx].Rows[rowIdx].Cells[cellIdx].RowSpan; } Console.WriteLine("{0}.{1}.{2} colspan={3}\t rowspan={4}", tabIdx, rowIdx, cellIdx, colSpan, rowSpan); return(VisitorAction.Continue); }
Aspose.Words.Tables.Cell CreateCell(string value, Document doc) { Aspose.Words.Tables.Cell c1 = new Aspose.Words.Tables.Cell(doc); Aspose.Words.Paragraph p = new Paragraph(doc); p.AppendChild(new Run(doc, value)); c1.AppendChild(p); return(c1); }
protected Aspose.Words.Tables.Cell CreateCell(string value, Document doc) { Aspose.Words.Tables.Cell c1 = new Aspose.Words.Tables.Cell(doc); c1.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; c1.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; Aspose.Words.Paragraph p = new Paragraph(doc); p.AppendChild(new Run(doc, value)); c1.AppendChild(p); return(c1); }
/// <summary> /// 合并 /// </summary> /// <param name="builder"></param> /// <param name="table"></param> /// <param name="mcell"></param> public static void MergeCell(Aspose.Words.DocumentBuilder builder, Table table, IList <MCell> mcell) { try { var rCount = table.Rows.Count; var cCount = table.Rows[0].Cells.Count; for (int i = 0; i < mcell.Count; i++) { var m = mcell[i]; if (m.IsColumn) { //某一列进行合并 //到达的行大于总行数、或者起始的某一列所以大于总列数 if (m.toR2C > rCount || cCount < m.C2RIndex) { continue; } //开始位置,起始行的开始列 Cell sCell = table.Rows[m.fromR2C].Cells[m.C2RIndex]; //结束位置,结束行的开始列 Cell eCell = table.Rows[m.toR2C].Cells[m.C2RIndex]; builder.AAMergeCells(sCell, eCell); } else { //某一行进行合并 //到达列大于总列数或者起始的某一行大于总行数 if (m.toR2C > cCount || rCount < m.C2RIndex) { continue; } Cell sCell = table.Rows[m.C2RIndex].Cells[m.fromR2C]; Cell eCell = table.Rows[m.C2RIndex].Cells[m.toR2C]; builder.AAMergeCells(sCell, eCell); } } } catch (Exception ex) { } }
/// <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)); } } } }
/// <summary> /// Convert Excel Cell to Word Cell /// </summary> /// <param name="doc">Parent document</param> /// <param name="cells">Excel cells collection</param> /// <param name="rowIndex">Row index</param> /// <param name="columnIndex">Column index</param> /// <returns>Word Cell</returns> private Aspose.Words.Tables.Cell ImportExcelCell(Document doc, Aspose.Cells.Cells cells, int rowIndex, int columnIndex) { //Create a new Word Cell Aspose.Words.Tables.Cell wordsCell = new Aspose.Words.Tables.Cell(doc); //Get Excel cell from collection Aspose.Cells.Cell excelCell = cells[rowIndex, columnIndex]; //Set cell width double cellWidth = ConvertUtil.PixelToPoint(cells.GetColumnWidthPixel(columnIndex)); wordsCell.CellFormat.PreferredWidth = PreferredWidth.FromPoints(cellWidth); wordsCell.CellFormat.Width = ConvertUtil.PixelToPoint(cellWidth); //Set background color wordsCell.CellFormat.Shading.ForegroundPatternColor = excelCell.GetDisplayStyle().ForegroundColor; wordsCell.CellFormat.Shading.BackgroundPatternColor = excelCell.GetDisplayStyle().BackgroundColor; //Set background texture wordsCell.CellFormat.Shading.Texture = ConvertBackgroundTexture(excelCell.GetDisplayStyle().Pattern); //Import borders from Excel cell to Word cell ImportBorders(wordsCell, excelCell); //Set vertical alignment wordsCell.CellFormat.VerticalAlignment = ConvertVerticalAlignment(excelCell.GetDisplayStyle().VerticalAlignment); //If Excel cells is merged then merge cells in Word Table wordsCell.CellFormat.VerticalMerge = ConvertVerticalCellMerge(excelCell); wordsCell.CellFormat.HorizontalMerge = ConvertHorizontalCellMerge(excelCell); //Create paragraph that will containc content of cell Paragraph wordsParagraph = new Paragraph(doc); //Set horizontal alignment wordsParagraph.ParagraphFormat.Alignment = ConvertHorizontalAlignment(excelCell.GetDisplayStyle().HorizontalAlignment); //Get text with formating from Excel cell as collection Run ArrayList wordRuns = GetTextFromCell(excelCell, doc); foreach (Run run in wordRuns) { wordsParagraph.AppendChild(run); } //Import formating of the cell ImportFont(wordsParagraph.ParagraphBreakFont, excelCell.GetDisplayStyle().Font); //Insert paragrahp with content into cell wordsCell.AppendChild(wordsParagraph); //If Excel cell contains drawing object then convert this object and insert into Word cell InsertDrawingObject(excelCell, wordsCell); return wordsCell; }