EXCEL 转为PDF

1、使用  Aspose.Cells.dll

      Aspose.Cells.dll只能excel工作簿转PDF,可将其他工作簿隐藏。

      using Aspose.Cells;   命名空间

            Worksheet sourceSheet;
            Workbook workbook = new Workbook(path.xlsx");
            WorksheetCollection myColection = workbook.Worksheets;
           
            for (int i = 1; i < myColection.Count; i++)
            {
                sourceSheet = workbook.Worksheets[i];     //隐藏工作簿
                sourceSheet.IsVisible = false;
            }
         
          
            workbook.Save(path2.pdf", SaveFormat.Pdf);

2、using Microsoft.Office.Interop.Excel

   // Excel 檔案位置
            string sourcexlsx = path1.xlsx;
            // PDF 儲存位置
            string targetpdf = path2.pdf;
            //ConverterToPdf(sourcexlsx, targetpdf);
            //建立 Excel application instance
            Microsoft.Office.Interop.Excel.Application appExcel = new Microsoft.Office.Interop.Excel.Application();
            //開啟 Excel 檔案
            Microsoft.Office.Interop.Excel.Workbook workbook= appExcel.Workbooks.Open(sourcexlsx);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[工作表];
            //匯出為 pdf
            worksheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, targetpdf);
   //workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, targetpdf);
            //關閉 Excel 檔
            workbook.Close();
            //結束 Excel
            appExcel.Quit();

 

posted @ 2020-04-22 15:12  越吃越瘦  阅读(476)  评论(0编辑  收藏  举报