实现excel在线打印

   这个功能和上一篇实现pdf内容中前面都差不多,让我们直接上代码

  body内按钮:

<div id='exclepdf'  class="buttoncss"  onclick="printout()">
	<img src="...."><span>在线打印</span>
</div>

 css样式设计:

.buttoncss{
	height:35px;
	width:100px;
	text-align:center;
	margin:10px 0px 0px 15px;
	border:1px solid;
	float:right;
	margin-bottom:15px;
}
#excelpdf:hover {
	cursor:pointer;
}
.buttoncss img{
	width:25px;
	padding-top:5px;
	display:inline-block;
}
.buttoncss span{
	display:inline-block;
	padding-left:5px;
	vertical-align:super;
}

引入工具人js文件:

<script src="jquery-3.4.1.js"><script>
<script src="jspdf.debug.js"><script>
<script src="html2canvas.min.js"><script>

重点来咯,关于这一在线打印目前我找到四种方法可以实现这一功能

开始第一个:

//整个页面直接打印
function printout(){
	var newWindow;
	//打开一个新的窗口
	newWindow = window.open();
	//是新窗口获得焦点
	newWindow.focus();
	//保存写入内容
	var newContent = "<html> <head> <meta charset='utf-8'/><title>打印</title> </head><body>"
	newContent += document.getElementById("showPC").outerHTML;
	newContent += "</body></html>"
	//将html代码写入新窗口中
	newWindow.documment.write(newContent);
	newWindow.print();
	//close layout stream
	newWindow.document.close();
	//关闭打开的临时窗口
	newWindow.close();
	return false;
}

第二个吖:

function printout(){
	if(!!window.activexobject || "activexobject" in window){
		remove_ie_header_and_footer();
	}
	window.print();	
};
funtion remove_ie_header_and_footer();{
	var hkey_root,hkey_path,hkey_key;
	hkey_path = "hkey_current_user\\software//microsoft\\internet explorer \\pagesetup\\";
	try{
		var regwsh = new activexobject("wscript.shell");
		regwsh.regwrite(hkey_path + "header","");
		regwsh.regwrite(hkey_path + "footer","");
	}catch(e){}
}

第三个属于直接让页面打印简单方便,如不颜色,可直接用这个方法去实现excel打印:

funtion printout(){
	window.print();
}

第四个,同样也是最后一个,看到这个可能就没啦,还不在上面挑一个再走?

function printout(printout){
	var headhtml = "<html><head><title></title></head><body>";
	var foothtml = "<body/>";
	//获取div中的html内容
	var newhtml = document.all.item(ShowPc).innerHTML;
	//获取div中的html内容
	var oldhtml = $("#" + ShowPc).html();
	var oldhtml = document.body.innerHTML;
	document.body.innerHTML = headhtml + newhtml + foothtml;
	window.print();
	document.body.innerHTML = oldhtml;
	return false;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 Apache POI 这个 Java 库来实现 Java Excel在线预览和打印功能。Apache POI 提供了一组 API,可以用于读取、写入和修改 Excel 文件。 要实现在线预览,你可以将 Excel 文件加载到内存,然后将其转换为 HTML 格式。Apache POI 提供了 HSSF(用于处理 Excel 97-2003 格式)和 XSSF(用于处理 Excel 2007+ 格式)两个子项目,你可以根据你的需要选择适合的项目。一旦你将 Excel 文件转换为 HTML 格式,你可以将其在网页上显示出来,用户就可以在线预览了。 至于打印功能,你可以使用 Java 的打印 API(javax.print 包)来实现。你可以将 Excel 文件加载到内存,然后使用打印 API 将其发送到打印机进行打印。 下面是一个简单的示例代码,演示了使用 Apache POI 将 Excel 文件转换为 HTML 并进行打印的过程: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; public class ExcelPreviewAndPrint { public static void main(String[] args) { try { // 加载 Excel 文件 FileInputStream fileInputStream = new FileInputStream("path/to/excel/file.xlsx"); Workbook workbook = new XSSFWorkbook(fileInputStream); // 将 Excel 文件转换为 HTML ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); workbook.write(byteArrayOutputStream); String htmlContent = new String(byteArrayOutputStream.toByteArray()); // 在网页上显示 HTML 内容,实现在线预览 // 打印 Excel 文件 PDDocument document = PDDocument.load(new ByteArrayInputStream(htmlContent.getBytes())); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); job.print(); // 关闭资源 document.close(); workbook.close(); fileInputStream.close(); } catch (IOException | PrinterException e) { e.printStackTrace(); } } } ``` 请注意,示例代码的路径 "path/to/excel/file.xlsx" 需要替换为你的实际 Excel 文件路径。另外,你可能需要添加 Apache POI 和 Apache PDFBox 的依赖到你的项目。 希望这个示例对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许豪平安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值