给文档加密码保护 public static Stream SetDocPRotect(byte[] docContent, string key) { var mem = new MemoryStream(docContent); mem.Seek(0, SeekOrigin.Begin);
IWordDocument document = new WordDocument(mem, FormatType.Automatic); document.Protect(ProtectionType.AllowOnlyFormFields, key); var outMem = new MemoryStream(); document.Save(outMem, FormatType.Doc); outMem.Seek(0, SeekOrigin.Begin); return outMem; }
在书签位置创建一个表格 public static IWTable ReplaceTable(WordDocument document, string bookmarkName, DataTable data, string mergeColName , List<List<string>> mutilTableCaption) { if (document == null) throw new ArgumentNullException("document"); if (bookmarkName == null) throw new ArgumentNullException("bookmarkName"); if (data == null) throw new ArgumentNullException("data"); if (data.Columns.Count < 1) throw new ArgumentNullException("data");
int captionCount = mutilTableCaption != null && mutilTableCaption.Count > 0 ? mutilTableCaption.Count : 1; WTable table = new WTable(document, true);
for (var colCount = 0; colCount < captionCount; colCount++) { for (var col = 0; col < data.Columns.Count; col++) { var paragraph = table.Rows[colCount].Cells[col].AddParagraph();
var caption = data.Columns[col].ColumnName; if (mutilTableCaption != null && mutilTableCaption.Count > 0) caption = mutilTableCaption[colCount][col]; var text = paragraph.AppendText(caption); paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; text.CharacterFormat.FontName = "宋体"; text.CharacterFormat.Bold = false; text.CharacterFormat.FontSize = 10.5f; } }
for (var row = captionCount; row <= data.Rows.Count; row++) for (var col = 0; col < data.Columns.Count; col++) { var paragraph = table.Rows[row].Cells[col].AddParagraph(); var text = paragraph.AppendText(data.Rows[row - captionCount][col] + "");
text.CharacterFormat.FontName = "宋体"; text.CharacterFormat.FontSize = 9f; double val = 0; if (double.TryParse(text.Text, out val)) { text.Text = Math.Round(val, 2) + ""; //align right paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right; table.Rows[row].Cells[col].CellFormat.VerticalAlignment = VerticalAlignment.Middle; table.Rows[row].Cells[col].CellFormat.TextWrap = false; } } //合并单元格,向下合并 if (!string.IsNullOrEmpty(mergeColName)) for (var row = captionCount; row < table.Rows.Count; row++) { var cell = table.Rows[row].Cells[data.Columns[mergeColName].Ordinal]; cell.CellFormat.VerticalMerge = CellMerge.Start; var text = data.Rows[row - captionCount][mergeColName] + ""; if (row > captionCount) { var priorCell = table.Rows[row - captionCount].Cells[data.Columns[mergeColName].Ordinal]; var findText = data.Rows[row - captionCount - 1][mergeColName] + ""; if (text.Equals(findText)) cell.CellFormat.VerticalMerge = CellMerge.Continue; } }
BookmarksNavigator bk = new BookmarksNavigator(document); bk.MoveToBookmark(bookmarkName);