private void ExportWord() { if (Tables != null) { var columns = Columns; using (Docx.DocX doc = Docx.DocX.Create(FileName)) { //表格的边框样式 Docx.Border border = new Docx.Border(); border.Tcbs = Docx.BorderStyle.Tcbs_single; int n = 0; foreach (TableEntity tableEntity in Tables) { string tableId = tableEntity.ID.ToString(); //插入表名 Docx.Paragraph title = doc.InsertParagraph(); title.Append(tableEntity.TableName + "(" + tableEntity.Attr + ")"); title.Alignment = Docx.Alignment.center; title.FontSize(15); title.Bold(); title.SetLineSpacing(Docx.LineSpacingType.After, 1); title.SetLineSpacing(Docx.LineSpacingType.Before, 1); DataTable fields = service.GetColumnDataTable(tableId); int rowCount = (fields == null ? 0 : fields.Rows.Count) + 1; //计算表格多少行,多少列 Docx.Table table = doc.InsertTable(rowCount, columns.Count); //先生成列头 Docx.Row colRow = table.Rows[0]; int k = 0; foreach (string colKey in columns.Keys) { Docx.Cell colCell = colRow.Cells[k]; colCell.Paragraphs[0].Append(columns[colKey]).Alignment = Docx.Alignment.center; colCell.SetBorder(Docx.TableCellBorderType.Top, border); colCell.SetBorder(Docx.TableCellBorderType.Bottom, border); colCell.SetBorder(Docx.TableCellBorderType.Left, border); colCell.SetBorder(Docx.TableCellBorderType.Right, border); k++; } for (int i = 0; i < fields.Rows.Count; i++) { //一个属性为一行 Docx.Row row = table.Rows[i + 1]; //循环每列 int j = 0; foreach (string key in columns.Keys) { Docx.Cell cell = row.Cells[j]; string text = fields.Rows[i][key].ToString(); if (key == requiredKey) { text = text.ToLower() == "true" ? "是" : ""; } cell.Paragraphs[0].Append(text).Alignment = Docx.Alignment.center; cell.Paragraphs[0].FontSize(10); cell.SetBorder(Docx.TableCellBorderType.Top, border); cell.SetBorder(Docx.TableCellBorderType.Bottom, border); cell.SetBorder(Docx.TableCellBorderType.Left, border); cell.SetBorder(Docx.TableCellBorderType.Right, border); j++; } } n++; if (OnProgress != null) { OnProgress(this, new ProgressEventArgs() { Max = Tables.Count, Value = n }); } } doc.Save(); } } }
/// <summary> /// Copy (text) contents of srcCell into dstCell. /// Clears text of dstCell plus all but first paragraph instance before copying. /// </summary> /// <param name="srcCell"></param> /// <param name="dstCell"></param> public static void CellCopyContents(Cell srcCell, Cell dstCell) { CellClear(dstCell, true); srcCell.Paragraphs.ToList().ForEach(p => dstCell.InsertParagraph(p)); }
public static void CellCopyProperties (Cell fromCell, Cell toCell) { if (!fromCell.FillColor.IsEmpty) { toCell.FillColor = fromCell.FillColor; } if (!fromCell.Shading.IsEmpty) { toCell.Shading = fromCell.Shading; } toCell.MarginBottom = fromCell.MarginBottom; toCell.MarginLeft = fromCell.MarginLeft; toCell.MarginRight = fromCell.MarginRight; toCell.MarginTop = fromCell.MarginTop; toCell.TextDirection = fromCell.TextDirection; toCell.VerticalAlignment = fromCell.VerticalAlignment; }
/// <summary> /// Clear text and images from all paragraphs in cell. /// <para/> /// Optional: Delete all paragraph objects. /// Keep in mind, cells with zero paragraphs are considered malformed by Word! /// Also keep in mind, you will lose formatting info (e.g. font). /// <para/> /// Special cases of content other than text or images may not be removed; /// If this becomes a problem, we can develop it. /// </summary> /// <param name="cell"></param> /// <param name="deleteAllParagraphs"></param> public static void CellClear(Cell cell, bool deleteAllParagraphs = false) { // Remove all but first paragraph cell.Paragraphs.Skip(deleteAllParagraphs ? 0 : 1).ToList().ForEach(p => cell.RemoveParagraph(p)); if (!deleteAllParagraphs) { var p = cell.Paragraphs.Last(); if (p.Text.Length > 0) { p.RemoveText(0, p.Text.Length); } } cell.Pictures.ForEach(pic => pic.Remove()); }
private void SetBorder(Cell cell) { cell.SetBorder(TableCellBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Black)); cell.SetBorder(TableCellBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Black)); cell.SetBorder(TableCellBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Black)); cell.SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.Black)); }