word,ppt,excel转换为html
因为最近上班做邮箱附件的预览功能,上网找到的解决方案是转为html文件,自己总结了一下:
1.先添加office组建,右键网站,点击添加引用
2.选择.NET找到你需要的组件,例如Microsoft.Office.Interop.Excel(版本为12.0.0.0),我这里用的还有ppt,word,三个都添加上
3.开始写代码
View Code 1 using System;2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using word = Microsoft.Office.Interop.Word;//引入对应的组件
8 using System.IO;
9 using ppt = Microsoft.Office.Interop.PowerPoint;
10 using Microsoft.Office.Core;
11 using Microsoft.Office.Interop;
12
13 namespace Web.MailManage
14 {
15 public partial class WordShow : System.Web.UI.Page
16 {
17 protected void Page_Load( object sender, EventArgs e)
18 { string newurl = string.Empty; newurl = wordtohtml(filename);//转换word文档
Response.Redirect(filename);//跳转到转换后的html文件,这里要注意一样要路径正确,否则没有效果,也就是看到转换的文件在什么路径
19 }
20
21
22
23 /// <summary>
24 /// word转成html
25 /// </summary>
26 /// <param name="wordfilename"></param>
27 private string wordtohtml( object wordfilename)
28 {
29 // 在此处放置用户代码以初始化页面
30 word.Application word = new word.Application();
31 Type wordtype = word.GetType();
32 word.Documents docs = word.Documents;
33 // 打开文件
34 Type docstype = docs.GetType();
35 word.Document doc = (word.Document)docstype.InvokeMember( " open " , System.Reflection.BindingFlags.InvokeMethod, null , docs, new object [] { wordfilename, true , true });
36 // 转换格式,另存为
37 Type doctype = doc.GetType();
38 string wordsavefilename = wordfilename.ToString();
39 string strsavefilename = wordsavefilename.Substring( 0 , wordsavefilename.Length - 3 ) + " html " ;
40 object savefilename = ( object )strsavefilename;
41 doctype.InvokeMember( " saveas " , System.Reflection.BindingFlags.InvokeMethod, null , doc, new object [] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
42 doctype.InvokeMember( " close " , System.Reflection.BindingFlags.InvokeMethod, null , doc, null );
43 // 退出 word
44 wordtype.InvokeMember( " quit " , System.Reflection.BindingFlags.InvokeMethod, null , word, null );
45 return savefilename.ToString();
46 }
47
48 private string ppttohtml( string filePath)
49 {
50 // 被转换的html文档保存的位置
51 string saveFileName = filePath + " .html " ;
52 Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
53 Microsoft.Office.Core.MsoTriState m1 = new MsoTriState();
54 Microsoft.Office.Core.MsoTriState m2 = new MsoTriState();
55 Microsoft.Office.Core.MsoTriState m3 = new MsoTriState();
56 Microsoft.Office.Interop.PowerPoint.Presentation pp = ppt.Presentations.Open(filePath, m1, m2, m3);
57 pp.SaveAs(saveFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoTriStateMixed);
58 pp.Close();
59 // 返回文件名
60 return saveFileName;
61 }
62 /**/
63 /// <summary>
64 /// 读取Excel表格数据,返回数据集DataSet
65 /// </summary>
66 /// <param name="FileName"> 文件名 </param>
67 /// <returns> dataset </returns>
68 public string Exceltohtml( string FileName)
69 {
70 Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application(); // 实例化Excel
71 Microsoft.Office.Interop.Excel.Workbook workbook = null ;
72 Microsoft.Office.Interop.Excel.Worksheet worksheet = null ;
73 workbook = repExcel.Application.Workbooks.Open(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // 打开文件,n.FullPath是文件路径
74 worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[ 1 ];
75 string filesavefilename = FileName.ToString();
76 string strsavefilename = filesavefilename.Substring( 0 , filesavefilename.Length - 3 ) + " html " ;
77 object savefilename = ( object )strsavefilename;
78 object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
79 workbook.SaveAs(savefilename, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // 进行另存为操作
80 object osave = false ;
81 workbook.Close(osave, Type.Missing, Type.Missing); // 逐步关闭所有使用的对象
82 repExcel.Quit();
83 System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
84 worksheet = null ;
85 GC.Collect(); // 垃圾回收
86 System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
87 workbook = null ;
88 GC.Collect();
89 System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
90 GC.Collect();
91 System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
92 repExcel = null ;
93 GC.Collect();
94 System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName( " EXCEL " ); // 依据时间杀灭进程
95 foreach (System.Diagnostics.Process p in process)
96 {
97 if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5 )
98 {
99 p.Kill();
100 }
101 }
102
103 return savefilename.ToString();
104
105 }
106 }
107 }
以上是转换成为html文件的方法,转换成功厚在你文件夹下会生对应的图片文件夹,和样式文件,和html文件。
转换成功后直接跳转到转换的html文件,这样office的预览就成功了
希望能够帮助大家,有问题的我会尽力帮助大家。希望和大家一起学习,有什么好的方法希望大家一起交流。
转载于:.html
word,ppt,excel转换为html
因为最近上班做邮箱附件的预览功能,上网找到的解决方案是转为html文件,自己总结了一下:
1.先添加office组建,右键网站,点击添加引用
2.选择.NET找到你需要的组件,例如Microsoft.Office.Interop.Excel(版本为12.0.0.0),我这里用的还有ppt,word,三个都添加上
3.开始写代码
View Code 1 using System;2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using word = Microsoft.Office.Interop.Word;//引入对应的组件
8 using System.IO;
9 using ppt = Microsoft.Office.Interop.PowerPoint;
10 using Microsoft.Office.Core;
11 using Microsoft.Office.Interop;
12
13 namespace Web.MailManage
14 {
15 public partial class WordShow : System.Web.UI.Page
16 {
17 protected void Page_Load( object sender, EventArgs e)
18 { string newurl = string.Empty; newurl = wordtohtml(filename);//转换word文档
Response.Redirect(filename);//跳转到转换后的html文件,这里要注意一样要路径正确,否则没有效果,也就是看到转换的文件在什么路径
19 }
20
21
22
23 /// <summary>
24 /// word转成html
25 /// </summary>
26 /// <param name="wordfilename"></param>
27 private string wordtohtml( object wordfilename)
28 {
29 // 在此处放置用户代码以初始化页面
30 word.Application word = new word.Application();
31 Type wordtype = word.GetType();
32 word.Documents docs = word.Documents;
33 // 打开文件
34 Type docstype = docs.GetType();
35 word.Document doc = (word.Document)docstype.InvokeMember( " open " , System.Reflection.BindingFlags.InvokeMethod, null , docs, new object [] { wordfilename, true , true });
36 // 转换格式,另存为
37 Type doctype = doc.GetType();
38 string wordsavefilename = wordfilename.ToString();
39 string strsavefilename = wordsavefilename.Substring( 0 , wordsavefilename.Length - 3 ) + " html " ;
40 object savefilename = ( object )strsavefilename;
41 doctype.InvokeMember( " saveas " , System.Reflection.BindingFlags.InvokeMethod, null , doc, new object [] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
42 doctype.InvokeMember( " close " , System.Reflection.BindingFlags.InvokeMethod, null , doc, null );
43 // 退出 word
44 wordtype.InvokeMember( " quit " , System.Reflection.BindingFlags.InvokeMethod, null , word, null );
45 return savefilename.ToString();
46 }
47
48 private string ppttohtml( string filePath)
49 {
50 // 被转换的html文档保存的位置
51 string saveFileName = filePath + " .html " ;
52 Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
53 Microsoft.Office.Core.MsoTriState m1 = new MsoTriState();
54 Microsoft.Office.Core.MsoTriState m2 = new MsoTriState();
55 Microsoft.Office.Core.MsoTriState m3 = new MsoTriState();
56 Microsoft.Office.Interop.PowerPoint.Presentation pp = ppt.Presentations.Open(filePath, m1, m2, m3);
57 pp.SaveAs(saveFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoTriStateMixed);
58 pp.Close();
59 // 返回文件名
60 return saveFileName;
61 }
62 /**/
63 /// <summary>
64 /// 读取Excel表格数据,返回数据集DataSet
65 /// </summary>
66 /// <param name="FileName"> 文件名 </param>
67 /// <returns> dataset </returns>
68 public string Exceltohtml( string FileName)
69 {
70 Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application(); // 实例化Excel
71 Microsoft.Office.Interop.Excel.Workbook workbook = null ;
72 Microsoft.Office.Interop.Excel.Worksheet worksheet = null ;
73 workbook = repExcel.Application.Workbooks.Open(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // 打开文件,n.FullPath是文件路径
74 worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[ 1 ];
75 string filesavefilename = FileName.ToString();
76 string strsavefilename = filesavefilename.Substring( 0 , filesavefilename.Length - 3 ) + " html " ;
77 object savefilename = ( object )strsavefilename;
78 object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
79 workbook.SaveAs(savefilename, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // 进行另存为操作
80 object osave = false ;
81 workbook.Close(osave, Type.Missing, Type.Missing); // 逐步关闭所有使用的对象
82 repExcel.Quit();
83 System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
84 worksheet = null ;
85 GC.Collect(); // 垃圾回收
86 System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
87 workbook = null ;
88 GC.Collect();
89 System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
90 GC.Collect();
91 System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
92 repExcel = null ;
93 GC.Collect();
94 System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName( " EXCEL " ); // 依据时间杀灭进程
95 foreach (System.Diagnostics.Process p in process)
96 {
97 if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5 )
98 {
99 p.Kill();
100 }
101 }
102
103 return savefilename.ToString();
104
105 }
106 }
107 }
以上是转换成为html文件的方法,转换成功厚在你文件夹下会生对应的图片文件夹,和样式文件,和html文件。
转换成功后直接跳转到转换的html文件,这样office的预览就成功了
希望能够帮助大家,有问题的我会尽力帮助大家。希望和大家一起学习,有什么好的方法希望大家一起交流。
转载于:.html