代码改变世界

利用Microsoft.Office.Interop.Excel 将web页面转成PDF

2014-07-28 17:09  IT农民!  阅读(2286)  评论(5编辑  收藏  举报

网上有很多将Web页面转成PDF的方法,还有许多收费的第三方插件。其实利用Office 自带的将EXCEL发布成PDF的功能就可以实现,如果你的需求没有多复杂,可以采用笔者的方法。

首先将web页面html保存为EXCEL文件(此步骤有多种方法,就不详细探讨了。只要能将web页面转成EXCEL文件,剩下的就好说了。)

StringWriter html = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(html);
base.Render(tw);

//excelName存放Excel的地址
FileStream fs = new FileStream(excelName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("utf-8"));
            //开始写入
            sw.Write(html);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();

 然后利用Microsoft.Office.Interop.Excel将EXCEL 转成PDF

首先引用 Microsoft.Office.Interop.Excel.dll,并设置 dll 的属性值无法嵌入互操作类型 为false 。否则会报 类型“Microsoft.Office.Interop.Excel.ApplicationClass”未定义构造函数    无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口 错误。
然后安装 SaveAsPDFandXPS.exe (安装此插件才能将excel 另存为 pdf)
源代码如下:
/// <summary>
        /// Excel保存PDF
        /// </summary>
        /// <param name="excelPath"> EXCEL全路径 </param>
        /// <param name="pdfPath"> PDF保存路径 </param>
        /// <returns></returns>
        public static bool CovertExcelToPDF( string excelPath, string pdfPath)
        {
            object missing = Type .Missing;
            ////创建excel应用程序实例
            ApplicationClass application = null ;
            ////创建工作薄实例
            Workbook workBook = null ;
            try
            {
                application = new ApplicationClass ();
                ////打开工作簿
                workBook = application.Workbooks.Open(excelPath, missing, missing, missing, missing, missing,
                                                      missing, missing, missing, missing, missing, missing, missing, missing, missing);
                ////打开sheet
                Worksheet ws = (Worksheet )workBook.Worksheets.Item[1];
                ////设置打印放放为水平
                ws.PageSetup.Orientation = XlPageOrientation .xlPortrait;
                ////设置打印时excel内容在一个页面上显示。Zoom必须设置为false
                ws.PageSetup.Zoom = false ;
                ws.PageSetup.FitToPagesTall = 1;
                ws.PageSetup.FitToPagesWide = 1;

                ////将工作簿发布为PDF或XPS格式
                ws.ExportAsFixedFormat( XlFixedFormatType .xlTypePDF, pdfPath
                    , XlFixedFormatQuality .xlQualityStandard
                    , true
                    , false     ////忽略打印区域
                    , missing, missing, missing, missing);
                return true ;
            }
            catch
            {
                throw ;
            }
            finally
            {
                ////工作簿关闭
                if (workBook != null )
                {
                    workBook.Close( true , missing, missing);
                    workBook = null ;
                }
                //// excel应用程序退出关闭
                if (application != null )
                {
                    application.Quit();
                    application = null ;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }


【代码示例】