마이크로소프트를 이용하다.Office.Interop.Excel에서 웹 페이지를 PDF로 변환

8044 단어 Microsoft
인터넷에는 웹 페이지를 PDF로 바꾸는 방법과 유료 제3자 플러그인도 많다.사실 Office가 자체적으로 가지고 있는 EXCEL을 PDF로 발표하는 기능을 이용하면 실현할 수 있다. 만약 당신의 요구가 복잡하지 않다면 필자의 방법을 사용할 수 있다.
우선 웹 페이지 html를 EXCEL 파일로 저장합니다. (이 절차는 여러 가지 방법이 있기 때문에 상세하게 검토하지 않겠습니다. 웹 페이지를 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();

그리고 마이크로소프트를 이용해서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();
            }
        }

[코드 예제]

좋은 웹페이지 즐겨찾기