Spring POI 从模板生成Excel并打印

Spring POI 从模板生成Excel并打印


提醒:

  • java运行在服务器端,使用java调用打印时,外部连接服务器访问打印程序,调用的是服务器端的打印机。

  • 如果想调用本地打印机可以使用JavaScript。

理想:

用户->服务器: 我要打印报表
Note right of 服务器: 收到请求,调用打印程序
服务器-->用户: 弄好了,你打印吧
Note left of 用户: 调用打印机,打印报表 

实际:

用户->服务器: 我要打印报表
Note right of 服务器: 收到请求,调用打印程序
Note right of 服务器: 怎么调用了我自己的打印机,是java的锅,我不背
服务器-->用户: 很抱歉,报表在我这打印好了,你过来取吧!
Note left of 用户: 你大爷的! 

项目需求:从数据库中生成报表并打印。之前已经写好报表导出成Excel模块,所以我想在此基础上进行扩充,先生成Excel文件,再将该文件打印。由于本次项目使用Chrome,如果是IE浏览器可以使用自带打印功能,但是Chrome并没有。下面是解决过程:

Java自带打印功能

在查询资料工程中得知java自带打印功能,但是不能打印office文档,可以打印PDF。
在测试过程中发现PDF也打印不了,不知道是什么原因。
虽然打印类型可选DocFlavor.INPUT_STREAM.PDF,或者自动识别DocFlavor.INPUT_STREAM.AUTOSENSE,但是打印PDF时还是会抛异常(使用PDF打印机测试):

Exception in thread "main" java.lang.IllegalArgumentException: services must be non-null and non-empty
    at javax.print.ServiceUI.printDialog(ServiceUI.java:167)
    at com.srie.util.test.TestPrinter.main(TestPrinter.java:32)

打印图片正常,可以设置打印参数,代码如下:(忘记是从哪位朋友博客里看到的了o(╯□╰)o)

package com.srie.util.test;

import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;

public class TestPrinter {
    public static void main(String[] args) {
        JFileChooser fileChooser = new JFileChooser(); // 创建打印作业
        int state = fileChooser.showOpenDialog(null);
        if (state == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile(); // 获取选择的文件
            // 构建打印请求属性集
            HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            // 设置打印格式,打印JPG图片
            DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
            // 查找所有的可用的打印服务
            PrintService printService[] = PrintServiceLookup
                    .lookupPrintServices(flavor, pras);
            // 定位默认的打印服务
            PrintService defaultService = PrintServiceLookup
                    .lookupDefaultPrintService();
            // 显示打印对话框
            PrintService service = ServiceUI.printDialog(null, 200, 200,
                    printService, defaultService, flavor, pras);
            if (service != null) {
                try {
                    DocPrintJob job = service.createPrintJob(); // 创建打印作业
                    FileInputStream fis = new FileInputStream(file); // 构造待打印的文件流
                    DocAttributeSet das = new HashDocAttributeSet();
                    Doc doc = new SimpleDoc(fis, flavor, das);
                    job.print(doc, pras);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

<center>


Java打印功能

</center>

JACOB - Java COM Bridge

下载链接:http://pan.baidu.com/s/1jHVWps6
使用jacob可以方便的操作office文档,甚至打印,而我的关注点在打印上。

1. 使用jacob首先要将jar包对应的dll文件放到“某个目录”下。

有说放在Windows/system32下的,有说放在%JAVA_HOME/jre/bin%下的。经我测试得到的结果是Windows7系统只在%JAVA_HOME/jre/bin%放一份dll文件即可,Windows10不需要dll文件。
在使用过程中我遇到一个问题,同样的代码在两台电脑上一个打印成功,一个抛异常。

Windows7_64位系统:
- office2007
- eclipse_32位
- jdk1.8_32位
%JAVA_HOME/jre/bin%下放入×64dll文件后打印正常。

Windows10_64位系统:

  • office2016
  • eclipse_32位
  • jdk1.8_32位
    %JAVA_HOME/jre/bin%下放入dll文件后打印抛如下异常:
java.lang.NoSuchMethodError: com.jacob.com.Dispatch.put(Lcom/jacob/com/Dispatch;Ljava/lang/String;Ljava/lang/Object;)V

换成jdk1.8_64位后报错如下:

java.lang.NoClassDefFoundError: Could not initialize class com.jacob.com.ComThread

原因:jar包对应的dll文件是32位,应该用64位,换成64位打印正常。

Windows10_64位系统:

  • office2007
  • IDEA2017.1
  • jdk1.8_32位
    添不添加dll文件打印时都会抛如下异常:
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x77a282a5, pid=14440, tid=15272
#
# JRE version: Java(TM) SE Runtime Environment (8.0_31-b13) (build 1.8.0_31-b13)
# Java VM: Java HotSpot(TM) Client VM (25.31-b07 mixed mode windows-x86 )
# Problematic frame:
# C  [ntdll.dll+0x782a5]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\tools\tomcat-7.0.57\bin\hs_err_pid14440.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

**Windows10_64位系统:

  • office2007
  • IDEA2017.1
  • jdk1.8_64位
    无需dll文件打印正常。**

在遇到抛异常的情况的时候,检查代码无错,请确认JDK与IDE的版本,dll文件需和jar包对应,位数和电脑系统对应。

2. 在项目中添加jar包依赖。

由于项目采用maven管理,将jacob-1.18.jar放到WEB-INF/lib下,在pom.xml中添加如下代码后maven更新项目。

<!-- 打印依赖的jar包 -->
<dependency>
    <groupId>com.jacob</groupId>
    <artifactId>jacob</artifactId>
    <version>1.18</version>
    <scope>system</scope>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/jacob-1.18.jar</systemPath>
</dependency>

3. 代码

首先感谢甲方公司的chykong大神,代码是基于他写的方法上完成的。

打印参数类:PrintsVo

package com.srie.common.vo;
/**
 * 打印页面设置条件
 * @author 王晓安
 * @date 2017-7-11
 */
public class PrintVo {
    
    private final double rate = 0.393700787; //1厘米=0.393700787英寸

    private short zzxh; //纸张型号,如A4
    private boolean dyfx; //打印方向,true为横向,false为竖向
    private double topMargin; //页面上边距
    private double bottomMargin; //页面下边距
    private double leftMargin; //页面左边距
    private double rightMargin; //页面右边距
    
    public short getZzxh() {
        return zzxh;
    }

    public void setZzxh(short zzxh) {
        this.zzxh = zzxh;
    }

    public boolean getDyfx() {
        return dyfx;
    }

    public void setDyfx(boolean dyfx) {
        this.dyfx = dyfx;
    }

    public double getTopMargin() {
        return topMargin;
    }

    public void setTopMargin(double topMargin) {
        this.topMargin = topMargin * rate;
    }

    public double getBottomMargin() {
        return bottomMargin;
    }

    public void setBottomMargin(double bottomMargin) {
        this.bottomMargin = bottomMargin * rate;
    }

    public double getLeftMargin() {
        return leftMargin;
    }

    public void setLeftMargin(double leftMargin) {
        this.leftMargin = leftMargin * rate;
    }

    public double getRightMargin() {
        return rightMargin;
    }

    public void setRightMargin(double rightMargin) {
        this.rightMargin = rightMargin * rate;
    }

    @Override
    public String toString() {
        return "PrintVo [zzxh=" + zzxh + ", dyfx=" + dyfx + ", topMargin=" + topMargin + ", bottomMargin="
                + bottomMargin + ", leftMargin=" + leftMargin + ", rightMargin=" + rightMargin + "]";
    }
    
}

打印方法类:Printer

package com.srie.util.test;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.srie.common.vo.PrintVo;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

import java.io.*;

/**
 * 打印测试
 * @author 王晓安
 * 2017年7月16日
 */
public class Printer {

    //打印文件依赖的jacob-1.18.jar需要在jre路径下放jacob-1.18-x86.dll文件
    static {
        FileOutputStream os = null;
        FileInputStream in = null;
        //获取Windows版本,取得对应dll文件
        String systemBit = System.getProperties().get("os.arch").toString().substring(3); //amd64 or amd32
        String dllFile = "";
        if (systemBit.equals("64"))
            dllFile = "jacob-1.18-x64.dll";
        else
            dllFile = "jacob-1.18-x86.dll";
        //jre目录下bin目录,将web-inf下lib文件里的dll文件放到这里,打印需要这个文件
        File jreBinDll = new File(System.getProperty("java.home") + "/bin/" + dllFile);
        //打印所需的dll文件路径(系统根目录)
        File printDLL = new File(Thread.currentThread().getContextClassLoader().getResource("").getPath() + "../../" + dllFile);
        if (!jreBinDll.exists()) {
            try {
                in = new FileInputStream(printDLL);
                os = new FileOutputStream(jreBinDll);
                byte[] tempArr = new byte[(int) printDLL.length()];
                in.read(tempArr);
                os.write(tempArr);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 打印报表,后台调取数据并生成Excel文件参考自exportRpt方法,
     * 不同之处是将生成的Excel以文件形式直接保存在本地,并调用系统默认打印机打印生成的Excel
     * @author 王晓安
     * @param data 需要转换成Excel的二维数组
     * @param srcFile 报表模板
     * @param file_name 生成的Excel文件名
     * @param titleRows 标题栏行数
     * @param alignment Excel每列的对齐方式数组
     * @param printVo 打印参数
     */
    public static void print(String[][] data, String srcFile, String file_name, int titleRows, short[] alignment, PrintVo printVo) {
        FileOutputStream os = null;
        FileInputStream in = null;
        //临时文件夹,用于保存将要打印的Excel文件
        File tempFolder= new File("D:/tempPrint");
        //Excel文件名
        String fileName = "temp.xls";
        //保存将要打印的excel
        File printFile = null;
        try {
            in = new FileInputStream(srcFile);
            Workbook work = new HSSFWorkbook(in);
            HSSFSheet sheet = (HSSFSheet) work.getSheetAt(0);
            sheet.getPrintSetup().setLandscape(printVo.getDyfx());// 打印方向,true:横向,false:纵向(默认)
            sheet.setMargin(HSSFSheet.TopMargin, printVo.getTopMargin());// 页边距(上)
            sheet.setMargin(HSSFSheet.BottomMargin, printVo.getBottomMargin());// 页边距(下)
            sheet.setMargin(HSSFSheet.LeftMargin, printVo.getLeftMargin());// 页边距(左)
            sheet.setMargin(HSSFSheet.RightMargin, printVo.getRightMargin());// 页边距(右)
            sheet.setVerticallyCenter(false);//设置Excel是否垂直居中
            sheet.setHorizontallyCenter(false);//设置Excel是否水平居中
            //设置打印纸张
            HSSFPrintSetup ps = sheet.getPrintSetup();
            ps.setPaperSize(printVo.getZzxh());
            // 数据样式
            Font fontData = work.createFont();
            fontData.setFontHeightInPoints((short) 11);
            fontData.setFontName("宋体");
            fontData.setBoldweight((short) 1);

            Font fontDataBold = work.createFont();
            fontDataBold.setFontHeightInPoints((short) 10);
            fontDataBold.setFontName("宋体");
            fontDataBold.setBoldweight(Font.BOLDWEIGHT_BOLD);
            CellStyle cellDataStyleBold = work.createCellStyle();
            cellDataStyleBold.setFont(fontDataBold);
            cellDataStyleBold.setBorderBottom((short) 1);
            cellDataStyleBold.setBorderLeft((short) 1);
            cellDataStyleBold.setBorderRight((short) 1);
            cellDataStyleBold.setBorderTop((short) 1);
            cellDataStyleBold.setFillPattern(CellStyle.SOLID_FOREGROUND);
            cellDataStyleBold.setFillForegroundColor(HSSFColor.WHITE.index);
            cellDataStyleBold.setAlignment(CellStyle.ALIGN_CENTER);
            cellDataStyleBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
            cellDataStyleBold.setWrapText(true);

            //填充单元格样式
            CellStyle cellTextStyle = work.createCellStyle();
            cellTextStyle.setFont(fontData);
            cellTextStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
            cellTextStyle.setWrapText(true);

            // 得到行,并填充数据和表格样式
            for (int i = 0; i < data.length; i++) {
                Row row = sheet.createRow(i + titleRows);// 创建新行
                for (int j = 0; j < data[i].length; j++) {
                    cellDataStyleBold.setAlignment(alignment[j]);
                    String str = data[i][j];
                    Cell cell = row.createCell(j);// 得到第j个单元格
                    cell.setCellStyle(cellDataStyleBold);// 填充样式
                    cell.setCellValue(str);// 填充值
                }
            }
            //如果文件夹不存在,则创建该临时文件夹
            if (!tempFolder.exists()) {
                tempFolder.mkdirs();
            }
            //如果文件夹还不存在,可能该用户电脑没有D盘,在C盘创建相应文件夹
            if (!tempFolder.exists()) {
                tempFolder = new File(tempFolder.getPath().replace('D', 'C'));
                tempFolder.mkdirs();
            }
            printFile = new File(tempFolder, fileName);
            os = new FileOutputStream(printFile);
            work.write(os);
            os.flush();
            work.close();
            //初始化COM线程
            ComThread.InitSTA();
            //新建Excel对象
            ActiveXComponent xl = new ActiveXComponent("Excel.Application");
            //设置打印时不打开文档,true为打开文档
            Dispatch.put(xl, "Visible", new Variant(false));
            //打开具体的工作簿
            Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
            // 打开文档
            Dispatch excel = Dispatch.call(workbooks, "Open", printFile.getPath()).toDispatch();
            // 开始打印
            Dispatch.call(excel, "PrintOut");
            xl.invoke("Quit", new Variant[] {});
        } catch (FileNotFoundException e) {
            System.out.println("文件路径错误");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("文件输入流错误");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 释放资源
            ComThread.Release();
        }
    }
}

打印测试类:TestPrinter

package com.srie.util.test;

import com.srie.common.vo.PrintVo;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.PrintSetup;

public class TestPrinter {
    public static void main(String[] args) {
        PrintVo printVo = new PrintVo(); //设置打印参数的类
        printVo.setTopMargin(1); //上边距 1cm
        printVo.setBottomMargin(1); //下边距 1cm
        printVo.setLeftMargin(1); //左边距 1cm
        printVo.setRightMargin(1); //右边距 1cm
        printVo.setDyfx(true); //打印方向,true横向,false纵向
        printVo.setZzxh(PrintSetup.A4_PAPERSIZE); //打印纸张型号A4, /** A4 - 210x297 mm */short A4_PAPERSIZE = 9;
        short[] alignment = new short[]{CellStyle.ALIGN_CENTER, CellStyle.ALIGN_CENTER}; //short ALIGN_CENTER = 0x2;
        String srcFile = "D:/测试模板.xls";//定义报表模板
        String[][] data = new String[][]{{"Lisa", "22"}, {"Tony", "21"}};
        Printer printer = new Printer();
        String fileName = "测试";
        printer.print(data, srcFile, fileName, 1, alignment, printVo);
    }
}

测试结果:
<center>


测试结果

hhhh
</center>

可以写一个前台页面,用来传递打印参数和调取打印方法。
<center>


打印设置

</center>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,198评论 6 514
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,334评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,643评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,495评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,502评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,156评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,743评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,659评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,200评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,282评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,424评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,107评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,789评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,264评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,390评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,798评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,435评论 2 359

推荐阅读更多精彩内容