读取word文档的内容

shmilyatouer 2008-07-28 04:06:33
如题,
strFilePath=GetFilePath();
strFilePath=strFilePath+".doc";
wordPath="/html/"+ViewState["FileName"]+".doc";
fileWord.PostedFile.SaveAs(strFilePath);
上传完文档后,如何读取刚刚上传的文档内容。
用个字符串接可以吗?
string docContent="";//如何实现?
...全文
628 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
od_huang 2011-12-14
  • 打赏
  • 举报
回复
学习了
nvmtest 2011-12-08
  • 打赏
  • 举报
回复
学习!
komei888 2011-10-11
  • 打赏
  • 举报
回复
asponse.words这个有没有实例啊
lqlzxl 2011-08-12
  • 打赏
  • 举报
回复
为啥 放到服务器上 word 拒绝访问
网上说的这种方法,我试了,有的计算机还是拒绝访问,但是的的计算机上是好的
1、在命令行中输入:dcomcnfg,会显示出“组件服务”管理器
2、打开“组件服务-》计算机-》我的电脑-》DCOM 配置”,找到“Microsoft Word文档”,单击右键,选择“属性”
3、在“属性”对话框中单击“安全”选项卡,在“启动和激活权限”处选择“自定义”,再单击右边的”编辑“,在弹出的对话框中添加”ASPNET“(在IIS6中是NETWORD SERVICE)用户,给予”本地启动“和”本地激活“的权限,单击”确定“,关闭”组件服务“管理器。

这是什么原因呢?

shmilyatouer 2008-07-29
  • 打赏
  • 举报
回复
如果能换行就更好了!
shmilyatouer 2008-07-29
  • 打赏
  • 举报
回复
哇,终于可以了。
哥哥,你再看下那个换行的问题是否可以解决呢?
jaasvip 2008-07-29
  • 打赏
  • 举报
回复
我也学习了。。。。
stg609 2008-07-29
  • 打赏
  • 举报
回复
学习!
luoaizhou 2008-07-29
  • 打赏
  • 举报
回复
顶 学习 !!!
wanghui0380 2008-07-29
  • 打赏
  • 举报
回复
ls的代码不太实用啊,因为你需要在服务器安装office并且设置好权限才能用
我的建议是使用aspose.words或Essential Studio里的DocIO控件,使用这两个控件服务端都可以不用装office
虽然是使用第三方的控件,好像体现不出啥技术,不过胜在实用,我个人绝对是实用主义者
  • 打赏
  • 举报
回复
我的webform
你重新建立一个web站点试试

web.config加上那句话了么?

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;
using System.Runtime.InteropServices;


namespace readDoc
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Write(ReadAllFromWord());
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

#region 读取word
/// <summary>
/// 读取word所有文字内容(不包含表格)
/// </summary>
/// <returns>word中的字符内容(纯文本)</returns>
public string ReadAllFromWord()
{
Word.ApplicationClass app = null;
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = @"E:\学习试验项目\ReadFromWordDoc\test.doc";
object readOnly = true;
object isVisible = false;
try
{
app = new Word.ApplicationClass();
doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing);

string textString = "";
//读取全部内容
textString = doc.Content.Text.Trim();
// int ParCount = this.getParCount(doc);//段数
// for (int i = 1 ; i <= ParCount ; i++)
// {
// textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
// }
textString = textString.Replace("\a",""); //替换空串为空。(word中\a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
textString = textString.Replace("\r","\n"); //替换回车为回车换行
return textString;
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
try
{
doc.Close(ref missing, ref missing, ref missing);
}
catch
{}
doc = null;
}
if (app != null)
{
try
{
app.Quit(ref missing, ref missing, ref missing);
}
catch
{}
app = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();

}
}
#endregion

}
}
  • 打赏
  • 举报
回复
我的webform
你重新建立一个web站点试试

web.config加上那句话了么?

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;
using System.Runtime.InteropServices;


namespace readDoc
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Write(ReadAllFromWord());
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

#region 读取word
/// <summary>
/// 读取word所有文字内容(不包含表格)
/// </summary>
/// <returns>word中的字符内容(纯文本)</returns>
public string ReadAllFromWord()
{
Word.ApplicationClass app = null;
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = @"E:\学习试验项目\ReadFromWordDoc\test.doc";
object readOnly = true;
object isVisible = false;
try
{
app = new Word.ApplicationClass();
doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing);

string textString = "";
//读取全部内容
textString = doc.Content.Text.Trim();
// int ParCount = this.getParCount(doc);//段数
// for (int i = 1 ; i <= ParCount ; i++)
// {
// textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
// }
textString = textString.Replace("\a",""); //替换空串为空。(word中\a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
textString = textString.Replace("\r","\n"); //替换回车为回车换行
return textString;
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
try
{
doc.Close(ref missing, ref missing, ref missing);
}
catch
{}
doc = null;
}
if (app != null)
{
try
{
app.Quit(ref missing, ref missing, ref missing);
}
catch
{}
app = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();

}
}
#endregion

}
}
shmilyatouer 2008-07-29
  • 打赏
  • 举报
回复
还有将读取后的文件打印出来,
并没有换行?!
shmilyatouer 2008-07-29
  • 打赏
  • 举报
回复

“/WebApplication3”应用程序中的服务器错误。
--------------------------------------------------------------------------------

分析器错误
说明: 在分析向此请求提供服务所需资源时出错。请检查下列特定分析错误详细信息并适当地修改源文件。

分析器错误信息: 未能加载类型“WebApplication3.Global”。

源错误:


行 1: <%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication3.Global" %>



源文件: c:\inetpub\wwwroot\WebApplication3\global.asax 行: 1




---------------------------------------------
现在报这个错了·
  • 打赏
  • 举报
回复

在web.config的
<authentication mode="Windows" />
节点下面。复制下面这句。添上管理员密码,用户名
<identity impersonate="true" userName="administrator" password="123"/>

如果感觉比较危险,暴露密码。你需要修改iis的用户权限设置。或者提升asp.net权限 试试
shmilyatouer 2008-07-29
  • 打赏
  • 举报
回复
晕,我用vs2005做了测试,
这段代码可以运行,
但是换了vs2003,就不行了,有什么解决的办法吗?
shmilyatouer 2008-07-29
  • 打赏
  • 举报
回复
还是不行喔,你用vs03,然后新建个网站测试下试试。
我的就用03,然后在网站里读取的。
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 shmilyatouer 的回复:]
如果能换行就更好了!
[/Quote]



   textString = textString.Replace("\a","");    //替换空串为空。(word中\a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
textString = textString.Replace("\r","\n"); //替换回车为回车换行



替换成
textString = textString.Replace("\r","<br>"); //替换回车为回车换行

因为上面那两句是我在控制台输出用到的。这里网页输出,就需要替换换行为<br>标签了。
shmilyatouer 2008-07-28
  • 打赏
  • 举报
回复
晕,明天再来测试吧`
  • 打赏
  • 举报
回复
我测试了下,我的放在ntfs也没问题


这样,有可能内存一个进程已经独占打开了word。


重启下看看。不过我的代码是读,没有写


using System; 
using System.Runtime.InteropServices;



namespace ReadFromWordDoc
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//word操作类实例化
string filepath = @"C:\ReadFromWordDoc\test.doc";
OfficeWordOp word = new OfficeWordOp(filepath);


Console.WriteLine(word.ReadAllFromWord());

}
}
}
加载更多回复(7)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧