JavaScript打印Excel、Word JavaScript调⽤本地打印机,打印Excel、Word⽂件
理想:
实际:
解决思路:
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>
<!-- mvnrepository/artifact/org.apachemons/commons-collections4 --> <dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<!-- mvnrepository/artifact/org.lucee/curvesapi -->
<dependency>
<groupId>org.lucee</groupId>
<artifactId>curvesapi</artifactId>
<version>1.04.0</version>
</dependency>
<!-- mvnrepository/artifact/lbeans/xmlbeans -->
<dependency>
<groupId>lbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
<!-- mvnrepository/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.16</version>
</dependency>
package com.l;
import org.apache.verter.ExcelToHtmlConverter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
l.parsers.DocumentBuilderFactory;
l.parsers.ParserConfigurationException;
l.transform.*;
l.transform.dom.DOMSource;
l.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 = ParentFile();
InputStream is = null;
OutputStream out = null;
StringWriter writer = null;
String content = null;
try{
html document是什么ists()){
if(!ists()){
htmlFolder.mkdirs();
}
is = new FileInputStream(excelFile);
HSSFWorkbook workBook = new HSSFWorkbook(is);
ExcelToHtmlConverter converter = new wInstance().newDocumentBuilder().newDocument());
//设置不输出⾏号(1 )及列标(A )等
converter.setOutputColumnHeaders(false);
converter.setOutputHiddenColumns(false);
converter.setOutputColumnHeaders(false);
converter.setOutputLeadingSpacesAsNonBreaking(false);
converter.setOutputRowNumbers(false);
converter.processWorkbook(workBook);
writer = new StringWriter();
Transformer serializer = wInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "YES");
serializer.setOutputProperty(OutputKeys.METHOD, "HTML");
new Document()),
new StreamResult(writer) );
out = new FileOutputStream(htmlFile);
content = String();
//替换掉Sheet1
content = placeAll("<h2>Sheet[\\d]</h2>", "")
.replaceAll("<h2>第[⼀⼆三四五六七⼋九⼗壹贰叁肆伍陆柒捌玖拾]页</h2>", "");
out.Bytes("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 {
}
}
⽣成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();
}
});
}
测试结果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。