C# 对Excel文档打印时的页面设置
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); //表格的权限设定,如只读,密码,权限
//以下是添加新的sheet
Excel.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; //上边距为0
objsheet.PageSetup.BottomMargin = 0; //下边距为0
objsheet.PageSetup.LeftMargin = 0; //左边距为0
objsheet.PageSetup.RightMargin = 0; //右边距为0
objsheet.PageSetup.CenterHorizontally = true;//水平居中
示例代码2:
exce.Application.Workbooks.Add(true);
Workbookbooks=(Excel.Workbook)exce.Workbooks[1];
Excel.Worksheetsheets=(Excel.Worksheet)books.Worksheets[1];
exce.Cells.VerticalAlignment=2;//单元格文字垂直居中
sheets.Cells.Font.Name="宋体";
sheets.Cells.Font.Size=11;
//设置表格标题
sheets.Cells[1,1]="正在生成表格,请稍候.....警告:在生成期间,请不要编辑单元格";
exce.get_Range("A1","P1").MergeCells=true;//合并单元格
exce.get_Range("a1","a1").HorizontalAlignment=3;//水平居中
exce.get_Range("a1","a1").Font.Name="黑体";
exce.get_Range("a1","a1").Font.Size=20;
exce.get_Range("a1","a1").Font.Bold=true;
exce.get_Range("A2","P2").MergeCells=true;
sheets.Cells[2,1]="部门:"+treeView1.SelectedNode.FullPath+
"(共计:"+RowCount.ToString()+"项)"; //设置表格值 ....... exce.get_Range("a3","p3").HorizontalAlignment=3;//列名称居中
sheets.Columns.AutoFit();//设置最合适列宽 ...... exce.get_Range("A3",ENDSELECT).Borders.LineStyle=1;//设置选中单元格的网格线
sheets.Cells.Select();
sheets.Columns.AutoFit();//再次设置最合适列宽 sheets.Cells[1,1]="总帐";
sheets.Cells[2,1]="部门:"+treeView1.SelectedNode.FullPath+
"(共计:"+RowCount.ToString()+"项,合计数量:"+
Numer.ToString()+"合计金额:"+jine.ToString("C2")+"元)";
ENDSELECT="A"+(RowCount+3);
exce.get_Range("A3",ENDSELECT).HorizontalAlignment=3;//设置序号列居中
ENDSELECT="G"+(RowCount+3);
exce.get_Range("G4",ENDSELECT).NumberFormatLocal="#,##0.00_";//设置金额列为货币式
exce.get_Range("B4","B4").Select();
exce.ActiveWindow.FreezePanes=true;//冻结窗口
//页面设置
exce.ActiveWindow.DisplayGridlines=false;//不显示网格线
sheets.DisplayAutomaticPageBreaks=true;//显示分页线
sheets.PageSetup.CenterFooter="第&P页,共&N页";
sheets.PageSetup.TopMargin=exce.InchesToPoints(0.590551181102362);//上1.5
sheets.PageSetup.BottomMargin=exce.InchesToPoints(0.590551181102362);//下1.5
sheets.PageSetup.LeftMargin=exce.InchesToPoints(0.78740157480315);//左边距2
sheets.PageSetup.RightMargin=exce.InchesToPoints(0.393700787401575);//右边距1
sheets.PageSetup.HeaderMargin=exce.InchesToPoints(0.393700787401575);//页眉1
sheets.PageSetup.FooterMargin=exce.InchesToPoints(0.393700787401575);//页脚1
sheets.PageSetup.CenterHorizontally=true;//水平居中
sheets.PageSetup.PrintTitleRows="$1:$3";//顶端标题行
sheets.PageSetup.PaperSize=Excel.XlPaperSize.xlPaperA3;//.xlPaperB4;//纸张大小
sheets.PageSetup.Orientation=Excel.XlPageOrientation.xlLandscape;//纸张方向.横向
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[];
sheet.PageSetup.PrintArea = "A7:T8";
sheet.PageSetup.PrintTitleRows = "$1:$1";
sheet.PageSetup.FitToPagesWide = ;
sheet.PageSetup.FitToPagesTall = ;
//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 = ;
dialog.PrinterSettings.ToPage = ;
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.
3. c#的excel边距设置, 2008-6.
扩展阅读
3. C#读写xml文件
C# 对Excel文档打印时的页面设置的更多相关文章
- C# 设置Excel打印选项及打印excel文档
C# 设置Excel打印选项及打印excel文档 打印Excel文档是一个很常见的操作,但有时候我们会碰到各种不同的打印需求,例如只打印一个Excel工作表的其中一部分,或打印时每页都有表头,或把工作 ...
- struts2中利用POI导出Excel文档并下载
1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: package com.tydic.eshop.action.feedback; impor ...
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- 一次Mono解析Excel文档编码出错排查记录
最近在捯饬Asp.Net站点部署到Linux平台上面,在文档导入的操作中经过网上搜索采用了能够支持跨平台的ExcelDataReader组建.在本地windows上测试通过NuGet安装的组建,这货依 ...
- GrapeCity Documents for Excel 文档API组件 V2.2 新特性介绍
GrapeCity Documents for Excel 文档API组件 V2.2 正式发布,本次新版本包含诸多重量级产品功能,如:将带有形状的电子表格导出为 PDF.控制分页和电子表格内容.将Ex ...
- 四种方法 恢复损坏的Excel文档
四种方法 恢复损坏的Excel文档 打开一个以前编辑好的Excel工作簿,却发现内容混乱,无法继续进行编辑,而且还不能够进行打印.这是很多朋友在处理Excel文件时都可能会遇到的一个问题,面对这种情况 ...
- C#中5步完成word文档打印的方法
在日常工作中,我们可能常常需要打印各种文件资料,比如word文档.对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作.特别是提到Web打印,这的确会很棘手.一般如果要想选 ...
- 基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理
http://www.cnblogs.com/wuhuacong/p/4175266.html 在一般的管理系统模块里面,越来越多的设计到一些常用文档的上传保存操作,其中如PDF.Word.Excel ...
- Python处理Excel文档(xlrd, xlwt, xlutils)
简介 xlrd,xlwt和xlutils是用Python处理Excel文档(*.xls)的高效率工具.其中,xlrd只能读取xls,xlwt只能新建xls(不可以修改),xlutils能将xlrd.B ...
随机推荐
- Spring之在客户端访问RESTful业务
Spring之在客户端访问RESTful业务 RestTemplate 是客户端访问RESTful业务的核心类.在概念上与Spring其他的模板类相似,比如JdbcTemplate和JmsTempla ...
- Burpsuite如何抓取使用了SSL或TLS传输的Android App流量
一.问题分析 一般来说安卓的APP端测试分为两个部分,一个是对APK包层面的检测,如apk本身是否加壳.源代码本身是否有恶意内嵌广告等的测试,另一个就是通过在本地架设代理服务器来抓取app的包分析是否 ...
- Html5 Geolocation demo
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>HTML5 Geoloca ...
- ADODB.Connection 错误 '800a0e7a'。。
今天帮同学调程序的时候发现的:错误提示如下: ADODB.Connection 错误 '800a0e7a' 未找到提供程序.该程序可能未正确安装. /hua1/manage/inc/conn.asp, ...
- opencv显示鼠标所在位置的rgb值
#include"highgui.h" #include"cv.h" #include"cxcore.h" #include<stdl ...
- jenkins-slave的搭建和使用
一 什么是Jenkins的分布式构建和部署 Jenkins的分布式构建,在Jenkins的配置中叫做节点,分布式构建能够让同一套代码或项目在不同的环境(如:Windows和Linux系统)中编译.部署 ...
- 利用SOLR搭建企业搜索平台 之——配置文件
运行solr是个很简单的事,如何让solr高效运行你的项目,这个就不容易了.要考虑的因素太多.这里很重要一个就是对solr的配置要了解.懂得配置文件每个配置项的含义,这样操作起来就会如鱼得水! 在so ...
- R.id.layout等不能识别:cannot be resolved or is not a field
Do not modify the R class. The error means there's something syntactically wrong with your XML layou ...
- Webservce、WCF、WebApi的区别
Web Service It is based on SOAP and return data in XML form. It support only HTTP protocol. It is no ...
- 一、导入、导出远程Oracle数据库
一.导入.导出远程Oracle数据库 其语法实示例如下: imp/exp [username[/password[@service]]] 其中service是服务实例名,关于如何创建服务实 ...