用户登录
用户注册

分享至

asp.networdset_sty

  • 作者: 左晅同学
  • 来源: 51数据库
  • 2020-04-21

asp.net中读取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里加 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应用程序,位置为“http://localhost/word”。

(如图一) 二、引用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源码如下:using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace word {///

/// Webform1 的摘要说明。

///

public class Webform1 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox SaveAs; protected System.Web.UI.WebControls.Button Button; 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.TextBox wordText;#region Web form Designer generated code override protected void OnInit(EventArgs e) {//// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。// InitializeComponent(); base.OnInit(e); }/// /// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。

///

private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); }#endregion public void Button_Click(object sender, System.EventArgs e) { Object Nothing=System.Reflection.Missing.value;//取得Word文件保存路径 object filename=@SaveAs.Text;//创建一个名为WordApp的组件对象 Word.Application WordApp=new Word.ApplicationClass();//创建一个名为WordDoc的文档对象 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文档对象的内容保存为DOC文档 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文档对象 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);//关闭WordApp组件对象 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);//返回结果 result.Text="文档路径:"+SaveAs.Text+"(点击链接查看)生成结果:成功!"; } private void Page_Load(object sender, System.EventArgs e) { } } } 四、Webform1.aspx代码 完成CS源码后,我们就可以设计Webform页面了,完整的代码如下:<%@ Page language="c#" Codebehind="Webform1.aspx.cs" AutoEventWireup="false" Inherits="word.Webform1" %>基于Webforms的操作Word。

如何将Asp.net页面导入到World里

有图片就没试过哦!下面是关键性代码,但只是生成文字!希望对您有用添加引用:using Microsoft.Office.Interop.Word; //============================ private unsafe void ThreadWork() { try { this.SetBtnDisable(); string str = "数据库名:" + this.dbname; int count = this.listTable2.Items.Count; this.SetprogressBar1Max(count); this.SetprogressBar1Val(1); this.SetlblStatuText("0"); object template = Missing.Value; object obj3 = @"\endofdoc"; Microsoft.Office.Interop.Word._Application application = new Microsoft.Office.Interop.Word.ApplicationClass(); application.Visible = false; Microsoft.Office.Interop.Word._Document document = application.Documents.Add(ref template, ref template, ref template, ref template); application.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView; application.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; application.ActiveWindow.ActivePane.Selection.InsertAfter("导出到Word"); application.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; application.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; Microsoft.Office.Interop.Word.Paragraph paragraph = document.Content.Paragraphs.Add(ref template); paragraph.Range.Text = str; paragraph.Range.Font.Bold = 1; paragraph.Range.Font.Name = "宋体"; paragraph.Range.Font.Size = 12f; paragraph.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph.Format.SpaceAfter = 5f; paragraph.Range.InsertParagraphAfter(); for (int i = 0; i columnInfoList = this.dbobj.GetColumnInfoList(this.dbname, tableName); int num3 = columnInfoList.Count; if ((columnInfoList != null) && (columnInfoList.Count > 0)) { object obj4 = document.Bookmarks.get_Item(ref obj3).Range; Microsoft.Office.Interop.Word.Paragraph paragraph2 = document.Content.Paragraphs.Add(ref obj4); paragraph2.Range.Text = str3; paragraph2.Range.Font.Bold = 1; paragraph2.Range.Font.Name = "宋体"; paragraph2.Range.Font.Size = 10f; paragraph2.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph2.Format.SpaceBefore = 15f; paragraph2.Format.SpaceAfter = 1f; paragraph2.Range.InsertParagraphAfter(); obj4 = document.Bookmarks.get_Item(ref obj3).Range; Microsoft.Office.Interop.Word.Paragraph paragraph3 = document.Content.Paragraphs.Add(ref obj4); paragraph3.Range.Text = ""; paragraph3.Range.Font.Bold = 0; paragraph3.Range.Font.Name = "宋体"; paragraph3.Range.Font.Size = 9f; paragraph3.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph3.Format.SpaceBefore = 1f; paragraph3.Format.SpaceAfter = 1f; paragraph3.Range.InsertParagraphAfter(); Microsoft.Office.Interop.Word.Range range = document.Bookmarks.get_Item(ref obj3).Range; Microsoft.Office.Interop.Word.Table table = document.Tables.Add(range, num3 + 1, 10, ref template, ref template); table.Range.Font.Name = "宋体"; table.Range.Font.Size = 9f; table.Borders.Enable = 1; table.Rows.Height = 10f; table.AllowAutoFit = true; table.Cell(1, 1).Range.Text = "序号"; table.Cell(1, 2).Range.Text = "列名"; table.Cell(1, 3).Range.Text = "数据类型"; table.Cell(1, 4).Range.Text = "长度"; table.Cell(1, 5).Range.Text = "小数位"; table.Cell(1, 6).Range.Text = "标识"; table.Cell(1, 7).Range.Text = "主键"; table.Cell(1, 8).Range.Text = "允许空"; table.Cell(1, 9).Range.Text = "默认值"; table.Cell(1, 10).Range.Text = "说明"; table.Columns[1].Width = 33f; table.Columns[3].Width = 60f; table.Columns[4].Width = 33f; table.Columns[5].Width = 43f; table.Columns[6].Width = 33f; table.Columns[7].Width = 33f; table.Columns[8].Width = 43f; for (int j = 0; j 评论0 0 0。

如何用ASP读取word文档中的内容(表格+文字)然后写入数据库-百度

一改不是asp读取把,asp一般只负责做前台界面,后台处理需要其他语言,如C语言,C++,java等。

不知道你用的哪种语言。下面这一段代码是打开word的,你可以参考一下<% response.ContentType="application/msword" Const adTypeBinary = 1 Dim strFilePath strFilePath = "E:\website\test\doc.doc" Set objStream = Server.CreateObject("ADODB.Stream") objStream.Open objStream.Type = adTypeBinary objStream.LoadFromFile strFilePath Response.BinaryWrite objStream.Read objStream.Close Set objStream = Nothing %>。

ASP.NET读取word文档并在ASPX网页中显示,您解决了吗?能不能告

string path = "Word/" + model.vc_Word;//文档路径

using (Package package = Package.Open(Server.MapPath(path)))

{

Uri docxUri = new Uri("/word/document.xml", UriKind.Relative);

PackagePart docxPart = package.GetPart(docxUri);

XmlDocument docxXmlDocument = new XmlDocument();

docxXmlDocument.Load(docxPart.GetStream());

div_content.InnerHtml = docxXmlDocument.InnerText.ToString();//显示

}

我也在研究这个问题,你试试

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 = "<html><head><title>;无标题文档</title></head><body>;这里放从数据库导出的word文档内容</body></html>";

//写入

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页面导入到World里

有图片就没试过哦!下面是关键性代码,但只是生成文字!希望对您有用添加引用:using Microsoft.Office.Interop.Word; //============================ private unsafe void ThreadWork() { try { this.SetBtnDisable(); string str = "数据库名:" + this.dbname; int count = this.listTable2.Items.Count; this.SetprogressBar1Max(count); this.SetprogressBar1Val(1); this.SetlblStatuText("0"); object template = Missing.Value; object obj3 = @"\endofdoc"; Microsoft.Office.Interop.Word._Application application = new Microsoft.Office.Interop.Word.ApplicationClass(); application.Visible = false; Microsoft.Office.Interop.Word._Document document = application.Documents.Add(ref template, ref template, ref template, ref template); application.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView; application.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; application.ActiveWindow.ActivePane.Selection.InsertAfter("导出到Word"); application.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; application.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; Microsoft.Office.Interop.Word.Paragraph paragraph = document.Content.Paragraphs.Add(ref template); paragraph.Range.Text = str; paragraph.Range.Font.Bold = 1; paragraph.Range.Font.Name = "宋体"; paragraph.Range.Font.Size = 12f; paragraph.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph.Format.SpaceAfter = 5f; paragraph.Range.InsertParagraphAfter(); for (int i = 0; i < count;="" i++)="" {="" string="" tablename="this.listTable2.Items[i].ToString();" string="" str3="表名:" +="" tablename;=""> columnInfoList = this.dbobj.GetColumnInfoList(this.dbname, tableName); int num3 = columnInfoList.Count; if ((columnInfoList != null) && (columnInfoList.Count > 0)) { object obj4 = document.Bookmarks.get_Item(ref obj3).Range; Microsoft.Office.Interop.Word.Paragraph paragraph2 = document.Content.Paragraphs.Add(ref obj4); paragraph2.Range.Text = str3; paragraph2.Range.Font.Bold = 1; paragraph2.Range.Font.Name = "宋体"; paragraph2.Range.Font.Size = 10f; paragraph2.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph2.Format.SpaceBefore = 15f; paragraph2.Format.SpaceAfter = 1f; paragraph2.Range.InsertParagraphAfter(); obj4 = document.Bookmarks.get_Item(ref obj3).Range; Microsoft.Office.Interop.Word.Paragraph paragraph3 = document.Content.Paragraphs.Add(ref obj4); paragraph3.Range.Text = ""; paragraph3.Range.Font.Bold = 0; paragraph3.Range.Font.Name = "宋体"; paragraph3.Range.Font.Size = 9f; paragraph3.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph3.Format.SpaceBefore = 1f; paragraph3.Format.SpaceAfter = 1f; paragraph3.Range.InsertParagraphAfter(); Microsoft.Office.Interop.Word.Range range = document.Bookmarks.get_Item(ref obj3).Range; Microsoft.Office.Interop.Word.Table table = document.Tables.Add(range, num3 + 1, 10, ref template, ref template); table.Range.Font.Name = "宋体"; table.Range.Font.Size = 9f; table.Borders.Enable = 1; table.Rows.Height = 10f; table.AllowAutoFit = true; table.Cell(1, 1).Range.Text = "序号"; table.Cell(1, 2).Range.Text = "列名"; table.Cell(1, 3).Range.Text = "数据类型"; table.Cell(1, 4).Range.Text = "长度"; table.Cell(1, 5).Range.Text = "小数位"; table.Cell(1, 6).Range.Text = "标识"; table.Cell(1, 7).Range.Text = "主键"; table.Cell(1, 8).Range.Text = "允许空"; table.Cell(1, 9).Range.Text = "默认值"; table.Cell(1, 10).Range.Text = "说明"; table.Columns[1].Width = 33f; table.Columns[3].Width = 60f; table.Columns[4].Width = 33f; table.Columns[5].Width = 43f; table.Columns[6].Width = 33f; table.Columns[7].Width = 33f; table.Columns[8].Width = 43f; for (int j = 0; j < num3;="" j++)="" {="" columninfo="" info="columnInfoList[j];" string="" colorder="info.Colorder;" string="" columnname="info.ColumnName;" string="" typename="info.TypeName;" string="" length="info.Length;" string="" scale="info.Scale;" string="" str9="(info.IsIdentity.ToString().ToLower()" =="true" )="" ?="" "是"="" :="" "";="" string="" str10="(info.IsPK.ToString().ToLower()" =="true" )="" ?="" "是"="" :="" "";="" string="" str11="(info.cisNull.ToString().ToLower()" =="true" )="" ?="" "是"="" :="" "否";="">

转载请注明出处51数据库 » asp.networdset_sty

软件
前端设计
程序设计
Java相关