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含图片的更多相关文章

  1. PHP导入导出excel表格图片(转)

    写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xml的也用过,csv的就更不用谈了.呵呵.(COM方式不讲了,这种可读的太多了,我也写过利用wp ...

  2. PHP导入导出excel表格图片的代码和方法大全

    基本上导出的文件分为两种: 1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件 ...

  3. NPOI 导出excel带图片,可控大小

    using NPOI.HSSF.UserModel;using NPOI.HSSF.Util;using NPOI.DDF;using NPOI.SS.UserModel;using System.I ...

  4. asp.net 导出excel带图片

    protected void btgua_Click(object sender, EventArgs e) { DataTable dt = ds.Tables[0]; if (dt != null ...

  5. java用freemarker实现导出word----包含图片

    分为以下三个步骤: 1.先制作word模板 2.将该文档另存为 xml 文件 3.打开xml 文件 将对应的字段替换,比如 4.将xml文件保存成ftl格式的文档 5.相应的代码: package o ...

  6. Birt导出Excel图片

    有一段时间没有使用Birt了,最近突然之间发现新版的Birt可以支持导出Excel附带图片.我目前下载的是Birt 4.3版本的,导出图片的也只能在Excel 2007下面能够实现,2003的xls格 ...

  7. NPOI操作EXCEL(四)——反射机制批量导出excel文件

    前面我们已经实现了反射机制进行excel表格数据的解析,既然有上传就得有下载,我们再来写一个通用的导出方法,利用反射机制实现对系统所有数据列表的筛选结果导出excel功能. 我们来构想一下这样一个画面 ...

  8. OpenXml Excel数据导入导出(含图片的导入导出)

    声明:里面的很多东西是基于前人的基础上实现的,具体是哪些人 俺忘了,我做了一些整合和加工 这个项目居于openxml做Excel的导入导出,可以用OpenXml读取Excel中的图片 和OpenXml ...

  9. Vue+EasyPOI导出Excel(带图片)

    一.前言 平时的工作中,Excel 导入导出功能是非常常见的功能,无论是前端 Vue (js-xlsx) 还是 后端 Java (POI),如果让大家手动编码实现的话,恐怕就很麻烦了,尤其是一些定制化 ...

随机推荐

  1. Ad Hoc类问题

    __________________________________ Ad Hoc类问题的方法:(1)机理分析法.分析题目描述,推出算法. (2)统计分析法.追寻最终的数学模型. Problem 1: ...

  2. appium---切换webview时报错

    在上一篇中简单介绍了如何查看webview和切换到webview的方法,可能第一次切换webview的时候会报错“Error: session not created exception: Chrom ...

  3. Loading class `com.mysql.jdbc.Driver'. This is deprecated

    注意mysql的版本,pom.xml里面的版本.External Librarlies里面的mysql版本.application.properties版本都要检查 有时候还会报 Invalid bo ...

  4. sql server获取查询时间

    declare @d datetime set @d=getdate() /*你的SQL脚本开始*/ /*你的SQL脚本结束*/ select [语句执行花费时间(毫秒)]=datediff(ms,@ ...

  5. ES5 寄生式继承

    3 寄生式继承 组合继承存在调用两次父类构造的问题 原型继承存在不能实例化对象不能传参的问题 组合继承和原型继承都存在子类原有原型属性被覆盖的问题 因此推荐使用寄生式继承 /* 寄生式继承: 1 解决 ...

  6. 每天进步一点点------Alpha半透明图形叠加算法Matlab+Verilog实现

    Alpha图形叠加算法Matlab+Verilog实现 1.1. Alpha算法的研究 Alpha通道是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度信息,定义透明.不透明和半透明区域, ...

  7. scrapy下载 大文件处理

    # 一个校花网图片下载的案例,也适合大文件处理,多个文件视频,音频处理 工程流程 -- scrapy startproject xx cd xx scrapy genspider hh www.xx. ...

  8. shell脚本自学之路

    阿里云大学教学https://edu.aliyun.com/course/155/ 运行 chmod +x xx.sh ./xx.sh 基本语法:echo  输出  $赋值 特殊变量: $* 变量的使 ...

  9. MindManager安装、激活

    MindManager安装 MindManager激活

  10. C# 篇基础知识9——特性、程序集和反射

    特性(Attribute)是用于为程序元素添加额外信息的一种机制.比如记录文件修改时间或代码作者.提示某方法已经过期.描述如何序列化数据等等.方法.变量.属性.类.接口.结构体以及程序集等都是程序元素 ...