阿里云折扣码

轻云博客 > 博客分类 > asp.net导出word(word2007)

asp.net导出word(word2007)

作者:Aisencici / 日期:2014-5-9 9:09:00 / 分类:博客分类 / 浏览:6195

1、只能导出成word2007格式(.docx),可直接导出到客户端

2、服务器上不需要装任何东西,也没有权限限制,比较适合导出表格(支持图片)

3、需要一个国外的DocX.dll插件(我QQ邮箱里有该插件)

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();

            }

        }


本文标签:asp.net导出word
From:Aisencici
分享到: