c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 转swf)

最近项目中要用到文件预览,分为用HTML预览和用swf预览两种模式,所以要将用户上传的文件转成可以预览的格式。

首先是转HTML部分,word、excel、ppt、txt都是调用Microsoft.Office来完成的,全部类似,所以只贴一个word的代码:

c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
public static bool WordToHtml(string path)
        {
            bool ret = true;
            Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = null;
            // 缺省参数 
            object Unknown = Type.Missing;
            try
            {

                // 指定原文件和目标文件 
                object Source = path;
                object Target = path.Substring(0, path.LastIndexOf(".")) + ".html";
                //为了保险,只读方式打开 
                object readOnly = true;
                // 指定另存为格式(html) 
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
                // 打开doc文件 
                doc = newApp.Documents.Open(ref Source, ref Unknown, ref readOnly,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                // 转换格式 
                doc.SaveAs(ref Target, ref format,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
            }
word转HTML
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)

excel、ppt分别调用自己的application类来完成,txt用的类和word一样。

比较麻烦的是PDF转HTML的,最后我采用的是在*上找到的支持命令行的pdftohtml.exe,然后在程序里开启process调用就行了:

c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
        public static bool PdfToHtml(string path)
        {
            string convertPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "PDFtoHTML\\pdftohtml.exe";
            Process p = new Process();
            p.StartInfo.FileName = convertPath;
            p.StartInfo.Arguments = " -enc GBK " + path;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            bool isStart = p.Start();
            p.WaitForExit();
            p.Close();
            return isStart;
        }
pdf转html
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)

 

再来是转swf部分,flashpaper不支持64位,print2flash又需要花钱解锁,所以只好走别的路了。

最后使用了先转成pdf,再用swftools把pdf转成swf的方式

其中转pdf的模式也是调用Microsoft.Office来完成,以word为例:

c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
        private static bool wordToPdf(string path, string outpath = "")
        {
            bool ret = true;
            Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = null;
            // 缺省参数 
            object Unknown = Type.Missing;
            try
            {

                // 指定原文件和目标文件 
                object Source = path;
                object Target = path.Substring(0, path.LastIndexOf(".")) + ".pdf";
                if (outpath != "")
                    Target = outpath;
                //为了保险,只读方式打开 
                object readOnly = true;
                // 指定另存为格式(html) 
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;


                // 打开doc文件 
                doc = newApp.Documents.Open(ref Source, ref Unknown, ref readOnly,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                // 转换格式 
                doc.SaveAs(ref Target, ref format,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
            }
            catch (Exception e)
            {
                ret = false;
            }
            finally
            {
                if (doc != null)
                {
                    // 关闭文档和Word程序 
                    object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                    doc.Close(ref saveChanges, ref Unknown, ref Unknown);
                }
                newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
                GC.Collect();
            }
           
word转pdf
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)

然后是调用swftools来转换成swf,但是在服务器上不管怎么尝试,直接使用process来启动pdf2swf.exe都转换失败,原因不明,最后使用把启动pdf2swf写到BAT里,然后用process去调用BAT,就可以成功转换了。

c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
c:
cd C:\Program Files (x86)\SWFTools\
pdf2swf.exe -f -T 9 -t "%1" -o "%2"
调用pdf2swf.exe
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)

其中一定要记得有-T的参数,后面的数字是flash的版本,如果对不上的话是转换不了的。

然后就是在程序里调用这个BAT,把路径传参进去:

c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)
        private static bool ChangeToSwf(string filePath, string swfPath, string basePath)
        {
            bool isStart = false;
            string type = filePath.Split(.).Last().ToLower();

            if (type != "pdf")
            {
                string tempPdf = basePath + "/swffiles/" + "temp" + Guid.NewGuid().ToString() + ".pdf";//临时pdf的路径
                if (!FileToTempPdf(filePath, tempPdf))//先把文件转换成PDF
                {
                    return false;
                }
                else
                {
                    filePath = tempPdf;
                }
            }

            try
            {
                Process p = new Process();
                string path = basePath + "/plugin/ChangeSwf.bat";//bat路径
                ProcessStartInfo pi = new ProcessStartInfo(path, filePath + " " + swfPath);//第二个参数为传入的参数,string类型以空格分隔各个参数
                pi.UseShellExecute = false;
                pi.RedirectStandardOutput = true;
                p.StartInfo = pi;
                isStart = p.Start();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                return false;
            }
            return isStart;
        }
文件转swf
c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 
转swf)

大功告成,页面上我使用了flex paper来预览

c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 转swf),布布扣,bubuko.com

c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 转swf)

上一篇:ASP.NET MVC 学习2、从Controller传递数据到View


下一篇:HTML标签实现图片滚动显示