生活随笔
收集整理的這篇文章主要介紹了
c# Create Excel file and save as PDF
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法一: 引用庫: FreeSpire.XLS(VS NuGet 里面能直接找到)
//void ConvertToPDF(string Path,string filename)//{// Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();// workbook.LoadFromFile(Path);//// Spire.Xls.Worksheet sheet= workbook.Worksheets[0];// sheet.PageSetup.PrintArea = "A1:I41";//設置打印區域// sheet.SaveToPdf(Environment.CurrentDirectory.ToString() + @"\OUTPUT_PDF\"+ filename + ".pdf"); //}
方法二:
調用Office庫:Microsoft.Office.Interop.Excel
//public bool PrintPDF(string ipt)//{// var res = false;// try// {// var lab_path = @"C:\printPDF\pdf.xls";//調用xls模板// mExcel = new Microsoft.Office.Interop.Excel.Application();// mExcel.Visible = false;//true// mWorkbook = mExcel.Application.Workbooks.Open(lab_path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, // Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);// mWorkSheet = new Microsoft.Office.Interop.Excel.Worksheet();// mWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)mWorkbook.Worksheets[1];// // mWorkSheet.Activate();//// ..........此部分未數據填充以及文件格式設置//// mWorkSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;//設置打印紙張大小// mWorkSheet.PageSetup.PrintArea = "A1:I41";//設置打印區域// //end filling value// //start to print out // //mWorkbook.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//mWorkbook.PrintOutEx();// //end print,then save file//// var savetoXls = Environment.CurrentDirectory.ToString() + @"\PDF_XLS\PDF_" + ipt + ".xls";// var savetoPDF = Environment.CurrentDirectory.ToString() + @"\PDF_PDF\PDF_" + ipt + ".pdf";//// mWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, savetoPDF);// mWorkbook.SaveAs(savetoXls, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value);// mWorkbook.Close(false, Missing.Value, Missing.Value);// mExcel.Quit();// res =true;// }// catch// {// res=false;// throw;// }// return res;//}
可根據需求選擇合適的。
總結
以上是生活随笔為你收集整理的c# Create Excel file and save as PDF的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。