以前对Excel或Word文档操作都使用微软的COM组件Microsoft Word 15.0 object library。
但是这种方式必须要求服务器上安装Office,而且会出现读写操作完成后未成功释放资源的情况。
还有使用NPOI的方式,可以在没有安装Office的情况下对Word或Excel文档进行读写操作,但是面对复杂的需求,还是显得无能为力。
于是出现了一些其他的API库,如Spire.Doc和Aspose,不但可以很好地读写操作Office文档,而且可以轻松实现PDF等文档格式的转换。
Spire.Doc支持JAVA和C#开发语言。我们可以从官网下载到。
Document document = new Document(); Section section = document.AddSection(); ShowProgressBar(); //显示进度条 for (int i = 0; i < dsSpInfo.Tables[0].Rows.Count; i++) { GenerateWord(document,section, dsSpInfo.Tables[0], dsQiandi.Tables[0], dsBaocun.Tables[0],i); //生成Word文档 } section.AddColumn(550f, 50f); //添加分栏,参数为分栏宽度 section.AddColumn(550f, 50f); section.MakeColumnsSameWidth();
string s = Guid.NewGuid().ToString("N") + ".docx";
document.SaveToFile(@"temp\" + s, FileFormat.Docx);
document.Close();
System.Diagnostics.Process.Start(@"temp\" + s);
private void GenerateWord(Document document,Section section,DataTable dtSpInfo, DataTable dtQiandi, DataTable dtBaocun,int index) { Paragraph pFamily = section.AddParagraph(); //添加段落 AddTextRange(section, pFamily, cfamily, 14, true, "黑体", Spire.Doc.Documents.HorizontalAlignment.Center); AddTextRange(section, pFamily, family, 14, true, "宋体", Spire.Doc.Documents.HorizontalAlignment.Center); Paragraph paragraph = section.AddParagraph(); AddTextRange(section, paragraph, cGenus, 12, true, "黑体", Spire.Doc.Documents.HorizontalAlignment.Left); AddTextRange(section, paragraph, genus, 12, true, "Times New Roman", Spire.Doc.Documents.HorizontalAlignment.Left); DataRow[] drQiandi = dtQiandi.Select("SPID='" + spid + "'"); //获取表格数据 if (drQiandi.Length > 0) { String[] headerQiandi = { "保存地点", "种质份数", "个体数量", "引种方式", "来源地", "生长状况" }; //表头字段 string[][] arrQiandiData = new string[drQiandi.Length][]; for (int i = 0; i < drQiandi.Length; i++) { arrQiandiData[i] = new string[] { drQiandi[i]["保存地点"].ToString(), drQiandi[i]["种质份数"].ToString(), drQiandi[i]["个体数量"].ToString(), drQiandi[i]["引种方式"].ToString(), drQiandi[i]["来源地"].ToString(), drQiandi[i]["生长状况"].ToString() }; } Table tableQiandi = section.AddTable(); //新建表格 tableQiandi.ResetCells(arrQiandiData.Length + 1, headerQiandi.Length); tableQiandi.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single; TableRow rowQiandi = tableQiandi.Rows[0]; //添加行 rowQiandi.IsHeader = true; //设为表头 rowQiandi.Height = 16; rowQiandi.HeightType = TableRowHeightType.Auto; for (int i = 0; i < headerQiandi.Length; i++) //生成表头 { rowQiandi.Cells[i].Width = 50; rowQiandi.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; rowQiandi.Height = 20; rowQiandi.HeightType = TableRowHeightType.Auto; Paragraph p = rowQiandi.Cells[i].AddParagraph(); AddTextRange(section, p, headerQiandi[i], 9, true, "黑体", Spire.Doc.Documents.HorizontalAlignment.Center); } for (int r = 0; r < arrQiandiData.Length; r++) //生成表体 { TableRow dataRow = tableQiandi.Rows[r + 1]; dataRow.RowFormat.BackColor = Color.Empty; for (int c = 0; c < arrQiandiData[r].Length; c++) { dataRow.Cells[c].Width = 50; dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; TextRange tr = dataRow.Cells[c].AddParagraph().AppendText(arrQiandiData[r][c]); tr.CharacterFormat.FontSize = 8; } } } }
private void AddTextRange(Section section, Paragraph pragraph, string word, float fontSize, bool isBold, string fontName, Spire.Doc.Documents.HorizontalAlignment alignType) { TextRange textRange = pragraph.AppendText(word); textRange.CharacterFormat.FontSize = fontSize; textRange.CharacterFormat.Bold = isBold; textRange.CharacterFormat.FontName = fontName; pragraph.Format.HorizontalAlignment = alignType; }
下面是生成的效果图
需要注意的是,Spire.Doc是收费软件,如果超过了免费版生成表格的数量(好像是25个),就要收取费用。否则会在生成的文档的第一页上方中添加广告。