用asp.net网页显示word和excel文档,不知如何运用下面网上发现的代码,,求大神赐教

dingyet 2012-10-12 11:42:46
已经在E盘添加了12345.文件,aspx页面不知要加什么控件,该cs文件只是更改了一些引用后,删了word部分调试,页面空白。怎么用啊??


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Excel;
using System.Diagnostics;
using Microsoft;
using Word = Microsoft.Office.Interop.Word;



public partial class tel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//word
Response.Write(Get_Html("New.doc"));
//excel
ExcelConvertToHtml("E:\\12345.xls", "E:\\aaa.html");
}

protected void ExcelConvertToHtml(string xlsPath, string htmlPath)
{
try
{
Excel.Application app = new Excel.Application();
app.Visible = false;
Object o = Missing.Value;

/// _Workbook xls=app.Workbooks.Open(xlsPath,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);

_Workbook xls = app.Workbooks.Open(xlsPath, o, o, o, o, o, o, o, o, o, o, o, o);
object fileName = htmlPath;
object format = Excel.XlFileFormat.xlHtml;//Html

// xls.SaveAs(ref fileName,ref format,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
xls.SaveAs(fileName, format, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o);
//xls.SaveAs(
object t = true;
app.Quit();

Process[] myProcesses = Process.GetProcessesByName("EXCEL");
foreach (Process myProcess in myProcesses)
{
myProcess.Kill();
}
}
catch (Exception ex)
{
System.Console.Write(ex.Message);
// MessageBox.Show(ex.Message);
}
}
private string Get_Html(string FileName)
{

Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;

// 打开文件
Type docsType = docs.GetType();
// RAID.doc
// string newFileNamePath = this.Server.MapPath(mydata.Config.SysUploadPath + "/" + FileName);
string newFileNamePath = this.Server.MapPath("download/" + FileName);
Word.Document doc = (Word.Document)docsType.InvokeMember

("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { newFileNamePath, true,

true });

// 转换格式,另存为
Type docType = doc.GetType();
object saveFileName = newFileNamePath.Replace(".doc", ".htm");
//下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
/*
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
*/
///其它格式:
///wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatHTML });

// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

string newFileName = FileName.Replace(".doc", ".htm");
return (newFileName);
}

}




有word部分调试,出现显示下面代码调用目标发现异常:
Word.Document doc = (Word.Document)docsType.InvokeMember

("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { newFileNamePath, true,

true });

怎么用啊怎么用啊???
...全文
330 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-10-12
  • 打赏
  • 举报
回复
删了word部分是什么意思?你的代码是使用word存储成html内容的,你删除了word还怎么转呢
dingyet 2012-10-12
  • 打赏
  • 举报
回复
又发现一个问题,每次新建一个12345.xls文件,第一次运行时总会显示,

Response.WriteFile("E:\\aaa.html");
文件“E:\aaa.html”正由另一进程使用,因此该进程无法访问此文件。
怎么解决?
dingyet 2012-10-12
  • 打赏
  • 举报
回复
同时,我发现删除了一下一段代码依然可以运行,什么原因,这段代码是干什么的,如何运用?

private string Get_Html(string FileName)
{

Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;

// 打开文件
Type docsType = docs.GetType();
// RAID.doc
// string newFileNamePath = this.Server.MapPath(mydata.Config.SysUploadPath + "/" + FileName);
string newFileNamePath = this.Server.MapPath("download/" + FileName);
Word.Document doc = (Word.Document)docsType.InvokeMember

("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { newFileNamePath, true,

true });

// 转换格式,另存为
Type docType = doc.GetType();
object saveFileName = newFileNamePath.Replace(".doc", ".htm");
//下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
/*
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
*/
///其它格式:
///wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatHTML });

// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

string newFileName = FileName.Replace(".doc", ".htm");
return (newFileName);
}
dingyet 2012-10-12
  • 打赏
  • 举报
回复
刚调试了几次,excel文件英文和数字显示正常,中文显示乱码,如何解决
孟子E章 2012-10-12
  • 打赏
  • 举报
回复
在前台操作无需操作。

首先,你要清楚。浏览器是不能直接显示doc,xls的
其次,你可以使用Silverlight,好像可以转换,但客户端需要安装office
另外,进行转换成html显示是一种方法,

另外,还可以转成pdf或者swf进行显示
dingyet 2012-10-12
  • 打赏
  • 举报
回复
net_lover
如果要实现在网页上找到该文件,点击文件在网页显示出这个文档,怎么在前台操作
dingyet 2012-10-12
  • 打赏
  • 举报
回复
哦,我是一开始不会在帖中添加代码,格式不好看,所以发多一次
孟子E章 2012-10-12
  • 打赏
  • 举报
回复
不要重复相同内容的发帖
孟子E章 2012-10-12
  • 打赏
  • 举报
回复
//word
Response.Write(Get_Html("New.doc"));
//excel
ExcelConvertToHtml("E:\\12345.xls", "E:\\aaa.html");

改成

ExcelConvertToHtml("E:\\12345.xls", "E:\\aaa.html");
Response.WriteFile("E:\\aaa.html");
dingyet 2012-10-12
  • 打赏
  • 举报
回复
现在我只有这段后台代码,前台界面什么都没有设置(不知咋设置。。),不知道怎么用啊。。。
dingyet 2012-10-12
  • 打赏
  • 举报
回复
因为运行全部的话,会出现以下情况:

Word.Document doc = (Word.Document)docsType.InvokeMember

("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { newFileNamePath, true,

true });

这段代码调试的目标出现异常
system.reflection.targetinvocationexception {"调用的目标发生了异常。"}
dingyet 2012-10-12
  • 打赏
  • 举报
回复
删了word部分就是只运行以下部分

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Excel;
using System.Diagnostics;
using Microsoft;
using Word = Microsoft.Office.Interop.Word;



public partial class tel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//word
Response.Write(Get_Html("New.doc"));
//excel
ExcelConvertToHtml("E:\\12345.xls", "E:\\aaa.html");
}

protected void ExcelConvertToHtml(string xlsPath, string htmlPath)
{
try
{
Excel.Application app = new Excel.Application();
app.Visible = false;
Object o = Missing.Value;

/// _Workbook xls=app.Workbooks.Open(xlsPath,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);

_Workbook xls = app.Workbooks.Open(xlsPath, o, o, o, o, o, o, o, o, o, o, o, o);
object fileName = htmlPath;
object format = Excel.XlFileFormat.xlHtml;//Html

// xls.SaveAs(ref fileName,ref format,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
xls.SaveAs(fileName, format, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o);
//xls.SaveAs(
object t = true;
app.Quit();

Process[] myProcesses = Process.GetProcessesByName("EXCEL");
foreach (Process myProcess in myProcesses)
{
myProcess.Kill();
}
}
catch (Exception ex)
{
System.Console.Write(ex.Message);
// MessageBox.Show(ex.Message);
}
}

}


61,657

社区成员

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

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

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

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