按照word/Excel模板导出word/excel

最近项目要实现下载打印的功能,想了想,用水晶报表实在是大材小用, 用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>

时间: 2024-10-12 23:34:25

按照word/Excel模板导出word/excel的相关文章

kettle 使用excel模板导出数据

通过excel进行高速开发报表: 建设思路: 1.首先制订相关的execl模板. 2.通过etl工具(kettle)能够高速的 将数据库中的数据按excel模板导出成新的excel就可以. 当中kettle 按excel模板导出excel 能够參考: http://type-exit.org/adventures-with-open-source-bi/2010/12/using-the-excel-writer-step/ 或可在我的资源中找.

C#,WPF使用word模板导出word文档

使用word模板导出word文档,首先需要在word模板中插入书签: 根据创建的书签名和位置,将需要写入的内容插入到word文件中. 需要引用  Microsoft.Office.Interop.Word;在添加引用-程序集中搜索可以找到. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows; usin

Jasper模板导出为Excel、PDF

/** * @Description: 将模板导出为Excel * @author: * @date: 2018年3月22日 下午8:57:24 * @param pagename 模板文件名称 * @param params 导出参数 * @param javaBean 报表数据源实体类 * @param request 请求对象 * @param response 响应对象 * @throws JRException Jasper异常 * @throws IOException IO异常 *

Magicodes.IE之Excel模板导出教材订购表

说明 本教程主要说明如果使用Magicodes.IE.Excel完成教材订购表的Excel模板导出. 要点 本教程使用Magicodes.IE.Excel来完成Excel模板导出 需要通过创建Dto来完成导出 需要按要求准备Excel模板 主要步骤 1.安装包Magicodes.IE.Excel 在本篇教程中,我们仅演示使用Excel来完成学生数据的导入.我们需要在已准备好的工程中安装以下包,参考命令如下所示: Install-Package Magicodes.IE.Excel 2.准备模板

pyhon/excel python导出到excel时的中文乱码问题

昨儿利用python+win32com将网页的表单导出到本地excel,遇到了输出乱码问题,解决方法: 将x改为x.decode('utf-8') setCall('sheet1',row,col,x.decode('utf-8')) 我的部分源码: self.xlBook = self.xlApp.Workbooks.Add() def setCell(self,sheet,row,col,value):#设置单元格的数据 "Set value of one cell" sht =

使用Spire.Doc组件利用模板导出Word文档

以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作,有NPOI,Spire等第三方组件.开始考虑的是NPOI,毕竟它在操作Excel方面还是很强大的:但是不知道是它本身没有,还是我没找到,无法实现利用Word模板的标签插入内容,纯靠代码去生成Word文档,排版是个大问题.最终找到了Spire.Doc组件,轻松实现! Spire的官网地址:https:

利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)

先下载jacob.jar包. 解压后将jacob.dll放到windows/system32以下或\jre\bin以下. 将jacob.jar增加项目. 这样项目的环境基本上搭建完毕,接下来就是书写相关的代码: /** * 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段.Value代表用来替换的值. * word模板中全部要替换的字段(即HashMap中的Key)以特殊字符开头和结尾. * 如:$code$.$date$--.以免执行错误的替换. * 全部要替换为图片

C# Winform Excel的导出,根据excel模板导出数据

namespace dxhbskymDemo { public partial class ExcelForm : DevExpress.XtraEditors.XtraForm { public ExcelForm() { InitializeComponent(); } #region 导出Excel //导出按钮 private void sbtnDaochu_Click(object sender, EventArgs e) { string fileName = "";//要

Net 自定义Excel模板导出数据

转载自:http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Aspos