当前位置: 移动技术网 > IT编程>开发语言>.net > C# winform打印excel的方法

C# winform打印excel的方法

2017年12月07日  | 移动技术网IT编程  | 我要评论

龙非世间物,河南省汝州市,德云社2010封箱

前言

c#做winform程序要求生成并打印excel报告,为了不安装office相应组件,我选择了npoi来生成excel报告,用winform的printdocument控件来触发打印操作,而难点在于如何将excel转换成graphics对象,在npoi中我只找到了excel打印的设置(如横向/纵向),还需要打开excel去触发打印操作,但项目要求是一次性直接实现打印,要用printdocument控件而不是再去操作excel。不得已重新搜索,发现了类库spire.xls,最终实现了要求。有什么错误或更简洁的方法还请广大博友不吝赐教。

思路

用npoi生成excel =》 用spire.xls转换成图片.png =》 获取生成的图片,利用graphic的drawimage方法对图片进行操作,当然了,如果不介意的话,可以不对图片进行操作:) =》使用printdocument控件的print方法实现打印( 当然这里可以直接用spire.xls直接生成excel,但是spire.xls的免费版本有每个sheet最多有150行的限制 )

知识点

【0】npoi使用方法

下载:

【1】winform的printdocument控件

参考:

理解:

printdocument控件的作用是定义一个向打印机发送输出的对象,有beginprint,printpage,endprint方法,当执行printdocument.print()语句时,会检查是否存在beginprint方法,存在则先调用。然后调用printpage方法,该方法会向printpageecentargs类中获得一个空白的graphics对象,我们可以对graphics对象进行操作(可以理解对画布进行绘制,graphics对象有内置的类似drawstring,drawpie的方法),在printpage方法的最后,会向打印设备发送这个graphics对象。打印设备完成打印后,会检查是否存在endprint方法,存在则调用。

【2】spire.xls使用方法

参考:.net读写excel工具spire.xls使用入门教程(1)

具体代码

【1】winform使用的打印控件介绍

使用的插件截图

printdocument1作用:定义一个向打印机发送输出的对象

printpreviewdialog1作用:显示一个对话框,向用户显示关联文档打印的样子

printdialog1作用:显示一个对话框,允许用户选择打印机并选择其他打印选项(如分数,纸张方向)

【2】代码展示

“直接打印” 按钮的后台设置

private void directprint_click(object sender, eventargs e)
{
isprint = false;
generateexcel_click(sender, e); //使用npoi生成excel
if (newsavefilepath != "" && isprint==true)
{
isprint = false; 
changeexcel2image(newsavefilepath); //利用spire将excel转换成图片
if (printdialog1.showdialog() == dialogresult.ok)
{ 
printdocument1.print(); //打印
}
} 

}

"生成excel" 按钮后台设置

private void generate_click(object sender, eventargs e)
{
createexcel(); //使用npoi生成excel内容
savefiledialog savedialog = new savefiledialog(); //弹出让用户选择excel保存路径的窗口
savedialog.filter = " excel files(*.xlsx)|*.xlsx|all files(*.*)|*.*";
savedialog.restoredirectory = true;
savedialog.filename = string.format("销售订单审批单{0}", datetime.now.tostring("yyyymmddhhmm"));
if (savedialog.showdialog() == dialogresult.ok)
{
//newsavefilepath是excel的保存路径
newsavefilepath = savedialog.filename.tostring().trim();
using (filestream newfs = new filestream(newsavefilepath, filemode.create, fileaccess.readwrite))
{
singlexssfwk.write(newfs); //将生成的excel写入用户选择保存的文件路径中
newfs.close();
}
} 
}

createexcel()方法举例

using npoi.xssf.usermodel;

xssfworkbook singlexssfwk;

//注意,不同的npoi版本调用的方法不一致,这里使用的版本是2.1.3.1
private void createxcel() 
{ 
//获取模板excel的路径
string str = system.environment.currentdirectory + "\\xxxx.xlsx";
if (file.exists(str))
{
using (filestream fs = new filestream(str, filemode.open, fileaccess.read))
{
singlexssfwk = new xssfworkbook(fs);
fs.close();
}
//获取表
xssfsheet xssfsheet = (xssfsheet)singlexssfwk.getsheetat(0);
//创建行 
xssfrow xssfrow1 = (xssfrow)xssfsheet.getrow(1);
//设置单元格内容
xssfrow1.getcell(0).setcellvalue("...");
... ...
    }
else{
... ...
}
}

changeexcel2image()方法举例

using spire.xls; 
public void changeexcel2image(string filename)
{
workbook workbook = new workbook();
workbook.loadfromfile(filename);
worksheet sheet = workbook.worksheets[0];
sheet.savetoimage(imagepath); //图片后缀.bmp ,imagepath自己设置
}

执行printdocument1.print()的方法会调用printdocument1_printpage方法

//在printpage方法中写截取图片 的代码
private void printdocument1_printpage(object sender, system.drawing.printing.printpageeventargs e)
{ 
#region 如果不需要截取图片,可以不用写以下代码
gc.collect();
graphics g = e.graphics; 
//imagepath是指 excel转成的图片的路径
using (bitmap bitmap = new dbitmap(imagepath))
{
//如何截取自己摸索
rectangle newarea = new rectangle();
newarea.x = 0;
newarea.y = 50;
newarea.width = bitmap.width;
newarea.height = bitmap.height - 120;
using (bitmap newbitmap = bitmap.clone(newarea, bitmap.pixelformat))
{
g.drawimage(newbitmap, 0, 0, newbitmap.width - 200, newbitmap.height - 150);
}
} 
#endregion
}

备注:关于预览的设置,关键在于printdocument1是个全局对象,直接设置printpreviewdialog1.document = printdocument1; printpreviewdialog1.showdialog();即可,这个的原理没有研究,反正这样设置后,只要你处理好内容,就能在预览中看到。

总结

以上就是我实现c# winform打印excel的经过,或许这只是一种比较冗杂的方法,如果有大佬有更精简的方法,请不吝赐教。如有错误,也请不吝赐教。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网