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. selenium chromedriver退出报错

    记录使用python调用chromedriver时遇到的问题 代码: #!/usr/bin/env python #-*- coding:utf-8 -*- # author : fy # versi ...

  2. JavaScript对象之对象标签和对象序列化

    对象标签有三种:proto.class和extensible. 一.proto标签 例如我新建了一个person对象,那么其__proto__则指向Person.prototype,然后Person. ...

  3. map-apply-applymap

    In [1]: import warnings import math import pandas as pd import numpy as np import matplotlib warning ...

  4. Nuxt项目文件目录结构解释

    |-- .nuxt // Nuxt自动生成,临时的用于编辑的文件,build |-- assets // 用于组织未编译的静态资源入LESS.SASS 或 JavaScript |-- compone ...

  5. svn还原与本地版本回退

    今天遇到了一个情况,由于没及时更新,对整个项目进行了Ctrl+shift+O,提交代码时冲突:然后就先还原项目,导致之前没有冲突的代码也回退了.然后就在eclipse中获取本地的版本记录,并回退 具体 ...

  6. 前端之js基础篇

    JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...

  7. (办公)记事本_linux网络命令

    参考谷粒学院的linux视频教程:http://www.gulixueyuan.com/course/300/task/7091/show 阿里云ECS云服务器更换公网IP的方法:https://yq ...

  8. mybatis批量插入和更新

    批量插入 <insert id="add" parameterType="java.util.List"> insert all <forea ...

  9. 201771010135 杨蓉庆/张燕/杨玲《面对对象程序设计(java)》第十四周学习总结

    1.实验目的与要求 (1) 掌握GUI布局管理器用法: (2) 掌握各类Java Swing组件用途及常用API 一.理论知识  Swing和MVC设计模式 (1)设计模式(Design patte ...

  10. 获取浏览器url参数

    //获取浏览器url参数 var methods ={ getQueryString: function (name) { var reg = new RegExp('(^|&)' + nam ...