62,041
社区成员
发帖
与我相关
我的任务
分享
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
}
}
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
}
}
“/WebApplication3”应用程序中的服务器错误。
--------------------------------------------------------------------------------
分析器错误
说明: 在分析向此请求提供服务所需资源时出错。请检查下列特定分析错误详细信息并适当地修改源文件。
分析器错误信息: 未能加载类型“WebApplication3.Global”。
源错误:
行 1: <%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication3.Global" %>
源文件: c:\inetpub\wwwroot\WebApplication3\global.asax 行: 1
textString = textString.Replace("\a",""); //替换空串为空。(word中\a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
textString = textString.Replace("\r","\n"); //替换回车为回车换行
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());
}
}
}