1、只能导出成word2007格式(.docx),可直接导出到客户端
2、服务器上不需要装任何东西,也没有权限限制,比较适合导出表格(支持图片)
3、需要一个国外的DocX.dll插件
4、需要添加引用:System.Drawing
5、需要引用:
using Novacode;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using System.Data;
6、方法样例如下:
public static void aa()
{
// Create a document.
using (DocX document = DocX.Create("F:/测试/test2/生成的word/111table.docx"))
{
// Add a hyperlink into the document.
//Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));
Paragraph title = document.InsertParagraph().Append("Test").FontSize();//.Font(new FontFamily("Comic Sans MS"));
title.Alignment = Alignment.center;
Table table = document.InsertTable(, );
table.Design = TableDesign.TableGrid;
//table.AutoFit = AutoFit.ColumnWidth;
//宽度设置(每个单元格都得设置),默认每个单元格宽度:154,表格总宽度大约:687
table.Rows[].Cells[].Width = ;
table.Rows[].Cells[].Width = ;
table.Rows[].Cells[].Width = ;
table.Rows[].Cells[].Width = ;
//table.Design = TableDesign.ColorfulGridAccent2;
table.Alignment = Alignment.center;
table.SetBorder(TableBorderType.InsideH, new Border());
table.SetBorder(TableBorderType.InsideV, new Border());
table.SetBorder(TableBorderType.Top, new Border());
table.SetBorder(TableBorderType.Right, new Border());
table.SetBorder(TableBorderType.Left, new Border());
table.SetBorder(TableBorderType.Bottom, new Border()); //table.AutoFit = AutoFit.ColumnWidth;//.Window;
table.Rows[].Cells[].Paragraphs[].Append(table.Rows[].Cells[].Paragraphs.Count.ToString());
table.Rows[].Cells[].Paragraphs[].Alignment = Alignment.right;
table.Rows[].Cells[].Paragraphs[].Append("").FontSize();//字体大小设置
table.Rows[].Cells[].Paragraphs[].InsertText("测试1");
//颜色设置
table.Rows[].Cells[].Paragraphs[].Color(Color.DarkBlue);
//加粗
table.Rows[].Cells[].Paragraphs[].Bold();
table.Rows[].MergeCells(, );
for (; table.Rows[].Cells[].Paragraphs.Count != ; )
table.Rows[].Cells[].Paragraphs.Last().Remove(false);
table.Rows[].Cells[].Paragraphs[].InsertText("测试2");
table.Rows[].Cells[].Paragraphs[].Append("");
table.Rows[].Cells[].Paragraphs[].Append(table.Rows[].Cells[].Paragraphs[].Text.Length.ToString());
Paragraph p2 = document.InsertParagraph(); Table table1 = document.InsertTable(, );
table1.AutoFit = AutoFit.Window;
table1.Alignment = Alignment.center;
table1.SetBorder(TableBorderType.InsideH, new Border());
table1.SetBorder(TableBorderType.InsideV, new Border());
table1.SetBorder(TableBorderType.Top, new Border());
table1.SetBorder(TableBorderType.Right, new Border());
table1.SetBorder(TableBorderType.Left, new Border());
table1.SetBorder(TableBorderType.Bottom, new Border());
table1.Rows[].MergeCells(, ); table1.Rows[].Cells[].Paragraphs[].Append(table1.Rows[].Cells[].Paragraphs.Count.ToString());
table1.Rows[].Cells[].Paragraphs[].Alignment = Alignment.right;
table1.Rows[].Cells[].Paragraphs[].Append("");
table1.Rows[].Cells[].Paragraphs[].InsertText("测试1");
table1.Rows[].Cells[].Paragraphs[].Bold();
for (; table1.Rows[].Cells[].Paragraphs.Count > ; )
table1.Rows[].Cells[].Paragraphs.Last().Remove(false);
table1.Rows[].Cells[].Paragraphs[].InsertText("测试2");
table1.Rows[].Cells[].Paragraphs[].Append("31\r\n");
table1.Rows[].Cells[].Paragraphs[].Append(table1.Rows[].Cells[].Paragraphs[].Text.Length.ToString()); System.IO.MemoryStream stream = new MemoryStream();
//document.Save();
document.SaveAs(stream);
byte[] bytes = stream.ToArray();
stream.Close();
stream.Dispose();
context.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition", "attachment; filename=" +
HttpUtility.UrlEncode("test.docx", System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
context.Response.End();
}
}