static void Main(string[] args) { wordUtil = new WordUtil(); wordUtil.CreateDocument("table.docx"); string connString = ConfigurationManager.ConnectionStrings["oracle"].ToString(); IDbConnection conncetion = new OracleConnection(connString); var tables = GetTables(conncetion); var columns = GetColumns(conncetion).ToList(); var tableTotalCount = tables.Count(); XWPFDocument document = new XWPFDocument(); // Loop all tables. int idxCurTable = 1; foreach (var table in tables) { Console.WriteLine($"# {DateTime.Now.ToString("HH:mm:ss fff")} 当前正在处理表: {table.TableName} 处理进度:{idxCurTable}/{tableTotalCount}"); // Loop all fields of the current table. var tableColumns = columns.FindAll(x => x.TableName == table.TableName); string title = $"{table.TableName} {table.TableComment}"; wordUtil.AddTextParagraph(title, new ParagraphOptions { Alignment = Office.Styles.Alignment.Center }, new TextOptions { FontStyle = new Office.Styles.FontStyle { FontSize = 18, IsBold = true } }); wordUtil.AddTextParagraph($"ID: {Guid.NewGuid()} 创建时间:{DateTime.Now.ToShortDateString()}"); TableModel tableModel = new TableModel(); tableModel.Headers = new string[] { "序号", "参数名", "类型", "长度", "可否为空", "说明" }; tableModel.Rows = new List <TableRow>(); int idxRow = 1; foreach (var column in tableColumns) { TableRow tableRowColumn = new TableRow(); tableRowColumn.RowValue = $"第{idxRow}行"; var columnValues = new List <string>(); columnValues.Add(idxRow.ToString()); columnValues.Add(column.ColumnName); columnValues.Add(column.DataType); columnValues.Add(column.DataLength); columnValues.Add(column.Nullable); columnValues.Add(column.ColumnComment ?? "-"); tableRowColumn.ColumnValues = columnValues; tableModel.Rows.Add(tableRowColumn); idxRow++; } wordUtil.AddTableParagraph(table.TableName, tableModel, new ParagraphOptions { Alignment = Office.Styles.Alignment.Center }, new TextOptions { FontStyle = new Office.Styles.FontStyle { } }); idxCurTable++; } wordUtil.SaveDocument(); Console.WriteLine("END..."); }
private static void CustomDemo() { // TODO: 部分“空”Unicode字符写入word导致word打不开,WPS可以 #region 暂不处理 //var aa = "\u4f60\u597d"; //var bb = "\u0013"; //var cc = ContainsUnicodeCharacter("\u0013"); //var ccc = ContainsUnicodeCharacter("好h"); //var dd = ContainsUnicodeCharacter("\u4f60\u597d"); #endregion wordUtil.CreateDocument("sample.docx"); wordUtil.AddTextParagraph("标题1", new ParagraphOptions { IsTitle = true, TitleSize = "1" }); wordUtil.AddTextParagraph("标题2", new ParagraphOptions { IsTitle = true, TitleSize = "2" }); wordUtil.AddTextParagraph("标题3", new ParagraphOptions { IsTitle = true, TitleSize = "标题 1" }); wordUtil.AddTextParagraph("测试\u4f60\u597d段落1\u263A\u0021\u1F60F"); wordUtil.AddTextParagraph(GetLongText()); wordUtil.AddTextParagraph("测试段落3", new TextOptions { FontStyle = new Word.Styles.FontStyle { ForeColor = "FF00FF" } }); wordUtil.AddMultiTextParagraph(new List <string> { "多段落1", "多段落2", "多段落3" }); wordUtil.AddTextParagraphWithMultiText(new List <string> { "单段落多文本段1", "单段落多文本段2", "单段落多文本段3" }, " | "); wordUtil.AddTextParagraphWithMultiText(GetOneParagraphMultiTextModelFakeData()); wordUtil.AddTextParagraphWithMultiText(GetOneParagraphMultiTextModelFakeData()); // 表格 wordUtil.AddTableParagraph("table name", GetTableModel(5, 10), new ParagraphOptions { Alignment = Word.Styles.Alignment.Center }, new TextOptions { FontStyle = new Word.Styles.FontStyle { IsBold = true } }); wordUtil.AddTextParagraph("--------------------------"); wordUtil.AddTableParagraph("table name", GetTableModel(5, 10), new ParagraphOptions { Alignment = Word.Styles.Alignment.Center }, new TextOptions { FontStyle = new Word.Styles.FontStyle { IsBold = true } }); wordUtil.AddTextParagraph("--------------------------"); wordUtil.SaveDocument(); }