Object Nothing = System.Reflection.Missing.Value;
string tempname = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".doc" ;
object filename = Server.MapPath("ExportDoc") + "\\" + tempname ; //存檔路徑
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
//建立一Word文件
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
//增加一個表格
Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range,2,2,ref Nothing,ref Nothing);
//在表格指定位置中增加內容
table.Cell(1,1).Range.Text = "新增表格內容文字(1,1)";
table.Cell(1,2).Range.Text = "新增表格內容文字(1,2)";
table.Cell(2,1).Range.Text = "新增表格內容文字(2,1)";
table.Cell(2,2).Range.Text = "新增表格內容文字(2,2)";
//在表格下方加入內容
WordDoc.Paragraphs.Last.Range.Text = "MicrosoftWord2003 第一行文字\n"; // \n 換行
WordDoc.Paragraphs.Last.Range.Text = "MicrosoftWord2003 第二行文字\r"; // \r 換行
WordDoc.Paragraphs.Last.Range.Text = "MicrosoftWord2003 第三行文字\n\r"; // \n\r 換行
//將修改的Word文件存檔<指定存檔路徑>
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);
//關閉Word文件
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//關閉Word組件
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//返回結果
this.Label1.Text = "<a href ='ExportDoc/"+ tempname +"'> 查看產生的文件 </a>";
沒有留言:
張貼留言