Asp.Netテンプレート(.dot)からWordをエクスポート

4001 ワード

OfficeのDLLを参照する必要があります.添付ファイルにあります.
 
コアコード(転載):
 Microsoft.Office.Interop.Word._Application appWord = new Microsoft.Office.Interop.Word.ApplicationClass();
        Microsoft.Office.Interop.Word._Document docFile = null;
        try
        {
            appWord.Visible = false;
            object objTrue = true;
            object objFalse = false;
            object objTemplate = Server.MapPath(@"dot//123.dot");//    
            object objDocType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
            docFile = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
            //     word  
            //      
            object obDD_Name = "DD_Name";//   
            object obDD_Sex = "DD_Sex";//   
            object obDD_Age = "DD_Age";//  
            object obDD_Birthday = "DD_Birthday"; //    
            object obDD_Nation = "DD_Nation"; //   
            object obDD_Native = "DD_Native"; //   

            //        ,     
            //SqlDataReader dr = XXXXX;//        
            //         
            //     
            docFile.Bookmarks.get_Item(ref obDD_Name).Range.Text = "   "; //   
            docFile.Bookmarks.get_Item(ref obDD_Sex).Range.Text = "   ";
            docFile.Bookmarks.get_Item(ref obDD_Age).Range.Text = "  ";

            //      word
            DateTime dt = DateTime.Now;
            object filename = Server.MapPath("dot//") + " " + dt.Ticks.ToString() + ".doc";
            object miss = System.Reflection.Missing.Value;
            docFile.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            object missingValue = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            docFile.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Quit(ref miss, ref miss, ref miss);
            docFile = null;
            appWord = null;
        }
        catch (Exception ex)
        {
            //    ,           ,  word,      
            string aa = e.ToString();
            object miss = System.Reflection.Missing.Value;
            object missingValue = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            docFile.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Quit(ref miss, ref miss, ref miss);
            docFile = null;
            appWord = null;
            throw ex;
        }

 
サービス側が生成すると、簡単なストリームダウンロードコードが追加されます.
              string fileName = " .doc";//         
                string filePath = Server.MapPath("~/Download//Word//Table1.doc");//  

                //           
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.ContentType = "application/octet-stream";
                //              
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();