標籤:
1、對列印頁面的朝向,頁寬,頁高進行設定
參考源碼[1]
using Excel = Microsoft.Office.Interop.Excel;Excel.Application tmpExcel = new Excel.ApplicationClass();Excel.Workbook tmpbook = tmpExcel.Workbooks.Open(tmppath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //表格的許可權設定,如唯讀,密碼,許可權
//以下是添加新的sheetExcel.Worksheet objsheet = (Excel.Worksheet)tmpbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing);objsheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;//頁面方向橫向objsheet.PageSetup.Zoom = false; //列印時版面設定,必須設定為false,下面的二行頁高,頁寬才有效objsheet.PageSetup.FitToPagesTall = 1; //頁高objsheet.PageSetup.FitToPagesWide = 1; //頁寬 objsheet.PageSetup.Zoom = 75;//列印時版面設定,縮放比例objsheet.PageSetup.TopMargin = 0; //上邊距為0objsheet.PageSetup.BottomMargin = 0; //下邊距為0objsheet.PageSetup.LeftMargin = 0; //左邊距為0objsheet.PageSetup.RightMargin = 0; //右邊距為0objsheet.PageSetup.CenterHorizontally = true;//水平置中
2、列印選項及列印文檔[2]
列印Excel文檔是一個很常見的操作,但有時候我們會碰到各種不同的列印需求, 例如只列印一個Excel工作表的其中一部分,或列印時每頁都有表頭,或把工作表中超出1頁所有內容列印到1頁上等等,這時我們需要對Excel的列印選 項進行設定。這篇文章主要是分享如何使用Excel組件及C#來設定一些常見的Excel列印選項及列印Excel文檔。
下面這個Excel工作表共含有17行,20列資料:
目標:將第7, 8行的所有資料列印到一頁上,並列印表頭(標題列)。
建立一個WinForm項目,使用如下命名空間:
using System;using System.Drawing.Printing;using System.Windows.Forms;using Spire.Xls;
步驟1:建立一個新的workbook對象並載入Excel文檔。
Workbook workbook = new Workbook();workbook.LoadFromFile("Sample.xlsx");
步驟2:擷取該Excel文檔的第一個工作表,並設定列印選項。
Worksheet sheet = workbook.Worksheets[0];
下面列出幾個常設定的列印選項:
設定列印範圍/範圍:
sheet.PageSetup.PrintArea = "A7:T8";
設定列印表頭(標題列):
sheet.PageSetup.PrintTitleRows = "$1:$1";
設定excel工作表縮放為一頁寬一頁高:
sheet.PageSetup.FitToPagesWide = 1;sheet.PageSetup.FitToPagesTall = 1;
這裡可以設定它們的值為0或1來改變列印效果以滿足不同需求。
除此之外還可以設定頁面方向及列印頁面大小等:
設定頁面方向:
sheet.PageSetup.Orientation = PageOrientationType.Portrait;
設定列印頁面大小:
sheet.PageSetup.PaperSize = PaperSizeType.PaperA3;
步驟3:建立一個新的PrintDialog對象,設定dialog屬性及列印頁面範圍並列印文檔。
PrintDialog dialog = new PrintDialog();dialog.AllowPrintToFile = true;dialog.AllowCurrentPage = true;dialog.AllowSomePages = true;dialog.AllowSelection = true;dialog.UseEXDialog = true;dialog.PrinterSettings.Duplex = Duplex.Simplex;dialog.PrinterSettings.FromPage = 0;dialog.PrinterSettings.ToPage = 8;dialog.PrinterSettings.PrintRange = PrintRange.SomePages;workbook.PrintDialog = dialog;PrintDocument pd = workbook.PrintDocument;if (dialog.ShowDialog() == DialogResult.OK){ pd.Print(); }
運行程式會出現如下對話方塊:
這裡我選擇Microsoft XPS Document Writer將這個excel文檔列印為XPS格式,得到的XPS檔案如下:
全部代碼:
using System;using System.Drawing.Printing;using System.Windows.Forms;using Spire.Xls; namespace Print_Excel_in_csharp{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); Worksheet sheet = workbook.Worksheets[0]; sheet.PageSetup.PrintArea = "A7:T8"; sheet.PageSetup.PrintTitleRows = "$1:$1"; sheet.PageSetup.FitToPagesWide = 1; sheet.PageSetup.FitToPagesTall = 1; //sheet.PageSetup.Orientation = PageOrientationType.Landscape; //sheet.PageSetup.PaperSize = PaperSizeType.PaperA3; PrintDialog dialog = new PrintDialog(); dialog.AllowPrintToFile = true; dialog.AllowCurrentPage = true; dialog.AllowSomePages = true; dialog.AllowSelection = true; dialog.UseEXDialog = true; dialog.PrinterSettings.Duplex = Duplex.Simplex; dialog.PrinterSettings.FromPage = 0; dialog.PrinterSettings.ToPage = 8; dialog.PrinterSettings.PrintRange = PrintRange.SomePages; workbook.PrintDialog = dialog; PrintDocument pd = workbook.PrintDocument; if (dialog.ShowDialog() == DialogResult.OK) { pd.Print(); } } }}
參考博文
1. brian0031. C#列印版面設定(橫向,頁寬,頁高), 2012-03.
2. E-iceblue. C# 設定Excel列印選項及列印excel文檔,2016-05.
擴充閱讀
1. C# 合并及拆分PDF檔案
2. C# 給Word文檔新增內容控制項
C# 對Excel文檔列印時的版面設定