asp.net导出word(word2007)

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(20);//.Font(new FontFamily("Comic Sans MS"));
                title.Alignment = Alignment.center;
                Table table = document.InsertTable(4, 4);
                table.Design = TableDesign.TableGrid;
                //table.AutoFit = AutoFit.ColumnWidth;
                //宽度设置(每个单元格都得设置),默认每个单元格宽度:154,表格总宽度大约:687
                table.Rows[0].Cells[0].Width = 100;
                table.Rows[0].Cells[1].Width = 200;
                table.Rows[0].Cells[2].Width = 300;
                table.Rows[0].Cells[3].Width = 87;
                //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[0].Cells[0].Paragraphs[0].Append(table.Rows[1].Cells[0].Paragraphs.Count.ToString());
                table.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.right;
                table.Rows[0].Cells[1].Paragraphs[0].Append("555").FontSize(15);//字体大小设置
                table.Rows[1].Cells[0].Paragraphs[0].InsertText("测试1");
                //颜色设置
                table.Rows[1].Cells[0].Paragraphs[0].Color(Color.DarkBlue);
                //加粗
                table.Rows[1].Cells[0].Paragraphs[0].Bold();
                table.Rows[1].MergeCells(0, 3);
                for (; table.Rows[1].Cells[0].Paragraphs.Count != 1; )
                    table.Rows[1].Cells[0].Paragraphs.Last().Remove(false);
                table.Rows[2].Cells[0].Paragraphs[0].InsertText("测试2");
                table.Rows[3].Cells[0].Paragraphs[0].Append("31");
                table.Rows[3].Cells[1].Paragraphs[0].Append(table.Rows[1].Cells[0].Paragraphs[0].Text.Length.ToString());
                Paragraph p2 = document.InsertParagraph();

                Table table1 = document.InsertTable(4, 8);
                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[1].MergeCells(0, 3);

                table1.Rows[0].Cells[0].Paragraphs[0].Append(table1.Rows[1].Cells[0].Paragraphs.Count.ToString());
                table1.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.right;
                table1.Rows[0].Cells[1].Paragraphs[0].Append("222222");
                table1.Rows[1].Cells[0].Paragraphs[0].InsertText("测试1");
                table1.Rows[1].Cells[0].Paragraphs[0].Bold();
                for (; table1.Rows[1].Cells[0].Paragraphs.Count > 2; )
                    table1.Rows[1].Cells[0].Paragraphs.Last().Remove(false);
                table1.Rows[2].Cells[0].Paragraphs[0].InsertText("测试2");
                table1.Rows[3].Cells[0].Paragraphs[0].Append("31\r\n");
                table1.Rows[3].Cells[1].Paragraphs[0].Append(table1.Rows[1].Cells[0].Paragraphs[0].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();
            }
        }

 

转载于:https://www.cnblogs.com/deep-blue/p/5110021.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值