您的位置:首页 > Web前端 > HTML

(c#) Word文档的上传,保存,检索内容文字,转化成HTML 及(在检索 COM 类工厂 错误 80070005 解决方案)

2011-07-21 11:19 966 查看
下面实现的是由页面上传一个word文档,然后把该文档保存到工程目录的文件夹UploadWord下.
在页面上可以实现搜索word文档里里面是否含有某些文字内容,同时可以把Word文件转化为HTMl页面.
1.上传word:
页面上代码:<asp:FileUpload ID="FU_wd" runat="server" />
点击保存后,保存方法如下:
//文档录入
protected void Button3_Click(object sender, EventArgs e)
{

string newName = "";
string flex = "";
string wjlj = "";
try
{

if (Request.Files.Count > 0)
{
Random ro = new Random();
if (FU_wd.FileName != "")
{
string filename = Path.GetFileName(FU_wd.FileName);
flex = Path.GetExtension(FU_wd.FileName);
string stro = ro.Next(100, 1000000).ToString();
newName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;

//保有存文件到目录下
FU_wd.PostedFile.SaveAs(Server.MapPath("UpLoadWord/") + newName + flex);
wjlj = "UpLoadWord/" + newName + flex;
}
}
string sql = string.Format("insert into zl(xh, wdmc, wdlj)values ('{0}','{1}','{2}')", Guid.NewGuid().ToString(), wdmc, wjlj);
//执行SQL语句,省略.....
Response.Write("<script>alert('保存成功');location.replace(location.href)</script>");
}
catch (Exception)
{
File.Delete(Server.MapPath("UpLoadWord/") + newName + flex);
Response.Write("<script>alert('保存错误');location.replace(location.href)</script>");
}
}

2.搜索word文档里面是否含有需要查询的文字,方法如下,如果含有则返回"true"字条串:
/// <summary>
///查询word文档中是否含有此文字 [引用例如:CkWordCont(Server.MapPath("../UpLoadWord/1.doc"), "火灾")]
/// </summary>
/// <param name="filePath">路径</param>
/// <param name="strKey">文字</param>
/// <returns></returns>
private string CkWordCont(string strKey, string filePath)
{
string result = "";
object MissingValue = Type.Missing;

Microsoft.Office.Interop.Word.Application wp = new Microsoft.Office.Interop.Word.Application();
//遍历每个word文档
object filename = Server.MapPath(filePath);

Microsoft.Office.Interop.Word.Document wd = wp.Documents.Open(ref filename, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue);

int i = 0, iCount = 0;
Microsoft.Office.Interop.Word.Find wfnd;
if (wd.Paragraphs != null && wd.Paragraphs.Count > 0)
{
iCount = wd.Paragraphs.Count;

wfnd = wd.Paragraphs[i].Range.Find;
wfnd.ClearFormatting();
wfnd.Text = strKey;
if (wfnd.Execute(ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue))
{
result ="true";
break;
}
//关闭word文档.
wd.Close(ref MissingValue, ref MissingValue, ref MissingValue);
wp.Quit(ref MissingValue, ref MissingValue, ref MissingValue);
}
return result;
}

3.把word文档转化成HTMl文件,方法如下:

/// <summary>
/// word转成html[把word文档换成同名的html文件,并返回其路径]
/// </summary>
/// <param name="wordFileName"></param>
private string WordToHtml(object wordFileName)
{
//在此处放置用户代码以初始化页面
ApplicationClass word = new ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;
//打开文件
Type docsType = docs.GetType();
Document doc = (Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
//转换格式,另存为
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = (object)strSaveFileName;

docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
return saveFileName.ToString();

}
以上方法为例:在通过WordToHtml()方法之后,获得html文件的路径HtmlPath:
举例:

string HtmlPath = WordToHtml(Server.MapPath("UpLoadWord/2416125839513.doc"));
string rePath = HtmlPath.Substring(HtmlPath.IndexOf("UpLoadWord\\") + 11, HtmlPath.Length - HtmlPath.IndexOf("UpLoadWord\\") - 11);
Response.Redirect("UpLoadWord/" + rePath);

通过路径获得文件,然后中转到页面.
-----------------------------------------------------------归总--------------------------------------------------------

这里做一下补充: 1.word的相关操作需要引用DDL文件:Microsoft.Office.Interop.Word.dll 2.word文字检索的方法可以写到一个单独的类里面: 如可定义一个类:WordHelper.cs: 操作方法如下:
/// <summary>
///查询word文档中是否含有此文字
/// </summary>
/// <param name="filename">路径</param>
/// <param name="strKey">文字</param>
/// <returns></returns>
public static bool CkWordCont(string strKey, string filename, HttpServerUtility server)
{
bool flag = false;
object MissingValue = Type.Missing;

Microsoft.Office.Interop.Word.Application wp = new Microsoft.Office.Interop.Word.Application();
//遍历word文档
//object filename = Server.MapPath(nrdt.Rows[j]["wdlj"].ToString());
object filepath = server.MapPath(filename);
Microsoft.Office.Interop.Word.Document wd = wp.Documents.Open(ref filepath, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue);

int i = 0, iCount = 0;
Microsoft.Office.Interop.Word.Find wfnd;
if (wd.Paragraphs != null && wd.Paragraphs.Count > 0)
{
iCount = wd.Paragraphs.Count;
for (i = 1; i <= iCount; i++)
{
wfnd = wd.Paragraphs[i].Range.Find;
wfnd.ClearFormatting();
wfnd.Text = strKey;
if (wfnd.Execute(ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue))
{
flag = true;
break;
}
}
//关闭文档
wd.Close(ref MissingValue, ref MissingValue, ref MissingValue);
wp.Quit(ref MissingValue, ref MissingValue, ref MissingValue);
}
return flag;
}
其中 因为HttpServerUtility类的Server对象不能写在静态方法里,所有把它作为静态方法的参数引用.在页面中调用如下:WordHelper.CkWordCont("关键字","upload/a.doc", Server)
3.程序在自己本地机器上调试的时候,要把本地存放文件的文件夹设成共享.
如果放到服务器上的话,可能出现以下错误:
在检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败时错误: 80070005
解决方案如下:
1.控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft word文档之后,单击属性打开此应用程序的属性对话框。
  2. 单击标识选项卡,然后选择交互式用户。  3.单击“安全”选项卡,分别在“启动和激活权限”和“访问权限”组中选中“自定义”,然后   自定义->编辑->添加ASP.NET账户和IUSER_计算机名(或USER用户)[注:两个用户均需要添加上]   ( 这些帐户仅在计算机上安装有 IIS 的情况下才存在。) 4. 确保允许每个用户访问,然后单击确定。  5. 单击确定关闭 DCOMCNFG。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐