最近項目要實現下載列印的功能,想了想,用水晶報表實在是大材小用, 用office組件直接就可以實現這一功能。
引用類 using Microsoft.Office.Interop.Word;
建立兩個實體類 一個是匯出word需要替換內容的配置,一個是替換內容的實體
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; }
}
}
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>