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我是否可以解决您的问题?
asp.net编程,获得word文档页数
通过SOAOffice客户端控件上传word文档,在页面里添加隐藏域在页面的js保存按钮事件里添脚本document.getElementById("pgCount").value = SOAOfficeCtrl.Document.BuiltInDocumentProperties(14).Value;在保存页面SaveDoc.aspx里调用iPageCount = int32.Parse(SaveObj.RequestFormField("pgCount"));iPageCount就是你要的上传的文档的页数
asp.net 如何调用客户端word程序打开服务器上的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转换为html文件,并在asp页面显示html
这个是可以实现的,WORD 转 HTML 就是按照WORD的排版,平移到 HTML上。
同时,HTML 转 WORD 也很方便的,只需要把HTML的样式,写入WORD即可。
这里需要注意,WORD 不支持DIV等标签,仅仅支持TABLE这些标签,写程序的时候注意样式和标签的改变哈。
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.WdExportFormat这个属性是需要引用哪个命名空间呢...
using Word = Microsoft.Office.Interop.Word;using Excel = Microsoft.Office.Interop.Excel;using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Core;Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
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; }
转载请注明出处51数据库 » asp.net wps word