JavaScript打印Excel、Word

JavaScript调用本地打印机,打印Excel、Word文件

之前写过一篇文章,使用java调用打印机打印Excel文件:
java调用打印机:http://blog.csdn.net/wangxiaoan1234/article/details/76032280
但是java运行与服务器上,外部访问无法调用本地打印机。
假设项目需求为:点击按钮打印某个报表(Excel)。当项目发布到服务器上后,当使用java调用打印机,无论哪台电脑访问页面,点击打印按钮,调用的都是java所运行的服务器上的打印机。

理想:

Created with Raphaël 2.1.0 用户 用户 服务器 服务器 我要打印报表 收到请求,调用打印程序 弄好了,你打印吧 调用打印机,打印报表

实际:

Created with Raphaël 2.1.0 用户 用户 服务器 服务器 我要打印报表 收到请求,调用打印程序 怎么调用了我自己的打印机,是java的锅,我不背 很抱歉,报表在我这打印好了,你过来取吧! 你大爷的!

解决思路:
JavaScript运行在本地,使用JavaScript调用本地打印机。

想到可行方法:

  • 直接调用打印机打印本地文件(IE浏览器用此方法)。
  • java后台将Excel转换成html页面,调用window.print()函数打印整个页面(通用);
  • 安装打印插件(麻烦)

使用Spring poi将Excel、Word转换为html再进行打印的好处有:

  1. 所有浏览器通用
  2. 可以进行打印预览
  3. 可视化的打印参数设置
  4. 我不会别的o(╯□╰)o

IE浏览器直接调用打印机(无预览):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>打印测试</title>
</head>
<body>
    <input id="btnPrint" value="打印整个页面" type="button" onclick="btnPrintClick()"/>  
    <input id="btnPrintExcel" value="打印Excel" type="button" onclick="printExcel('D:/test.xlsx')"/> 
    <script> //调用浏览器的打印功能  function btnPrintClick(){  window.print(); } //打印Excel function printExcel(obj) {  var explorer = window.navigator.userAgent ; if (explorer.indexOf("MSIE") >= 0 || "ActiveXObject" in window) { //判断是否为ie浏览器 var xlsApp = null; try { xlsApp = new ActiveXObject('Excel.Application'); } catch(e) { alert(e + ', 原因分析: 浏览器安全级别较高导致不能创建Excel对象或者客户端没有安装Excel软件'); return; } var xlBook = xlsApp.Workbooks.Open(obj); var xlsheet = xlBook.Worksheets(1); xlsApp.Application.Visible = false; xlsApp.visible = false; xlsheet.Printout; xlsApp.Quit(); xlsApp=null; } else { alert("只支持IE浏览器"); } } </script>
</body>
</html>

Java将Excel解析成html,在该页面上调用window.print()打印页面:

maven依赖:

<!-- POI -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.lucee/curvesapi -->
        <dependency>
            <groupId>org.lucee</groupId>
            <artifactId>curvesapi</artifactId>
            <version>1.04.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.16</version>
        </dependency>

jar包下载地址:http://download.csdn.net/detail/wangxiaoan1234/9909138

后台Excel转换HTML类:
类文件下载地址:http://download.csdn.net/detail/wangxiaoan1234/9909123

package com.srie.util.excel;

import org.apache.poi.hssf.converter.ExcelToHtmlConverter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

/** * 利用POI将Excel2003转换为HTML(不能读取图片并且不支持Excel2007) */
public class PoiExcel03lToHtml { 

    /** * 程序入口方法 * @param excelPath 待读取的Excel路径 * @param htmlPath 转换生成的HTML输出路径 */
    public static void convertExcelToHtml(String excelPath, String htmlPath) {
        File excelFile = new File(excelPath);
        File htmlFile = new File(htmlPath);
        File htmlFolder = htmlFile.getParentFile();
        InputStream is = null;
        OutputStream out = null;
        StringWriter writer = null;
        String content = null;
        try{
            if(excelFile.exists()){
                if(!htmlFolder.exists()){
                    htmlFolder.mkdirs();
                }
                is = new FileInputStream(excelFile);
                HSSFWorkbook workBook = new HSSFWorkbook(is);
                ExcelToHtmlConverter converter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
                //设置不输出行号(1 2 3...)及列标(A B C...)等
                converter.setOutputColumnHeaders(false);
                converter.setOutputHiddenColumns(false);
                converter.setOutputColumnHeaders(false);
                converter.setOutputLeadingSpacesAsNonBreaking(false);
                converter.setOutputRowNumbers(false);
                converter.processWorkbook(workBook);

                writer = new StringWriter();
                Transformer serializer = TransformerFactory.newInstance().newTransformer();
                serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                serializer.setOutputProperty(OutputKeys.INDENT, "YES");
                serializer.setOutputProperty(OutputKeys.METHOD, "HTML");
                serializer.transform(
                        new DOMSource(converter.getDocument()),
                        new StreamResult(writer) );
                out = new FileOutputStream(htmlFile);
                content = writer.toString();
                //替换掉Sheet1 Sheet2 Sheet3...
                content = content.replaceAll("<h2>Sheet[\\d]</h2>", "")
                        .replaceAll("<h2>第[一二三四五六七八九十壹贰叁肆伍陆柒捌玖拾]页</h2>", "");

                out.write(content.getBytes("UTF-8"));
                out.flush();
                out.close();
                writer.close();
            }
        } catch (IOException | ParserConfigurationException | TransformerException e) {
            e.printStackTrace();
        } finally{
            try{
                if(is != null){
                    is.close();
                }
                if(out != null){
                    out.close();
                }
                if(writer != null){
                    writer.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

class TestExcel {
    public static void main(String[] args) throws Exception {
        PoiExcel03lToHtml.convertExcelToHtml("D:/test.xls", "D:/test.html");
    }
}

生成html后就可以各种调用打印了。
我采用的方法是:

  1. 前台ajax访问后台转换方法
  2. 后台返回第58行的content字符串,这个字符串就是整个html页面代码。
  3. 将ajax的返回结果写入到一个新页面,然后打印这个新页面。

前台js代码:

/** * 打印方法 * @author 王晓安 * @创建时间 2017年7月19日10:32:39 * @param url 请求打印的路径 */
function print(url){ 
    $.ajax({
        url: url,
        data: {
            ...
        },
        type: "POST",
        dataType: "json",
        success: function(result){ 
            var printWin=window.open("打印窗口", "_blank");
            printWin.document.write(result);
            printWin.document.close();
            printWin.print();
            printWin.close();
        }
    });
}

测试结果:

Excel文件:
《JavaScript打印Excel、Word》

生成的html页面:
《JavaScript打印Excel、Word》

js打印设置:
《JavaScript打印Excel、Word》

打印的pdf文件:
《JavaScript打印Excel、Word》

注意:

当图表比较宽,所选纸张会出现只打印部分页面情况。如下所示:
《JavaScript打印Excel、Word》

解决办法:
调整页边距:效果较小
更换打印纸张:效果明显
调整缩放:效果明显

以下图片是进行缩放的演示:
《JavaScript打印Excel、Word》

打印word与打印Excel类似:

后台word2003转HTML类:
类文件下载地址:http://download.csdn.net/detail/wangxiaoan1234/9909154

package com.srie.util.excel;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.List;

/**
 * 利用POI将Excel2003转换为HTML(支持图片但不支持Excel2007)
 */
public class PoiWord03ToHtml { 

    /**
     * 程序入口方法
     * @param wordPath 待读取的word路径
     * @param htmlPath 转换生成的HTML输出路径
     */
    public static void convertExcelToHtml(String wordPath, String htmlPath){
        File htmlFile = new File(htmlPath);
        File htmlFolder = htmlFile.getParentFile();
        InputStream is = null;
        OutputStream out = null;
        StringWriter writer = null;
        String content;
        try {
            is = new FileInputStream(wordPath);
            HWPFDocument wordDocument = new HWPFDocument(is);
            WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
                    DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
            //不支持λ表达式请用注释里面的代码
            wordToHtmlConverter.setPicturesManager((bytes, pt, string, f, f1) -> string); /* wordToHtmlConverter.setPicturesManager(new PicturesManager() { public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) { return suggestedName; } });*/ wordToHtmlConverter.processDocument(wordDocument); List pics = wordDocument.getPicturesTable().getAllPictures(); if (pics != null) { for (Object pic1 : pics) { Picture pic = (Picture) pic1; try { pic.writeImageContent(new FileOutputStream(htmlFolder + pic.suggestFullFileName())); } catch (FileNotFoundException e) { e.printStackTrace(); } } } Document htmlDocument = wordToHtmlConverter.getDocument(); writer = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "YES"); serializer.setOutputProperty(OutputKeys.METHOD, "HTML"); serializer.transform(new DOMSource(htmlDocument), new StreamResult(writer)); out = new FileOutputStream(htmlFile); content = writer.toString(); out.write(content.getBytes("UTF-8")); out.flush(); out.close(); writer.close(); } catch (IOException | ParserConfigurationException | TransformerException e) { e.printStackTrace(); } finally { try{ if(is != null){ is.close(); } if(out != null){ out.close(); } if(writer != null){ writer.close(); } }catch(IOException e){ e.printStackTrace(); } } } } class TestWord { public static void main(String[] args) throws Exception { PoiWord03ToHtml.convertExcelToHtml("D:/test.doc", "D:/test.html"); } }

测试结果:

word2003文档:
《JavaScript打印Excel、Word》

HTML显示:
《JavaScript打印Excel、Word》

有哪位大神知道怎么使用poi将office2007转换成html吗?请赐教啊!!!

Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

同样的方法,打印.xlsx或者.docx文件时就会抛如上异常。

    原文作者:wxainn
    原文地址: https://blog.csdn.net/wangxiaoan1234/article/details/76032901
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞