用asp.net网页显示word和excel文档,不知如何运用下面网上发现的代码,,求大神赐教
已经在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 });
怎么用啊怎么用啊???
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 });
怎么用啊怎么用啊???
...全文
相关推荐
通用ASP.NET Core NPOI导出复杂Word、Excel和Excel数据导入实战项目
简单,易懂的ASP.NET Core NPOI导出复杂Word、Excel和Excel数据导入示例讲解
亲测,以html形式显示或预览word,excel,pdf,txt
本源码主要是ASP.net后台通过相应的类把word\excel\pdf\txt文件转化为html,将地址传回前端,前端获取地址,用iframe进行展现预览。
基于ASP.NET MVC项目源码在线预览Word、Excel、PPT、PDF文档
基于ASP.NET MVC框架的学习项目,包含MVC的框架的设计思路,通俗易懂学习MVC框架,项目中主要使用了Aspose控件通过pdfobject.js实现在线预览功能,快速易懂展示在线预览的精髓。需要VS2012版本及以上版本可以打开。VS2010版本及以及以上版本的源码链接地址:http://download.csdn.net/download/u012949335/10231812,讲解更加详细。
Asp.net mvc 在线预览、在线预览Word、Excel、PDF等
在线预览PDF、Word、Txt等文本文件 Microsoft.Office.Interop.Word Microsoft.Office.Interop.Excel
以html形式显示或预览word,excel,pdf,txt
本源码主要是ASP.net后台通过相应的类把word\excel\pdf\txt文件转化为html,将地址传回前端,前端获取地址,用iframe进行展现预览。



