【SSM 】导出excel含图片
ExprotRentUtils
package com.kikyo.stat.utils; import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date; import javax.imageio.ImageIO; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.util.CellRangeAddress; import com.kikyo.bus.domain.Customer;
import com.kikyo.bus.domain.Rent; /**
* 客户数据库导出
*
*
*/
public class ExprotRentUtils { /**
* 导出出租单数据
*/
@SuppressWarnings("deprecation")
public static ByteArrayOutputStream exportRent(Rent rent, Customer customer, String sheetName) {
// 一组装excel文档
// 1,创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 2,创建样式
HSSFCellStyle baseStyle = ExprotHSSFCellStyle.createBaseStyle(workbook);
HSSFCellStyle titleStyle = ExprotHSSFCellStyle.createTitleStyle(workbook);
// 3在工作簿创建sheet
HSSFSheet sheet = workbook.createSheet(sheetName);
// 4,设置sheet
sheet.setDefaultColumnWidth(30);
sheet.setColumnWidth(1, 50 * 256);
// 5,合并
CellRangeAddress region1 = new CellRangeAddress(0, 0, 0, 3);
sheet.addMergedRegion(region1);
// 6,创建第一行
int index = 0;
HSSFRow row1 = sheet.createRow(index);
// 6.1在第一行里面创建一个单元格
HSSFCell row1_cell1 = row1.createCell(0);
// 6.2设置标题样式
row1_cell1.setCellStyle(titleStyle);
// 6.3设置单元格内容
row1_cell1.setCellValue(customer.getCustname() + "的出租单信息"); // 7,第二行
index++;
HSSFRow row2 = sheet.createRow(index);
// 7.1设置行高
row2.setHeightInPoints(150); HSSFCell row2_cell1 = row2.createCell(0);
row2_cell1.setCellStyle(baseStyle);
row2_cell1.setCellValue("出租单号:"); HSSFCell row2_cell2 = row2.createCell(1);
row2_cell2.setCellStyle(baseStyle);
row2_cell2.setCellValue(rent.getRentid()); HSSFCell row2_cell3 = row2.createCell(2);
row2_cell3.setCellStyle(baseStyle);
row2_cell3.setCellValue("二维码:"); HSSFCell row2_cell4 = row2.createCell(3);
row2_cell4.setCellStyle(baseStyle);
row2_cell4.setCellValue(""); //画图片
InputStream logoStream = ExprotCustomerUtils.class.getClassLoader().getResourceAsStream("image/logo.jpg");
BufferedImage image = ZXingCodeEncodeUtils.createZxingCodeUseLogo(rent.getRentid(), 150, 150, logoStream); ByteArrayOutputStream bos=new ByteArrayOutputStream();
try {
ImageIO.write(image, "JPEG", bos);
} catch (IOException e1) {
e1.printStackTrace();
}
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); /**
* 参数4 设置图片的平铺程度 最大值是255 255代表铺满当前单元格 小于255就铺不满
* 参数5 列的开始坐标
* 参数6 行的开始坐标
* 参数7 列的结束坐标
* 参数8 行的结束坐标
*/
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 255,(short) 3, 1, (short) 4, 1);
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
patriarch.createPicture(anchor, workbook.addPicture(bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); // 第三行
index++;
HSSFRow row3 = sheet.createRow(index); HSSFCell row3_cell1 = row3.createCell(0);
row3_cell1.setCellStyle(baseStyle);
row3_cell1.setCellValue("客户姓名:"); HSSFCell row3_cell2 = row3.createCell(1);
row3_cell2.setCellStyle(baseStyle);
row3_cell2.setCellValue(customer.getCustname()); HSSFCell row3_cell3 = row3.createCell(2);
row3_cell3.setCellStyle(baseStyle);
row3_cell3.setCellValue("身份证号:"); HSSFCell row3_cell4 = row3.createCell(3);
row3_cell4.setCellStyle(baseStyle);
row3_cell4.setCellValue(customer.getIdentity()); // 第四行
index++;
HSSFRow row4 = sheet.createRow(index); HSSFCell row4_cell1 = row4.createCell(0);
row4_cell1.setCellStyle(baseStyle);
row4_cell1.setCellValue("起租时间:"); HSSFCell row4_cell2 = row4.createCell(1);
row4_cell2.setCellStyle(baseStyle);
row4_cell2.setCellValue(rent.getBegindate().toLocaleString()); HSSFCell row4_cell3 = row4.createCell(2);
row4_cell3.setCellStyle(baseStyle);
row4_cell3.setCellValue("还车时间:"); HSSFCell row4_cell4 = row4.createCell(3);
row4_cell4.setCellStyle(baseStyle);
row4_cell4.setCellValue(rent.getReturndate().toLocaleString()); // 第五行
index++;
HSSFRow row5 = sheet.createRow(index); HSSFCell row5_cell1 = row5.createCell(0);
row5_cell1.setCellStyle(baseStyle);
row5_cell1.setCellValue("车牌号:"); HSSFCell row5_cell2 = row5.createCell(1);
row5_cell2.setCellStyle(baseStyle);
row5_cell2.setCellValue(rent.getCarnumber()); HSSFCell row5_cell3 = row5.createCell(2);
row5_cell3.setCellStyle(baseStyle);
row5_cell3.setCellValue("出租价格:"); HSSFCell row5_cell4 = row5.createCell(3);
row5_cell4.setCellStyle(baseStyle);
row5_cell4.setCellValue(rent.getPrice()); // 第六行 -空行
index++; // 第七行
index++;
HSSFRow row7 = sheet.createRow(index);
HSSFCell row7_cell3 = row7.createCell(2);
row7_cell3.setCellStyle(baseStyle);
row7_cell3.setCellValue("打印时间:"); HSSFCell row7_cell4 = row7.createCell(3);
row7_cell4.setCellStyle(baseStyle);
row7_cell4.setCellValue(new Date().toLocaleString()); // 第八行
index++;
HSSFRow row8 = sheet.createRow(index);
HSSFCell row8_cell3 = row8.createCell(2);
row8_cell3.setCellStyle(baseStyle);
row8_cell3.setCellValue("操作员:"); HSSFCell row8_cell4 = row8.createCell(3);
row8_cell4.setCellStyle(baseStyle);
row8_cell4.setCellValue(rent.getOpername()); // 第九行
index++;
HSSFRow row9 = sheet.createRow(index);
HSSFCell row9_cell3 = row9.createCell(2);
row9_cell3.setCellStyle(baseStyle);
row9_cell3.setCellValue("客户签名:"); // 到此excel组装完成 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 把workbook里面的数据写到outputStream
try {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
return outputStream;
} }
222
<!--POI excel导出-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
【SSM 】导出excel含图片的更多相关文章
- PHP导入导出excel表格图片(转)
		
写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xml的也用过,csv的就更不用谈了.呵呵.(COM方式不讲了,这种可读的太多了,我也写过利用wp ...
 - PHP导入导出excel表格图片的代码和方法大全
		
基本上导出的文件分为两种: 1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件 ...
 - NPOI 导出excel带图片,可控大小
		
using NPOI.HSSF.UserModel;using NPOI.HSSF.Util;using NPOI.DDF;using NPOI.SS.UserModel;using System.I ...
 - asp.net 导出excel带图片
		
protected void btgua_Click(object sender, EventArgs e) { DataTable dt = ds.Tables[0]; if (dt != null ...
 - java用freemarker实现导出word----包含图片
		
分为以下三个步骤: 1.先制作word模板 2.将该文档另存为 xml 文件 3.打开xml 文件 将对应的字段替换,比如 4.将xml文件保存成ftl格式的文档 5.相应的代码: package o ...
 - Birt导出Excel图片
		
有一段时间没有使用Birt了,最近突然之间发现新版的Birt可以支持导出Excel附带图片.我目前下载的是Birt 4.3版本的,导出图片的也只能在Excel 2007下面能够实现,2003的xls格 ...
 - NPOI操作EXCEL(四)——反射机制批量导出excel文件
		
前面我们已经实现了反射机制进行excel表格数据的解析,既然有上传就得有下载,我们再来写一个通用的导出方法,利用反射机制实现对系统所有数据列表的筛选结果导出excel功能. 我们来构想一下这样一个画面 ...
 - OpenXml Excel数据导入导出(含图片的导入导出)
		
声明:里面的很多东西是基于前人的基础上实现的,具体是哪些人 俺忘了,我做了一些整合和加工 这个项目居于openxml做Excel的导入导出,可以用OpenXml读取Excel中的图片 和OpenXml ...
 - Vue+EasyPOI导出Excel(带图片)
		
一.前言 平时的工作中,Excel 导入导出功能是非常常见的功能,无论是前端 Vue (js-xlsx) 还是 后端 Java (POI),如果让大家手动编码实现的话,恐怕就很麻烦了,尤其是一些定制化 ...
 
随机推荐
- Linux gd库安装步骤说明
			
gd 库是 PHP 处理图形的扩展库,它提供了一系列用来处理图片的 API(应用程序编程接口),使用 gd 库可以处理图片或者生成图片.在网站上,gd 库通常用来生成缩略图,或者对图片加水印,或者生成 ...
 - maven一直加载2.0.0.M7 的 config server 失败
			
之前学习的时候使用F版的SpringBoot管理项目依赖一直好好的,今天不知idea为何抽疯,一直加载失败,各种重启,清除,没用 只能像之前学习注册consul 时将F版的SpringBoot 改为G ...
 - jmeter+influxdb+granfana+collectd监控cpu+mem+TPS
			
1.安装grafana #####gafana过期安装包安装报错 Error unpacking rpm package grafana-5.1.4-1.x86_64error: unpacking ...
 - centos长ping输出日志的脚本
			
为监控某服务器的网络情况,制作一个sh脚本,记录ping的长过程,并输出日志以备观察. 1.脚本如下 cat /home/summer/ping100.sh #!/bin/sh ping 172.16 ...
 - Winform遍历窗口的所有控件(几种方式实现)
			
本文链接:https://blog.csdn.net/u014453443/article/details/85088733 扣扣技术交流群:460189483 C#遍历窗体所有控件或某类型所有控件 ...
 - 【渗透测试】ZipperDown 漏洞分析
			
0x01 漏洞危害 1. 在使用 HTTP 下载 zip 包并使用有问题的第三方库 unzip 时,攻击者通过劫持 HTTP 流量,可以导致 APP 下载一个恶意的 zip 包,在解压 zip 包时可 ...
 - 主席树 hdu 4417
			
求一个区间内小于等于limit的数: 主席树模板题. 求出每一个节点的sum: #include<cstdio> #include<algorithm> #include< ...
 - vue简单计数器
			
//App.vue <template> <div id="app"> <!-- <img src="./assets/logo.pn ...
 - LinuxC下argv,argc[]的意义
			
MarkdownPad Document *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
 - 三大查找算法(Java实现)
			
三大查找算法 1.二分查找(Binary Search) public class BinarySearch { public static void main(String[] args) { in ...