C#.NET实现Word或Excel⽂件转为HTML⽂件
Word⽂件转html,返回相对路径
1private string GetPathByDocToHTML(string strFile)
2 {
3if (string.IsNullOrEmpty(strFile))
4 {
5return"0";//没有⽂件
6 }
7
8 Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
9 Type wordType = word.GetType();
10 Microsoft.Office.Interop.Word.Documents docs = word.Documents;
11
12// 打开⽂件
13 Type docsType = docs.GetType();
14
15object fileName = strFile;
16
17 Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
18 System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });
19
20// 转换格式,另存为html
21 Type docType = doc.GetType();
22//给⽂件重新起名
23string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
24 System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
25
26string strFileFolder = "../html/";
27 DateTime dt = DateTime.Now;
28//以yyyymmdd形式⽣成⼦⽂件夹名
29string strFileSubFolder = dt.Year.ToString();
30 strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString();
31 strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString();
32string strFilePath = strFileFolder + strFileSubFolder + "/";
33// 判断指定⽬录下是否存在⽂件夹,如果不存在,则创建
34if (!Directory.Exists(Server.MapPath(strFilePath)))
35 {
36// 创建up⽂件夹
37 Directory.CreateDirectory(Server.MapPath(strFilePath));
38 }
39
40//被转换的html⽂档保存的位置
41// HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html")
42string ConfigPath = Server.MapPath(strFilePath + filename + ".html");
43object saveFileName = ConfigPath;
44
45/*下⾯是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
46 * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
47 * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
48 * 其它格式:
49 * wdFormatHTML
50 * wdFormatDocument
51 * wdFormatDOSText
52 * wdFormatDOSTextLineBreaks
53 * wdFormatEncodedText
54 * wdFormatRTF
55 * wdFormatTemplate
56 * wdFormatText
57 * wdFormatTextLineBreaks
58 * wdFormatUnicodeText
59*/
60 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
61null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
62
63//docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
64// null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
65
66//关闭⽂档
67 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
68null, doc, new object[] { null, null, null });
69
70// 退出 Word
71 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
72//转到新⽣成的页⾯
73//return ("/" + filename + ".html");
74
75//转化HTML页⾯统⼀编码格式
76 TransHTMLEncoding(ConfigPath);
77
78return (strFilePath + filename + ".html");
79 }
Excel⽂件转HTML,返回相对路径
1private string GetPathByXlsToHTML(string strFile)
2 {
3if (string.IsNullOrEmpty(strFile))
4 {
5return"0";//没有⽂件
6 }
7
8//实例化Excel
9 Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
10 Microsoft.Office.Interop.Excel.Workbook workbook = null;
11 Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
12
13//打开⽂件,n.FullPath是⽂件路径
14 workbook = repExcel.Application.Workbooks.Open(strFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
15 worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
16
17//给⽂件重新起名
18string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
19 System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
20
21string strFileFolder = "../html/";
22 DateTime dt = DateTime.Now;
23//以yyyymmdd形式⽣成⼦⽂件夹名
24string strFileSubFolder = dt.Year.ToString();
25 strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString();
26 strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString();
27string strFilePath = strFileFolder + strFileSubFolder + "/";
28// 判断指定⽬录下是否存在⽂件夹,如果不存在,则创建
29if (!Directory.Exists(Server.MapPath(strFilePath)))
30 {
31// 创建up⽂件夹
32 Directory.CreateDirectory(Server.MapPath(strFilePath));
33 }
34string ConfigPath = Server.MapPath(strFilePath + filename + ".html");
35object savefilename = (object)ConfigPath;
36
37object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
38//进⾏另存为操作
39 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.Mis 40object osave = false;
41//逐步关闭所有使⽤的对象
42 workbook.Close(osave, Type.Missing, Type.Missing);
43 repExcel.Quit();
44 System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
45 worksheet = null;
46//垃圾回收
47 GC.Collect();
48 System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
49 workbook = null;
50 GC.Collect();
51 System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
52 GC.Collect();
53 System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
54 repExcel = null;
55 GC.Collect();
56//依据时间杀灭进程
57 System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");
html代码转链接58foreach (System.Diagnostics.Process p in process)
59 {
60if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
61 {
62 p.Kill();
63 }
64 }
65
66return (strFilePath + filename + ".html");
67 }
这⾥可能会遇到⼀个问题,由于转化为HTML⽂件的页⾯编码可能使得浏览器⽆法正确解读,所以需要转码,转换代码如下:
1private void TransHTMLEncoding(string strFilePath)
2 {
3try
4 {
5 System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0));
6string html = sr.ReadToEnd();
7 sr.Close();
8 html = System.Text.RegularExpressions.Regex.Replace(html, @"<meta[^>]*>", "<meta http-equiv=Content-Type content='text/html; charset=gb2312'>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
9 System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, Encoding.Default);
10
11 sw.Write(html);
12 sw.Close();
13 }
14catch (Exception ex)
15 {
16 Page.RegisterStartupScript("alt", "<script>alert('" + ex.Message + "')</script>");
17 }
18 }
这样就可以正常在页⾯上正常显⽰了
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论