1. 程式人生 > >NPOI寫Excel,Spire.XLS for.NET元件轉換Excel為PDF

NPOI寫Excel,Spire.XLS for.NET元件轉換Excel為PDF

首先要引用NPOI動態庫和Microsoft.Office.Interop.excel.dll (Microsoft.Office.Interop.excel.dll  最好使用使用NuGet下載 ,或者使用網上下載,網上下載以後解壓檔案,把Microsoft.Office.Interop.excel.dll拷貝到專案下,新增引用,而NuGet下載直接引用,不需要再新增引用了,建議NuGet下載。

NPOI的新增則專案選中右鍵使用管理NuGet管理程式包,nuget新增NPOI即可)

上述工作完成,下面直接程式碼

using System; using System.Collections.Generic; using System.Text; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; using NPOI.XSSF.UserModel; using System.IO; using System.Drawing; using System.Drawing.Imaging; using Excel = Microsoft.Office.Interop.Excel; //using Spire.Xls;

namespace ConsoleNPOI { class Program { static void Main(string[] args) { Console.WriteLine("開始操作EXCEL。。。。"); Console.WriteLine("正在操作請稍等。。。。"); //新建xls工作簿 HSSFWorkbook workbook2003 = new HSSFWorkbook(); //建立Sheet頁 ISheet sheet = workbook2003.CreateSheet("資訊核查表");

#region AAAAAAAA名稱 IRow IRow0 = sheet.CreateRow(0); for (int h = 0; h < 10; h++) { ICell Icell = IRow0.CreateCell(h); Icell.SetCellValue("AAAAAAAA名稱");

ICellStyle style = workbook2003.CreateCellStyle(); //設定單元格的樣式:水平對齊居中 style.Alignment = HorizontalAlignment.Center; //新建一個字型樣式物件 IFont font = workbook2003.CreateFont(); font.FontName = "宋體"; font.FontHeightInPoints = 18; //設定字型加粗樣式 font.Boldweight = (short)FontBoldWeight.Bold; //使用SetFont方法將字型樣式新增到單元格樣式中  style.SetFont(font); //將新的樣式賦給單元格 Icell.CellStyle = style; } //合併單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 9)); #endregion

#region 人證識別資訊表 IRow IRow1 = sheet.CreateRow(1); for (int h = 0; h < 10; h++) { ICell Icell1 = IRow1.CreateCell(h); Icell1.SetCellValue("人證識別資訊表");

ICellStyle style1 = workbook2003.CreateCellStyle(); //設定單元格的樣式:水平對齊居中 style1.Alignment = HorizontalAlignment.Center; //新建一個字型樣式物件 IFont font1 = workbook2003.CreateFont(); font1.FontName = "宋體"; font1.FontHeightInPoints = 16; //設定字型加粗樣式 font1.Boldweight = (short)FontBoldWeight.Normal; //使用SetFont方法將字型樣式新增到單元格樣式中  style1.SetFont(font1); //將新的樣式賦給單元格 Icell1.CellStyle = style1; } //合併單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 9)); #endregion

#region 身份證資訊 IRow IRow2 = sheet.CreateRow(2); for (int h = 0; h < 10; h++) { ICell Icell2 = IRow2.CreateCell(h); Icell2.SetCellValue("身份證資訊"); ICellStyle style2 = workbook2003.CreateCellStyle(); if (h == 0) { style2.BorderLeft = BorderStyle.Thin; } else if (h == 9) { style2.BorderRight = BorderStyle.Thin; } style2.BorderTop = BorderStyle.Thin; style2.Alignment = HorizontalAlignment.Left; IFont font2 = workbook2003.CreateFont(); font2.FontName = "宋體"; font2.FontHeightInPoints = 12; font2.Boldweight = (short)FontBoldWeight.Normal; style2.SetFont(font2); Icell2.CellStyle = style2; } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 2, 0, 9)); #endregion

#region 身份證正反面照片  IRow IRow3 = sheet.CreateRow(3); IRow3.Height = 200 * 20; for (int h = 0; h < 10; h++) { ICellStyle style3 = workbook2003.CreateCellStyle(); ICell Icell3 = IRow3.CreateCell(h); if (h == 0) { //Icell3.SetCellValue("身份證正面照"); #region 新增身份證正面照

//插入圖片 //byte[] bytes = System.IO.File.ReadAllBytes(FileName); //if (!string.IsNullOrEmpty(FileName)) //{ MemoryStream memoryStream = new MemoryStream(); Bitmap bitmap = new Bitmap(@"D:\Person\Solution\Solution\MySln\ConsoleNPOI\idcard_positive_photo.png"); bitmap.Save(memoryStream, ImageFormat.Png); byte[] imagebyte = memoryStream.GetBuffer(); int pictureIdx = workbook2003.AddPicture(imagebyte, PictureType.PNG); HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(300, 50, 600, 200, 0, 3, 4, 3); //##處理照片位置,【圖片左上角為(col, row)第row+1行col+1列,右下角為( col +1, row +1)第 col +1+1行row +1+1列,寬為100,高為50

HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);

//pict.Resize();//這句話一定不要,這是用圖片原始大小來顯示 #endregion style3.BorderLeft = BorderStyle.Thin; } else if (h == 5) { #region 新增身份證反面照

//插入圖片 //byte[] bytes = System.IO.File.ReadAllBytes(FileName); //if (!string.IsNullOrEmpty(FileName)) //{ MemoryStream memoryStream = new MemoryStream(); Bitmap bitmap = new Bitmap(@"D:\Person\Solution\Solution\MySln\ConsoleNPOI\idcard_negative_photo.png"); bitmap.Save(memoryStream, ImageFormat.Png); byte[] imagebyte = memoryStream.GetBuffer(); int pictureIdx = workbook2003.AddPicture(imagebyte, PictureType.PNG); HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(300, 50, 600, 200, 5, 3, 9, 3); //##處理照片位置,【圖片左上角為(col, row)第row+1行col+1列,右下角為( col +1, row +1)第 col +1+1行row +1+1列,寬為100,高為50

HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);

//pict.Resize();//這句話一定不要,這是用圖片原始大小來顯示 //}

//Drawing drawing = sheet.createDrawingPatriarch(); ////新增一個圖片  ////建立錨點  //ClientAnchor anchor = helper.createClientAnchor(); ////設定圖片的左上角  ////接下來呼叫Picture#resize()設定圖片相對於設定的左上角的位置  //anchor.setCol1(3);//從0開始 第3列  //anchor.setRow1(2);//從0開始 第2行  // //根據錨點和圖片下標建立並繪製一張圖片  //Picture pict = drawing.createPicture(anchor, pictureIdx); ////相對於圖片的左上角自動適應大小  ////===========>>>>>>>>>[注意]<<<<<<=================  ////picture.resize() 僅僅只是針對這兩種型別的圖片 JPEG 和 PNG.  ////其他格式暫時不支援  //pict.resize();

#endregion //Icell3.SetCellValue("身份證反面照"); } else if (h == 9) { style3.BorderRight = BorderStyle.Thin; } style3.BorderTop = BorderStyle.Thin; style3.Alignment = HorizontalAlignment.Left; IFont font3 = workbook2003.CreateFont(); font3.FontName = "宋體"; font3.FontHeightInPoints = 12; font3.Boldweight = (short)FontBoldWeight.Normal; style3.SetFont(font3); Icell3.CellStyle = style3;

} sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(3, 3, 0, 4)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(3, 3, 5, 9)); #endregion

#region 現場採集照片 IRow IRow4 = sheet.CreateRow(4);

for (int h = 0; h < 10; h++) { ICell Icell4 = IRow4.CreateCell(h); Icell4.SetCellValue("現場採集照片"); ICellStyle style4 = workbook2003.CreateCellStyle(); if (h == 0) { style4.BorderLeft = BorderStyle.Thin; } else if (h == 9) { style4.BorderRight = BorderStyle.Thin; } style4.BorderTop = BorderStyle.Thin; style4.Alignment = HorizontalAlignment.Left; IFont font4 = workbook2003.CreateFont(); font4.FontName = "宋體"; font4.FontHeightInPoints = 12; font4.Boldweight = (short)FontBoldWeight.Normal; style4.SetFont(font4); Icell4.CellStyle = style4; } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(4, 4, 0, 9));

#endregion

#region 現場採集照片-照片 IRow IRow5 = sheet.CreateRow(5); IRow5.Height = 200 * 20; for (int h = 0; h < 10; h++) { ICell Icell5 = IRow5.CreateCell(h); //Icell5.SetCellValue("現場採集照片-照片"); #region 新增圖片

//插入圖片 //byte[] bytes = System.IO.File.ReadAllBytes(FileName); //if (!string.IsNullOrEmpty(FileName)) //{ MemoryStream memoryStream = new MemoryStream(); Bitmap bitmap = new Bitmap(@"D:\Person\Solution\Solution\MySln\ConsoleNPOI\idcard_camera_photo.png"); bitmap.Save(memoryStream, ImageFormat.Png); byte[] imagebyte = memoryStream.GetBuffer(); int pictureIdx = workbook2003.AddPicture(imagebyte, PictureType.PNG); HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(10, 50, 100, 200, 1, 5, 9, 5); //##處理照片位置,【圖片左上角為(col, row)第row+1行col+1列,右下角為( col +1, row +1)第 col +1+1行row +1+1列,寬為100,高為50

HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);

//pict.Resize();//這句話一定不要,這是用圖片原始大小來顯示 //}

//Drawing drawing = sheet.createDrawingPatriarch(); ////新增一個圖片  ////建立錨點  //ClientAnchor anchor = helper.createClientAnchor(); ////設定圖片的左上角  ////接下來呼叫Picture#resize()設定圖片相對於設定的左上角的位置  //anchor.setCol1(3);//從0開始 第3列  //anchor.setRow1(2);//從0開始 第2行  // //根據錨點和圖片下標建立並繪製一張圖片  //Picture pict = drawing.createPicture(anchor, pictureIdx); ////相對於圖片的左上角自動適應大小  ////===========>>>>>>>>>[注意]<<<<<<=================  ////picture.resize() 僅僅只是針對這兩種型別的圖片 JPEG 和 PNG.  ////其他格式暫時不支援  //pict.resize();

#endregion ICellStyle style5 = workbook2003.CreateCellStyle(); if (h == 0) { style5.BorderLeft = BorderStyle.Thin; } else if (h == 9) { style5.BorderRight = BorderStyle.Thin; } style5.BorderTop = BorderStyle.Thin; style5.Alignment = HorizontalAlignment.Center; Icell5.CellStyle = style5; } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(5, 5, 0, 9));

#endregion

#region 比對結果 //IRow IRow6 = sheet.CreateRow(6); //for (int h = 0; h < 2; h++) //{ // ICell Icell = IRow6.CreateCell(h); // if (h == 0) // { // Icell.SetCellValue("比對結果"); // } // else // { // Icell.SetCellValue("92.68"); // }

// ICellStyle style6 = workbook2003.CreateCellStyle(); // //設定單元格的樣式:水平對齊居中 // style6.Alignment = HorizontalAlignment.Center; // Icell.CellStyle = style6; // //合併單元格 // // sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 9)); //} IRow IRow6 = sheet.CreateRow(6); for (int h = 0; h < 10; h++) { ICellStyle style6 = workbook2003.CreateCellStyle(); ICell Icell6 = IRow6.CreateCell(h); if (h == 0) { Icell6.SetCellValue("比對結果"); style6.BorderLeft = BorderStyle.Thin; } else if (h == 5) { Icell6.SetCellValue("92.68"); } else if (h == 9) { style6.BorderRight = BorderStyle.Thin; } style6.BorderTop = BorderStyle.Thin; style6.Alignment = HorizontalAlignment.Left; IFont font6 = workbook2003.CreateFont(); font6.FontName = "宋體"; font6.FontHeightInPoints = 12; font6.Boldweight = (short)FontBoldWeight.Normal; style6.SetFont(font6); Icell6.CellStyle = style6; } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(6, 6, 0, 4)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(6, 6, 5, 9));

#endregion

#region 比對日期 //IRow IRow7 = sheet.CreateRow(7); //for (int h = 0; h < 2; h++) //{ // ICell Icell7 = IRow7.CreateCell(h); // if (h == 0) // { // Icell7.SetCellValue("比對日期"); // } // else // { // Icell7.SetCellValue("2018年9月29日"); // }

// ICellStyle style7 = workbook2003.CreateCellStyle(); // //設定單元格的樣式:水平對齊居中 // style7.Alignment = HorizontalAlignment.Center; // Icell7.CellStyle = style7; // //合併單元格 // // sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 9)); //} IRow IRow7 = sheet.CreateRow(7); for (int h = 0; h < 10; h++) { ICellStyle style7 = workbook2003.CreateCellStyle(); ICell Icell7 = IRow7.CreateCell(h); if (h == 0) { Icell7.SetCellValue("比對日期"); style7.BorderLeft = BorderStyle.Thin; } else if (h == 5) { Icell7.SetCellValue("2018年9月29日"); } else if (h == 9) { style7.BorderRight = BorderStyle.Thin; } style7.BorderTop = BorderStyle.Thin; style7.Alignment = HorizontalAlignment.Left; IFont font7 = workbook2003.CreateFont(); font7.FontName = "宋體"; font7.FontHeightInPoints = 12; font7.Boldweight = (short)FontBoldWeight.Normal; style7.SetFont(font7); Icell7.CellStyle = style7;

} sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(7, 7, 0, 4)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(7, 7, 5, 9)); #endregion

#region 確認簽字 //IRow IRow8 = sheet.CreateRow(8); //ICell Icell8 = IRow8.CreateCell(0); //Icell8.SetCellValue("確認簽字"); //ICellStyle style8 = workbook2003.CreateCellStyle(); ////設定單元格的樣式:水平對齊居中 //style8.Alignment = HorizontalAlignment.Center; ////新建一個字型樣式物件 //IFont font8 = workbook2003.CreateFont(); //font8.FontName = "宋體"; //font8.FontHeightInPoints = 12; ////設定字型加粗樣式 //font8.Boldweight = (short)FontBoldWeight.Normal; ////使用SetFont方法將字型樣式新增到單元格樣式中  //style8.SetFont(font8); ////將新的樣式賦給單元格 //Icell8.CellStyle = style8; IRow IRow8 = sheet.CreateRow(8); for (int h = 0; h < 10; h++) { ICell Icell8 = IRow8.CreateCell(h); Icell8.SetCellValue("確認簽字"); ICellStyle style8 = workbook2003.CreateCellStyle(); style8.Alignment = HorizontalAlignment.Left; if (h == 0) { style8.BorderLeft = BorderStyle.Thin; } else if (h == 9) { style8.BorderRight = BorderStyle.Thin; } style8.BorderTop = BorderStyle.Thin; IFont font8 = workbook2003.CreateFont(); font8.FontName = "宋體"; font8.FontHeightInPoints = 12; font8.Boldweight = (short)FontBoldWeight.Normal; style8.SetFont(font8); Icell8.CellStyle = style8; } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(8, 8, 0, 9)); #endregion

#region 確認簽字--宣告內容 //IRow IRow9 = sheet.CreateRow(9); //ICell Icell9 = IRow9.CreateCell(0); //string confirmname = "以上照片為本人在宿遷市不動產登記中心辦理業務時,經本人同意現場所設。\r\n 本人知曉:若冒充他人偽造證件,將會承擔刑事責任和賠償責任"; //Icell9.SetCellValue(confirmname); //ICellStyle style9 = workbook2003.CreateCellStyle(); ////設定單元格的樣式:水平對齊居中 //style9.Alignment = HorizontalAlignment.Left; ////新建一個字型樣式物件 //IFont font9 = workbook2003.CreateFont(); //font9.FontName = "宋體"; //font9.FontHeightInPoints = 12; ////設定字型加粗樣式 //font9.Boldweight = (short)FontBoldWeight.Normal; ////使用SetFont方法將字型樣式新增到單元格樣式中  //style9.SetFont(font9); ////將新的樣式賦給單元格 //Icell9.CellStyle = style9;

IRow IRow9 = sheet.CreateRow(9); IRow9.Height = 15 * 200; for (int h = 0; h < 10; h++) { ICell Icell9 = IRow9.CreateCell(h); string confirmname = "以上照片為本人在宿遷市不動產登記中心辦理業務時,經本人同意現場所設。\r\n 本人知曉:若冒充他人偽造證件,將會承擔刑事責任和賠償責任"; Icell9.SetCellValue(confirmname); ICellStyle style9 = workbook2003.CreateCellStyle();

style9.Alignment = HorizontalAlignment.Left; style9.VerticalAlignment = VerticalAlignment.Center; style9.WrapText = true; if (h == 0) { style9.BorderLeft = BorderStyle.Thin; } else if (h == 9) { style9.BorderRight = BorderStyle.Thin; } style9.BorderTop = BorderStyle.Thin; IFont font9 = workbook2003.CreateFont(); font9.FontName = "宋體"; font9.FontHeightInPoints = 12; font9.Boldweight = (short)FontBoldWeight.Normal; style9.SetFont(font9); Icell9.CellStyle = style9; } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(9, 9, 0, 9)); #endregion

#region 確認簽字--簽字 //IRow IRow10 = sheet.CreateRow(10);  //for (int h = 0; h < 2; h++) //{ // ICell Icell10 = IRow10.CreateCell(h); // if (h == 0) // { // Icell10.SetCellValue("當事人簽名:"); // } // else // { // Icell10.SetCellValue("張三"); // }

// ICellStyle style10 = workbook2003.CreateCellStyle(); // //設定單元格的樣式:水平對齊居中 // style10.Alignment = HorizontalAlignment.Right; // Icell10.CellStyle = style10; // //合併單元格 // // sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 9)); //} IRow IRow10 = sheet.CreateRow(10); for (int h = 0; h < 10; h++) { ICellStyle style10 = workbook2003.CreateCellStyle(); ICell Icell10 = IRow10.CreateCell(h); if (h == 0) { Icell10.SetCellValue("當事人簽名:"); style10.Alignment = HorizontalAlignment.Right; style10.BorderLeft = BorderStyle.Thin; } else if (h == 5) { Icell10.SetCellValue("張三"); style10.Alignment = HorizontalAlignment.Left; } else if (h == 9) { style10.BorderRight = BorderStyle.Thin; }

style10.BorderBottom = BorderStyle.Thin;

IFont font10 = workbook2003.CreateFont(); font10.FontName = "宋體"; font10.FontHeightInPoints = 12; font10.Boldweight = (short)FontBoldWeight.Normal; style10.SetFont(font10); Icell10.CellStyle = style10;

} sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(10, 10, 0, 4)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(10, 10, 5, 9));

#endregion

//workbook2003.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, fileName, Excel.XlFixedFormatQuality.xlQualityStandard, true, true, 1, 3, false, Type.Missing); //匯出成PDF格式

FileStream file2003 = new FileStream(@"D:\Person\Solution\Solution\MySln\ConsoleNPOI\資訊核查表.xls", FileMode.Create); workbook2003.Write(file2003); file2003.Close(); //關閉檔案流 workbook2003.Close();

workbook2003.Close();

第一種excel轉pdf

等待使用的是Spire.XLS for.NET元件(可以使用Nuget程式包管理工具新增)

string excelToPDFPath = Path.Combine(path, "資訊核查表.pdf"); Workbook workbook = new Workbook(); workbook.LoadFromFile(excelPath, ExcelVersion.Version97to2003); workbook.SaveToFile(excelToPDFPath, Spire.Xls.FileFormat.PDF);

第二種excel轉pdf(但是專案中可以使用,做成安裝包就不行了,因為引用的Microsoft.Office.Interop.excel.dll,做成安裝包安裝以後預設使用COM元件中的Microsoft.Office.Interop.excel.dll而不是使用安裝目錄下的Microsoft.Office.Interop.excel.dll,只可以debug版本使用)

//Excel檔案轉換成PDF檔案,匯出成PDF格式 //string excelToPDFPath = Path.Combine(path, "資訊核查表.pdf"); //bool isOK = CovertExcelToPDF(excelPath, excelToPDFPath); //string result1 = isOK ? "成功" : "失敗"; //Program.Log.Info($"Excel檔案轉換成PDF檔案匯出{result1}!");

bool isOK = CovertExcelToPDF(@"D:\Person\Solution\Solution\MySln\ConsoleNPOI\資訊核查表.xls", @"D:\Person\Solution\Solution\MySln\ConsoleNPOI\資訊核查表.pdf");

//XSSFWorkbook workbook2007 = new XSSFWorkbook(); //新建xlsx工作簿 //workbook2007.CreateSheet("Sheet1"); //workbook2007.CreateSheet("Sheet2"); //workbook2007.CreateSheet("Sheet3"); ////workbook2007. ////workbook2007.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, fileName, Excel.XlFixedFormatQuality.xlQualityStandard, true, true, 1, 3, false, Type.Missing); //匯出成PDF格式 //FileStream file2007 = new FileStream(@"D:\Person\Solution\Solution\MySln\ConsoleNPOI\Excel2007.pdf", FileMode.Create); //workbook2007.Write(file2007); //file2007.Close(); //workbook2007.Close();

//Workbook workbook = new Workbook(); //workbook.LoadFromFile(@"D:\Person\Solution\Solution\MySln\ConsoleNPOI\Excel2007.xlsx"); //workbook.SaveToFile("輸出.pdf", FileFormat.PDF);

//obook.SaveCopyAs(@"D:\Diagonal.xls");//儲存到指定檔案

//obook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, fileName, Excel.XlFixedFormatQuality.xlQualityStandard, true, true, 1, 3, false, Type.Missing); //匯出成PDF格式

Console.WriteLine("操作EXCEL結束。。。"); Console.Read(); }

/// <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應用程式例項 Excel.ApplicationClass application = null; ////建立工作薄例項 Excel.Workbook workBook = null; try { application = new Excel.ApplicationClass(); ////開啟工作簿 workBook = application.Workbooks.Open(excelPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); ////開啟sheet Excel.Worksheet ws = (Excel.Worksheet)workBook.Worksheets.Item[1]; ////設定列印放放為水平 ws.PageSetup.Orientation = Excel.XlPageOrientation.xlPortrait; ////設定列印時excel內容在一個頁面上顯示。Zoom必須設定為false ws.PageSetup.Zoom = false; ws.PageSetup.FitToPagesTall = 1; ws.PageSetup.FitToPagesWide = 1;

////將工作簿釋出為PDF或XPS格式 ws.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, pdfPath, Excel.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(); // 安全回收程序 //System.GC.GetGeneration(application); } }

} }