C# 將Word,Excel轉換成Html

來源:互聯網
上載者:User

轉:http://skillerguo.blog.163.com/blog/static/1709513920071853219948/

 

首先,需要瞭解的是VBA編程,它是Office提供的外部編程介面,我採用的是Office2003所以添加的引用是Microsoft Office 11.0 Object Library 這個Com組件,否則無法操作Office的。下面開始:

Word -〉Html

Microsoft.Office.Interop.Word.ApplicationClass appclass = new Microsoft.Office.Interop.Word.ApplicationClass();//執行個體化一個Word
Type wordtype = appclass.GetType();
Microsoft.Office.Interop.Word.Documents docs = appclass.Documents;//擷取Document
Type docstype = docs.GetType();
object filename = Application.StartupPath+@"\ccyt_chm\inform\"+n.FullPath;//n.FullPath為Word檔案的路徑
Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docstype.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,null,docs,new object[]{filename,true,true});//開啟檔案
Type doctype = doc.GetType();
object savefilename = Application.StartupPath+@"\ccyt_chm\inform\"+n.FullPath.Split('.').GetValue(0)+".html";
doctype.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,null,doc,new object[]{savefilename,Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML});//另存新檔Html格式
wordtype.InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod,null,appclass,null);//退出
Thread.Sleep(3000);//為了使退出完全,這裡阻塞3秒
StreamReader objreader = new StreamReader(savefilename.ToString(),System.Text.Encoding.GetEncoding("GB2312"));  //以下內容是為了在Html中加入對本身Word檔案的下載       
FileStream fs = new FileStream(savefilename.ToString().Split('.').GetValue(0).ToString()+"$.html",FileMode.Create);
streamHtmlHelp = new System.IO.StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
streamHtmlHelp.WriteLine("<a href=\""+n.Text+"\">源檔案下載</a><br>");
do 
{
    str = objreader.ReadLine();
    streamHtmlHelp.WriteLine(str);
}
while (str !=  "</html>");
streamHtmlHelp.Close();
objreader.Close();
File.Delete(savefilename.ToString());
File.Move(savefilename.ToString().Split('.').GetValue(0).ToString()+"$.html",savefilename.ToString());

Excel -〉Html(這個與Word不同,因為Excel為很多層,又並列很多層,必須都清空才能銷毀執行個體,但實際中我發現並不是每次都能銷毀,所以網上求解多次沒有結果,只能殺沒進程,為了保證只殺滅最近的進程,我用時間進行判斷)

Microsoft.Office.Interop.Excel.Application   repExcel   =   new   Microsoft.Office.Interop.Excel.Application ();//執行個體化Excel
Microsoft.Office.Interop.Excel.Workbook   workbook   =   null;         
Microsoft.Office.Interop.Excel.Worksheet  worksheet   =   null;
workbook   =   repExcel.Application.Workbooks.Open(Application.StartupPath+@"\ccyt_chm\inform\"+n.FullPath,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是檔案路徑
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
object   htmlFile   =   Application.StartupPath+@"\ccyt_chm\inform\"+n.FullPath.Split('.').GetValue(0)+".html";   
object   ofmt   =   Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;   
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);//進行另存新檔操作   
object   osave   =   false;         
workbook.Close(osave,Type.Missing,Type.Missing);//逐步關閉所有使用的對象
repExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
GC.Collect();//記憶體回收
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook=null;
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
repExcel = null;
GC.Collect();
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");//依據時間殺滅進程
foreach ( System.Diagnostics.Process p in process)
{
    if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
    {
          p.Kill();
     }
}
Thread.Sleep(3000);//保證完全關閉
StreamReader objreader = new StreamReader(htmlFile.ToString().Split('.').GetValue(0)+".files\\sheet001.html",System.Text.Encoding.GetEncoding("GB2312"));//以下內容是在Html的第一個架構中添加下載原Excel的超連結         
FileStream fs = new FileStream(htmlFile.ToString().Split('.').GetValue(0)+".files\\sheet001$.html",FileMode.Create);
streamHtmlHelp = new System.IO.StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
streamHtmlHelp.WriteLine("<a href=\""+"../"+n.Text+"\">源檔案下載</a><br>");
do 
{
    str = objreader.ReadLine();
    streamHtmlHelp.WriteLine(str);
}
while (str !=  "</html>");
streamHtmlHelp.Close();
objreader.Close();
File.Delete(htmlFile.ToString().Split('.').GetValue(0)+".files\\sheet001.html");
File.Move(htmlFile.ToString().Split('.').GetValue(0)+".files\\sheet001$.html",htmlFile.ToString().Split('.').GetValue(0)+".files\\sheet001.html");

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.