Office在线预览
2015-10-15 17:54
423 查看
最近客户有个需求,需要在线预览PPT、Excel、Word,开始打算用第三方组建去读取 office系列,然后生成html,这样的话样式相当于丢了,只剩下数据,而且第三方组件对office版本支持不够完善,最关键的是还是样式丢了!
最后决定,用户在上传的过程中调用office API里面的saveAs,自动生成了静态html,预览的时候就直接访问的该html页面。
[a].WordToHtml
1 public static string WordToHtml(string path, string savePath, string wordFileName)
2 {
3
4 //在此处放置用户代码以初始化页面
5 Microsoft.Office.Interop.Word.Application word = new Word.Application();
6
7 Type wordType = word.GetType();
8
9 Word.Documents docs = word.Documents;
10
11 //打开文件
12 Type docsType = docs.GetType();
13 Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { path, true, true });
14
15 //转换格式,另存为
16 Type docType = doc.GetType();
17
18 string wordSaveFileName = savePath;
19
20 string strSaveFileName = savePath+wordFileName + ".html";
21
22 object saveFileName = (object)strSaveFileName;
23
24 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
25
26 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
27
28 //退出 Word
29 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
30
31 return saveFileName.ToString();
32 }
[b].ExcelToHtml
1 public static void ExcelToHtml(string path,string savePath, string wordFileName)
2 {
3 string str = string.Empty;
4 Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
5 Microsoft.Office.Interop.Excel.Workbook workbook = null;
6 Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
7 workbook = repExcel.Application.Workbooks.Open(path, 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);
8 worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
9 object htmlFile =savePath+wordFileName+ ".html";
10 object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
11 workbook.SaveAs(htmlFile, 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);
12 object osave = false;
13 workbook.Close(osave, Type.Missing, Type.Missing);
14 repExcel.Quit();
15
16 }
[c].PPTToHtml
public static void PPTToHtml(string path, string savePath, string wordFileName)
{
Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
string strSourceFile = path;
string strDestinationFile = savePath+wordFileName+".html";
Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(strSourceFile, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
prsPres.Close();
ppApp.Quit();
}
阅读原文
最后决定,用户在上传的过程中调用office API里面的saveAs,自动生成了静态html,预览的时候就直接访问的该html页面。
[a].WordToHtml
1 public static string WordToHtml(string path, string savePath, string wordFileName)
2 {
3
4 //在此处放置用户代码以初始化页面
5 Microsoft.Office.Interop.Word.Application word = new Word.Application();
6
7 Type wordType = word.GetType();
8
9 Word.Documents docs = word.Documents;
10
11 //打开文件
12 Type docsType = docs.GetType();
13 Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { path, true, true });
14
15 //转换格式,另存为
16 Type docType = doc.GetType();
17
18 string wordSaveFileName = savePath;
19
20 string strSaveFileName = savePath+wordFileName + ".html";
21
22 object saveFileName = (object)strSaveFileName;
23
24 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
25
26 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
27
28 //退出 Word
29 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
30
31 return saveFileName.ToString();
32 }
[b].ExcelToHtml
1 public static void ExcelToHtml(string path,string savePath, string wordFileName)
2 {
3 string str = string.Empty;
4 Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
5 Microsoft.Office.Interop.Excel.Workbook workbook = null;
6 Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
7 workbook = repExcel.Application.Workbooks.Open(path, 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);
8 worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
9 object htmlFile =savePath+wordFileName+ ".html";
10 object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
11 workbook.SaveAs(htmlFile, 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);
12 object osave = false;
13 workbook.Close(osave, Type.Missing, Type.Missing);
14 repExcel.Quit();
15
16 }
[c].PPTToHtml
public static void PPTToHtml(string path, string savePath, string wordFileName)
{
Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
string strSourceFile = path;
string strDestinationFile = savePath+wordFileName+".html";
Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(strSourceFile, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
prsPres.Close();
ppApp.Quit();
}
阅读原文
相关文章推荐
- 不借助 Wine 和云服务:新项目能让 Linux 完整运行微软 Office 套件
- WPS Office:Linux 上的 Microsoft Office 的免费替代品
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- 用Coldfusion生成 OFFICE 文件的代码
- 重现 Office 2007 中的菜单和工具栏的方法
- Office Word九条常用技巧
- Microsoft Office 2007 SP1 简体中文正式版 升级包官方下载地址
- 官方 WPS office 2005 个人精装版 下载
- 在office Word中任意选中单词某一部分字母
- Office 2007 System 常见问题集第1/3页
- Microsoft Office 2007 全系列迅雷下载
- 直接在线预览Word、Excel、TXT文件之ASP.NET
- 永中迎破产清算,叫板微软终成追忆
- 让source insight 搜索到.S的文件 高亮AT&T ASM 文件keyword的方法 sourceinsight技巧
- 解决Office 2010 激活问题。
- Office 2007的照相机功能增加
- Office2007中简繁体转换功能按钮消失解决
- 教你DIY一个漂亮的Word文本框
- 给Word添加一个实用的常用命令菜单
- 使用libre office writer 为文档添加可跳转目录