1.建立word模板檔案 person.dot
用書籤 標示相關欄位的填充位置
2.建立web應用程式 加入Microsoft.Office.Interop.Word引用
具體添加引用請參看
http://www.microsoft.com/china/msdn/library/office/office/OfficePrIntopAssFAQ.mspx?mfr=true
3.相關範例程式碼
protected void CreateReport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application appWord = null;//應用程式
Microsoft.Office.Interop.Word.DocumentClass doc = null;//文檔
try
{
appWord = new Microsoft.Office.Interop.Word.Application();
appWord.Visible = false;
object objTrue = true;
object objFalse = false;
object objTemplate = Server.MapPath("person.dot");//模板路徑
object objDocType = WdDocumentType.wdTypeDocument;
doc = (DocumentClass)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType,ref objTrue);
//第一步產生word文檔
//定義書籤變數
object obDD_Name = "bm_Name";//姓 名
object obDD_Sex = "bm_Sex";//性 別
object obDD_Birthday = "bm_Birthday"; //出生年月
object obpic="pic";
object obtable = "obtable";
object Nothing = System.Reflection.Missing.Value;
//InlineShape shape = appWord.Selection.InlineShapes.AddPicture(@"F:\Picture\_DSC1602.JPG", ref Nothing, ref Nothing, ref Nothing);
//第二步 讀取資料,填充資料集
System.Data.DataTable dt = new DataTable();
dt.Columns.Add("p_Name");
dt.Columns.Add("p_Sex");
dt.Columns.Add("p_Birthday");
DataRow dr = dt.NewRow();
dr["p_Name"] = "張三";
dr["p_Sex"] = "男";
dr["p_Birthday"] = "1980-01-01";
dt.Rows.Add(dr);
//第三步 給書籤賦值
//給書籤賦值
doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = dt.Rows[0]["p_Name"].ToString(); //姓 名
doc.Bookmarks.get_Item(ref obDD_Sex).Range.Text = dt.Rows[0]["p_Sex"].ToString();//性 別
doc.Bookmarks.get_Item(ref obDD_Birthday).Range.Text = dt.Rows[0]["p_Birthday"].ToString();//年齡
doc.Bookmarks.get_Item(ref obpic).Range.InlineShapes.AddPicture(@"F:\Picture\_DSC1602.JPG", ref Nothing, ref Nothing, ref Nothing);
//文檔中插入表格
//doc.Bookmarks.get_Item(ref obtable).Range.Tables.Add(doc.Bookmarks.get_Item(ref obtable).Range, 12, 3, ref Nothing, ref Nothing);
Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(doc.Bookmarks.get_Item(ref obtable).Range, 12, 3, ref Nothing, ref Nothing);
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
//給文檔的最後一行再新增內容
doc.Paragraphs.Last.Range.Text = "";
//第四步 產生word
object filename = Server.MapPath("~") + "http://www.cnblogs.com/chenbg2001/admin/file://bg//" + dt.Rows[0]["p_Name"].ToString() + ".doc";
object miss = System.Reflection.Missing.Value;
doc.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 = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
catch (System.Exception ex)
{
//捕捉異常,如果出現異常則清空執行個體,退出word,同時釋放資源
string aa = ex.ToString();
object miss = System.Reflection.Missing.Value;
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
}
-----
以上代碼在運行時 如遭遇80070005錯誤
解決方案一:
控制台-》管理工具-》元件服務-》電腦-》我的電腦-》DCom配置-》找到Microsoft Word文檔
之後
單擊屬性開啟此應用程式的屬性對話方塊。
2. 單擊標識選項卡,然後選擇互動式使用者。
3.單擊"安全"選項卡,分別在"啟動和啟用許可權"和"存取權限"組中選中"自訂",然後
自訂->編輯->添加ASP.NET賬戶和IUSER_電腦名稱
4. 確保允許每個使用者訪問,然後單擊確定。
5. 單擊確定關閉 DCOMCNFG。
解決方案二:
如果上述方法不能解決問題,就應該是許可權問題,請嘗試用下面的方法:
在web.config中使用身份類比,在<system.web>節中加入 <identity impersonate="true" userName="你的使用者名稱
" password="密碼"/>
</system.web>
參考文檔:http://wenku.baidu.com/view/fc8aa56fb84ae45c3b358c98.html
附:圖片的詳細操作
object filename = @"C:\Inetpub\wwwroot\TestWebApp\test.doc";//檔案名稱
Word.Application a = new Word.ApplicationClass();//建立一個Word程式對像
object Nothing = System.Reflection.Missing.Value;//空值
Word.Document b = a.Documents.Open(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文檔對像
//其實這步就是執行了這個宏
InlineShape shape = a.Selection.InlineShapes.AddPicture(@"C:\Documents and Settings\Administrator\案頭\2003121512223366481.jpg",ref Nothing,ref Nothing,ref Nothing);
shape.Height = InchesToPoints(0.5)
shape.Width = InchesToPoints(0.5)
//Selection.InlineShapes.AddPicture FileName:= "C:\Documents and Settings\Administrator\案頭\2003121512223366481.bmp", LinkToFile:=False, SaveWithDocument:=True End Sub b.Save();//儲存
b.Close(ref Nothing,ref Nothing,ref Nothing);//關閉Word文檔
a.Quit(ref Nothing,ref Nothing,ref Nothing);//退出Word程式