大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
JavaScript调用本地打印机,打印Excel、Word文件
之前写过一篇文章,使用java调用打印机打印Excel文件:
java调用打印机:http://blog.csdn.net/wangxiaoan1234/article/details/76032280
但是java运行与服务器上,外部访问无法调用本地打印机。
假设项目需求为:点击按钮打印某个报表(Excel)。当项目发布到服务器上后,当使用java调用打印机,无论哪台电脑访问页面,点击打印按钮,调用的都是java所运行的服务器上的打印机。
理想:
实际:
解决思路:
JavaScript运行在本地,使用JavaScript调用本地打印机。
想到可行方法:
- 直接调用打印机打印本地文件(IE浏览器用此方法)。
- java后台将Excel转换成html页面,调用window.print()函数打印整个页面(通用);
- 安装打印插件(麻烦)
使用Spring poi将Excel、Word转换为html再进行打印的好处有:
- 所有浏览器通用
- 可以进行打印预览
- 可视化的打印参数设置
- 我不会别的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后就可以各种调用打印了。
我采用的方法是:
- 前台ajax访问后台转换方法
- 后台返回第58行的content字符串,这个字符串就是整个html页面代码。
- 将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文件:
生成的html页面:
js打印设置:
打印的pdf文件:
注意:
当图表比较宽,所选纸张会出现只打印部分页面情况。如下所示:
解决办法:
调整页边距:效果较小
更换打印纸张:效果明显
调整缩放:效果明显
以下图片是进行缩放的演示:
打印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文档:
HTML显示:
有哪位大神知道怎么使用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文件时就会抛如上异常。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/210038.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...