最近項目要實現下載打印的功能,想了想,用水晶報表實在是大材小用, 用office組件直接就能夠實現這一功能。web
引用類 using Microsoft.Office.Interop.Word;安全
創建兩個實體類 一個是導出word須要替換內容的配置,一個是替換內容的實體app
public class AgentInfoEntity { private string comname; ide
public string ComName { get { return comname; } set { comname = value; } } private string linkname; 工具
public string LinkName { get { return linkname; } set { linkname = value; } } private string comemail; post
public string ComEmail { get { return comemail; } set { comemail = value; } } private string sn; ui
public string SN { get { return sn; } set { sn = value; } } code
private string comtel; orm
public string ComTel { get { return comtel; } set { comtel = value; } } private string comcontact; 對象
public string ComContact { get { return comcontact; } set { comcontact = value; } } private string registercomname;
public string RegisterComName { get { return registercomname; } set { registercomname = value; } } private string regaddresstel;
public string RegAddressTel { get { return regaddresstel; } set { regaddresstel = value; } } private string bank;
public string Bank { get { return bank; } set {bank = value; } } private string tax;
public string Tax { get { return tax; } set { tax = value; } } private string regtel;
public string Regtel { get { return regtel; } set { regtel = value; } } private string sendaddress; public string SendAddress { get { return sendaddress; } set { sendaddress = value; } } private string postaddress;
public string PostAddress { get { return postaddress; } set { postaddress = value; } }
private string agentaccount;
public string AgentAccount { get { return agentaccount; } set { agentaccount = value; } }
private string postcontact;
public string PostContact { get { return postcontact; } set { postcontact = value; } }
private string invoicetel;
public string InvoiceTel { get { return invoicetel; } set { invoicetel = value; } } private string financecontact;
public string FinanceContact { get { return financecontact; } set { financecontact = value; } } private string financetel;
public string FinanceTel { get { return financetel; } set { financetel = value; } } private string financefax;
public string FinanceFax { get { return financefax; } set { financefax = value; } } private string financepost;
public string FinancePost { get { return financepost; } set { financepost = value; } } private string deliveryarea;
public string DeliveryArea { get { return deliveryarea; } set { deliveryarea = value; } } private string deliverypost;
public string DeliveryPost { get { return deliverypost; } set { deliverypost = value; } } }
public class AgentInfoEntity { private string comname;
public string ComName { get { return comname; } set { comname = value; } } private string linkname;
public string LinkName { get { return linkname; } set { linkname = value; } } private string comemail;
public string ComEmail { get { return comemail; } set { comemail = value; } } private string sn;
public string SN { get { return sn; } set { sn = value; } }
private string comtel;
public string ComTel { get { return comtel; } set { comtel = value; } } private string comcontact;
public string ComContact { get { return comcontact; } set { comcontact = value; } } private string registercomname;
public string RegisterComName { get { return registercomname; } set { registercomname = value; } } private string regaddresstel;
public string RegAddressTel { get { return regaddresstel; } set { regaddresstel = value; } } private string bank;
public string Bank { get { return bank; } set {bank = value; } } private string tax;
public string Tax { get { return tax; } set { tax = value; } } private string regtel;
public string Regtel { get { return regtel; } set { regtel = value; } } private string sendaddress; public string SendAddress { get { return sendaddress; } set { sendaddress = value; } } private string postaddress;
public string PostAddress { get { return postaddress; } set { postaddress = value; } }
private string agentaccount;
public string AgentAccount { get { return agentaccount; } set { agentaccount = value; } }
private string postcontact;
public string PostContact { get { return postcontact; } set { postcontact = value; } }
private string invoicetel;
public string InvoiceTel { get { return invoicetel; } set { invoicetel = value; } } private string financecontact;
public string FinanceContact { get { return financecontact; } set { financecontact = value; } } private string financetel;
public string FinanceTel { get { return financetel; } set { financetel = value; } } private string financefax;
public string FinanceFax { get { return financefax; } set { financefax = value; } } private string financepost;
public string FinancePost { get { return financepost; } set { financepost = value; } } private string deliveryarea;
public string DeliveryArea { get { return deliveryarea; } set { deliveryarea = value; } } private string deliverypost;
public string DeliveryPost { get { return deliverypost; } set { deliverypost = value; } } }
/// <summary> /// l根據實體取得屬性的值。 /// </summary> /// <param name="ainfo"></param> /// <param name="key"></param> /// <returns></returns> public string getProperties(AgentInfoEntity ainfo,string key) { string tStr = string.Empty; if (ainfo== null) { return tStr; } System.Reflection.PropertyInfo[] properties = ainfo.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0) { return tStr; } foreach (System.Reflection.PropertyInfo item in properties) { string name = item.Name; object value = item.GetValue(ainfo,null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { //tStr += string.Format("{0}:{1},", name, value); if (name.ToLower() == key) { tStr =(string)value; break; } } } return tStr; }
/// <summary> /// 替換內容並導出 /// </summary> /// <param name="templetePathandName"></param> /// <param name="saasPathandFile"></param>
/// <param name="eword"></param>
/// <param name="agentinfo"></param> /// <returns></returns>
public void ExportWordForTemplete(string templetePathandName,string saasPathandFile,ExWordValue eword,AgentInfoEntity agentinfo) { //生成WORD程序對象和WORD文檔對象 string p_TemplatePath = templetePathandName; //例如"/templete.doc"; string p_SavePath = saasPathandFile; Application appWord = new Application(); Document doc = new Document();
object oMissing = System.Reflection.Missing.Value;//這是一個疑問
//打開模板文檔,並指定doc的文檔類型 object filename = ""; try { object objTemplate = Server.MapPath(p_TemplatePath); object objDocType = WdDocumentType.wdTypeDocument; object objFalse = false, objTrue = true; doc = (Document)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); //獲取模板中全部的書籤 Bookmarks odf = doc.Bookmarks; //循環全部的書籤,並給書籤賦值 for (int oIndex = 0; oIndex <eword.ItemValue.Count ; oIndex++) { object obDD_Name =eword.ItemValue[oIndex]; doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = getProperties(agentinfo, (string)obDD_Name); //p_TestReportTable.Rows[0][testTablevalues[oIndex]].ToString();//此處Range也是WORD中很重要的一個對象,就是當前操做參數所在的區域 } //第四步 生成word,將當前的文檔對象另存爲指定的路徑,而後關閉doc對象。關閉應用程序 filename = Server.MapPath(saasPathandFile); 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.Threading.ThreadAbortException ex) { 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); }
//導出 string file = filename.ToString(); FileInfo fi = new FileInfo(file); Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; //Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(Path.GetFileName(destFileName),System.Text.Encoding.Default)); Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(file), System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length", fi.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(file); Response.Flush(); Response.End(); }
以上是代碼,可是對於引用的dll它是com組件,若是不對它的權限進行設置將會報錯。
調用COM組件發佈IIS時常見錯誤 80070005解決方案
症狀:
oWordApplic = New Word.Application
當程序運行到這句時出現下面的錯誤:
檢索 COM 類工廠中 CLSID 爲 {000209FF-0000-0000-C000-000000000046} 的組件時失敗,緣由是出現如下錯誤: 80070005。
oWordApplic = New Word.Application
當程序運行到這句時出現下面的錯誤:
檢索 COM 類工廠中 CLSID 爲 {000209FF-0000-0000-C000-000000000046} 的組件時失敗,緣由是出現如下錯誤: 80070005。
解決方法一:
控制面板-》管理工具-》組件服務-》計算機-》個人電腦-》DCom配置-》找到Microsoft Word文檔
以後
單擊屬性打開此應用程序的屬性對話框。
2. 單擊標識選項卡,而後選擇交互式用戶。
3.單擊"安全"選項卡,分別在"啓動和激活權限"和"訪問權限"組中選中"自定義",而後
自定義->編輯->添加ASP.NET帳戶和IUSER_計算機名
* 這些賬戶僅在計算機上安裝有 IIS 的狀況下才存在。
13. 確保容許每一個用戶訪問,而後單擊肯定。
14. 單擊肯定關閉 DCOMCNFG。
解決方法二:
若是上述方法不能解決問題,就應該是權限問題,請嘗試用下面的方法:
在web.config中使用身份模擬,在<system.web>節中加入 <identity impersonate="true" userName="你的用戶名" password="密碼"/>
</system.web>