Office系列在線預覽
2011-08-10 16:42 【當耐特】 閱讀(17106) 評論(25) 收藏 舉報最近客戶有個需求,需要在線預覽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 }
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 }
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();
}
{
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,所以我們繼續尋找解決方案······
浙公網安備 33010602011771號