.net動態生成word文檔,並下載


哇,博客終於審批通過啦!趕緊不忘過來寫篇博文紀念一下!!哈哈

其實開通這個博客一方面是希望養成自己良好的學習習慣,另一方面也是以往總是自己默默的單方面的得到其它博友們的幫助,長此以往,心里過意不去,所以也想把自己平時遇到的難題或經驗還是小方法也跟大家分享一下,哦啦!

 

費話不多說,正題!

最近在做一個網上報名考試網站,里面涉及到准考證的打印,想說把准考證信息動態生成文本文檔,供考生下載這么一個功能,走了一些彎路,下面把我的成果分享給大家,希望幫助到一些正在經歷類似難題的親們……

首先,要創建word對象

創建之前別忘了引用
using Microsoft.Office.Interop.Word;

 

Object Nothing = System.Reflection.Missing.Value;

        //創建Word文檔
        Application WordApp = new ApplicationClass();
        Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

        //添加頁眉
        WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
        WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
        WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[頁眉內容]");
        WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;//設置右對齊
        WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出頁眉設置

        WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//設置文檔的行間距

        //移動焦點並換行
        object count = 8;
        object WdLine = WdUnits.wdLine;//換一行;
        WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移動焦點
        WordApp.Selection.TypeParagraph();//插入段落

        //文檔中創建表格
        Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 8, 3, ref Nothing, ref Nothing);

        //設置表格樣式
        newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
        newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
        newTable.Columns[1].Width = 100f;
        newTable.Columns[2].Width = 220f;
        newTable.Columns[3].Width = 105f;


            //填充表格內容
            newTable.Cell(1, 1).Range.Text = row["posname"].ToString() + "考試准考證";
            newTable.Cell(1, 1).Range.Bold = 2;//設置單元格中字體為粗體
            newTable.Cell(1, 1).Height = 30;
            newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
            newTable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
            WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中

            //填充表格內容
            newTable.Cell(2, 1).Range.Text = "姓名";
            newTable.Cell(2, 2).Range.Text = "xxxx";

            newTable.Cell(3, 1).Range.Text = "身份證號";
            newTable.Cell(3, 2).Range.Text = "xxxx";

            newTable.Cell(4, 1).Range.Text = "准考證號";
            newTable.Cell(4, 2).Range.Text = "xxxx";

            newTable.Cell(5, 1).Range.Text = "考點名稱";
            newTable.Cell(5, 2).Range.Text = "xxxx";

            newTable.Cell(6, 1).Range.Text = "考點地址";
            newTable.Cell(6, 2).Range.Text = "xxxx";

            newTable.Cell(7, 1).Range.Text = "考試座位";
            newTable.Cell(7, 2).Range.Text = "xxxx";

            newTable.Cell(8, 1).Range.Text = "考試時間";
            newTable.Cell(8, 2).Range.Text = "xxxx";

 到這里為此,一個簡單的文檔大致就成型了,如果需要在本文檔里加入圖片的話呢,請看下面:

           //縱向合並單元格
            newTable.Cell(2, 3).Select();//選中一行
            object moveUnit = WdUnits.wdLine;
            object moveCount = 6;
            object moveExtend = WdMovementType.wdExtend;
            WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
            WordApp.Selection.Cells.Merge();
            newTable.Cell(2, 3).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
            WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中

            //插入圖片
            string FileName = "E:\\123.jpg";//圖片所在路徑
            object LinkToFile = false;
            object SaveWithDocument = true;
            object Anchor = WordDoc.Application.Selection.Range;
            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
            WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 80f;//圖片寬度
            WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 110f;//圖片高度
            //將圖片設置為四周環繞型
            Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
            s.WrapFormat.Type = WdWrapType.wdWrapSquare;
           
            WordDoc.Paragraphs.Last.Range.Text = "文檔創建時間:" + DateTime.Now.ToString();//“落款”
            WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphRight;

             string name = "123.doc";
            object filename = Server.MapPath("TicketFile") + "\\" + name;  //文件保存路徑

            //文件保存
            WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
           
            if(WordDoc != null)
            {
                WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                WordDoc = null;
            }
            if (WordApp != null)
            {
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

                WordApp = null;
            }

寫到這里,整個文檔的生成就大功告成了,不過這其中我有個疑惑,就是在關閉文檔時,我之前的代碼是這樣的

      WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);

                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
這么寫之后,老是在quit這里報錯,后面改成上面那種樣子之后,不知道是不是因為這個原因,“莫名奇妙”地就好了 ,有哪個高手給我解釋一下!!!嘿嘿     

   如果想把該 文檔直接下載到本地的話,就簡單了,相信大家都會,我把代碼粘在下面: 

           string path = Server.MapPath("TicketFile/") + name;
            FileInfo fi = new FileInfo(path);
            if (fi.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(name));
                Response.AddHeader("Content-Length", fi.Length.ToString());
                Response.ContentType = "application/octet-stream;charset=gb2321";
                Response.WriteFile(fi.FullName);
                Response.Flush();
                Response.Close();
            }
        }

好了好了,這就是我弄了兩天的東西,呵呵,菜鳥一個,希望能對大家有幫助,少走彎路!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM