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> /// 添加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; }