ASP.NET(C#):如何利用word模板dot文件生成报表。
ASP.NET利用dot模板生成Word文档代码,假定有一个模板文件位于网站Mywebsite/templates/a.dot,现在需要利用Word.ApplicationClass和Word.Document类来生成一个word文档。
关键代码如下: //初始化生成word文档需要的信息 Word.Application appWord =null; Word.Document doc = null; try { appWord = new Word.ApplicationClass(); appWord.Visible = false; object objTrue = true; object objFalse = false; object objTemplate = Server.MapPath(@"templates\a.dot");//模板路径 object objDocType = Word.WdDocumentType.wdTypeDocument; doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); //第一步生成word文档 //定义书签变量 object obDD_Name = "DD_Name";//姓 名 object obDD_Sex = "DD_Sex";//性 别 object obDD_Age = "DD_Age";//年龄 object obDD_Birthday = "DD_Birthday"; //出生年月 object obDD_Nation = "DD_Nation"; //民 族 object obDD_Native = "DD_Native"; //籍 贯 //第二步 读取数据,填充数据集 SqlDataReader dr = XXXXX;//读取出来的数据集 //第三步 给书签赋值 //给书签赋值 doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = dr["appliername"].ToString(); //姓 名 doc.Bookmarks.get_Item(ref obDD_Sex).Range.Text = getCodeFullNameStd("性别", dr["Sex"].ToString());//性 别 doc.Bookmarks.get_Item(ref obDD_Age).Range.Text = Convert.ToString(DateTime.Now.Year - ((DateTime)dr["Birthday"]).Year);//年龄 . //第四步 生成word object filename = Server.MapPath("file") + "\\" + "XXXXXXXXXX情况表" +".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 = Word.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 e) { //捕捉异常,如果出现异常则清空实例,退出word,同时释放资源 string aa = e.ToString(); object miss = System.Reflection.Missing.Value; object missingValue = Type.Missing; object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue); appWord.Application.Quit(ref miss, ref miss, ref miss); doc = null; appWord = null; }
asp.net导出word
public void Download(){Random rd = new Random();string fileName = DateTime.Now.ToString("yyyyMMddhhmm") + rd.Next() + ".doc";//存储路径string path = Server.MapPath(fileName);//创建字符输出流StreamWriter sw = new StreamWriter(path, true, System.Text.UnicodeEncoding.UTF8);//需要导出的内容string str = "无标题文档这里放从数据库导出的word文档内容";//写入sw.Write(str);sw.Close();Response.Clear();Response.Buffer = true;this.EnableViewState = false;Response.Charset = "utf-8";Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));Response.ContentType = "application/octet-stream";Response.WriteFile(path);Response.Flush();Response.Close();Response.End();}标准word文档的格式微软暂未公布,由此我们可将需要导出的内容转为标准HTML文件储存,后缀名为.doc也可以将要导出内容转为标准XML格式存储,改后缀为.doc具体格式随意新建个word文档,输入内容,另存为.XML可见另外一种导出方式为word导出标准格式,服务器需要安装Microsoft Office word,需要预先设置好一个word文档并在要插入内容的地方设置书签做为模版,导出word文档时需要先遍历模版文件中的所有书签,然后给书签赋值就能实现导出数据了还有不懂的可以直接百度HI我是否可以解决您的问题?
IIS下asp.net调用com组建操作和生成新的word文件问题
操作WORD配置说明引入:Word的对象库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB)1.运行Dcomcnfg.exe 2.组件服务――计算机――我的电脑――DCOM配置――找到microsoft word 文档 3.点击属性 4.选择“安全性” 5.选定“使用自定义访问权限”和“使用自定义启动权限” 6.分别编辑权限,添加Everyone(ASPNET,VS Developers,Debugger User)7.选择“身份标识”,在选定“交互式用户” 即可 8.在Web.config里加 identity impersonate="true"/ C#:ASP.NET操作Word文档一直是一个大家比较关心的话题,其实在ASP.NET里操作Word文档一点也不难,大家只需按本文提示,就能轻轻松松操作Word文档!一、准备工作 首先请确认服务端已经安装了Office Word(以下将以Office XP为例),操作系统为win2000或XP,并且已配置好.NET的运行环境及安装VS.NET C#开发环境后,我们就可以打开VS.NET,并新建一个Visual C#项目ASP.NET Web应用程序,位置为“”。
(如图一)二、引用Word对象库文件 要操作Word,我们就需要Word的对象库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB),通常安装了Office Word后,你就可以在office安装目录的Office10文件夹下面找到这个文件,当我们将这个文件引入到项目后,我们就可以在源码中使用各种操作函数来操作Word。
具体做法是打开菜单栏中的项目添加引用浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,vs.net将会自动将库文件转化为DLL组件,这样我们只要在源码中创建该组件对象即可达到操作Word的目的! 答案补充 三、Webform1.aspx.cs代码 完成添加引用后,MSWORD.OLB已经转化为相关DLL文件并放置于项目的BIN目录下了,这样我们只需在源码中创建该对象,并使用word库文件内置的操作函数即可轻松实现操作Word,Webform1.aspx.cs源码请参见五、web.config设置 web.config文件还需添加一句 identity impersonate="true"/以启用模拟身份,因为默认ASPNET这个用户是没有权限访问Word.ApplicationClass(),当启用模拟身份后所有页面将会使用匿名Internet用户帐户(IUSR_machinename)这个用户名的权限执行,这样我们就能成功访问Word.ApplicationClass()并在ASP.NET中操作Word!//传文档所在路径 返回文档内容public string Doc2Text(string docFileName) { //实例化COM Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); object fileobj = docFileName; object nullobj = System.Reflection.Missing.Value; //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了) Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj ); //取得doc文件中的文本 string outText = doc.Content.Text; //关闭文件 doc.Close(ref nullobj, ref nullobj, ref nullobj); //关闭COM wordApp.Quit(ref nullobj, ref nullobj, ref nullobj); //返回 return outText; } 当然 在读取的时候会有损坏的文件 和被加密的文件等问题 总之C#和office的兼容性不太好别忘了要引用word的dll引用文件夹 右键添加引用 在组件里找Microsoft.Office.Interop.Word
如何生成word文档的源码
/// public class WebForm1 : System.Web.UI.Page{protected System.Web.UI.WebControls.TextBox SaveAs;protected System.Web.UI.WebControls.Label Label2;protected System.Web.UI.WebControls.Label Label1;protected System.Web.UI.WebControls.Label result;protected System.Web.UI.WebControls.Button Button1;protected System.Web.UI.WebControls.TextBox wordText;private void Page_Load(object sender, System.EventArgs e){// 在此处放置用户代码以初始化页面}#region Web 窗体设计器生成的代码override protected void OnInit(EventArgs e){//// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//InitializeComponent();base.OnInit(e);}/// /// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。
/// private void InitializeComponent(){this.Button1.Click += new System.EventHandler(this.Button1_Click);this.Load += new System.EventHandler(this.Page_Load);}#endregionprivate void Button1_Click(object sender, System.EventArgs e){Object Nothing=System.Reflection.Missing.Value;object filename=@SaveAs.Text;Word.Application WordApp=new Word.ApplicationClass();Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing);table.Cell(1,1).Range.Text=wordText.Text;WordDoc.Paragraphs.Last.Range.Text="Wellcome To Aspxcn.Com";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);WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);result.Text="文档路径:"+SaveAs.Text+"(点击链接查看)生成结果:成功!";}}}在web.config中要设置权限。
asp.net 在页面上显示本地的word文档里的内容。
网上方法不少,可以尝试搜索一下。
第一种方法:Response.ClearContent();Response.ClearHeaders();Response.ContentType = "Application/msword";string s=Server.MapPath("E:/wendang/wo582.doc");Response.WriteFile("E:/wendang/wo582.doc");Response.Write(s);Response.Flush();Response.Close();第二种方法:Response.ClearContent();Response.ClearHeaders();Response.ContentType = "Application/msword"; string strFilePath=""; strFilePath =Server.MapPath("E:/wendang/wo582.doc"); FileStream fs = new FileStream(strFilePath,FileMode.OpenOrCreate,FileAccess.Read);Response.WriteFile(strFilePath,0,fs.Length);fs.Close(); 第三种方法:string path=Server.MapPath("E:/wendang/wo582.doc");FileInfo file=new FileInfo(path);FileStream myfileStream=new FileStream(path,FileMode.Open,FileAccess.Read);byte[] filedata=new Byte[file.Length];myfileStream.Read(filedata,0,(int)(file.Length));myfileStream.Close();Response.Clear();Response.ContentType="application/msword";Response.AddHeader("Content-Disposition","attachment;filename=wo582.doc");Response.Flush();Response.BinaryWrite(filedata);Response.End();
asp.net中,把word文档转为PDF格式文件的问题。
C#编程,将Word转PDF,该方法有一定弊端,但是比起网路上的其他方法来说,还算比较好的,弊端是需要客户机上安装有WORD 2007或更高的版本。
1、添加引用: Microsoft.Office.Interop.Word版本12.0.0.0; 2、在开头添加命名空间引用:using Microsoft.Office.Interop.Word; 3、具体实现方法如下: //Word转换成pdf /// /// 把Word文件转换成为PDF格式文件 /// ///源文件路径 ///目标文件路径 ///true=转换成功 privatebool WordToPDF(string sourcePath, string targetPath) { bool result = false; Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF; object paramMissing = Type.Missing; Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document wordDocument = null; try { object paramSourceDocPath = sourcePath; string paramExportFilePath = targetPath; Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat; bool paramOpenAfterExport = false; Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint; Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument; int paramStartPage = 0; int paramEndPage = 0; Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent; bool paramIncludeDocProps = true; bool paramKeepIRM = true; Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; bool paramDocStructureTags = true; bool paramBitmapMissingFonts = true; bool paramUseISO19005_1 = false; wordDocument = wordApplication.Documents.Open( ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); if (wordDocument != null) wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); result = true; if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null; } if (wordApplication != null) { wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } catch { result = false; if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null; } if (wordApplication != null) {
asp.net如何下载已经上传到数据库中的Word文档(C#)
这个是可以实现的,WORD 转 HTML 就是按照WORD的排版,平移到 HTML上。
同时,HTML 转 WORD 也很方便的,只需要把HTML的样式,写入WORD即可。
这里需要注意,WORD 不支持DIV等标签,仅仅支持TABLE这些标签,写程序的时候注意样式和标签的改变哈。
转载请注明出处51数据库 » asp.net 生成word文档