之前对Excel或Word文档操做都使用微软的COM组件Microsoft Word 15.0 object library。c++
可是这种方式必需要求服务器上安装Office,并且会出现读写操做完成后未成功释放资源的状况。服务器
还有使用NPOI的方式,能够在没有安装Office的状况下对Word或Excel文档进行读写操做,可是面对复杂的需求,仍是显得无能为力。ui
因而出现了一些其余的API库,如Spire.Doc和Aspose,不但能够很好地读写操做Office文档,并且能够轻松实现PDF等文档格式的转换。spa
Spire.Doc支持JAVA和C#开发语言。咱们能够从官网下载到。code
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; }
下面是生成的效果图orm
须要注意的是,Spire.Doc是收费软件,若是超过了免费版生成表格的数量(好像是25个),就要收取费用。不然会在生成的文档的第一页上方中添加广告。blog