poi 导出excel 生成等比例图片
poi 导出的带等比例图片方法
/**
*
* <p>Description: 将一物一码列表导出到excel</p>
* @param response
* @param list
* @throws IOException
*/
public void exportExcel(DispMeta meta) throws IOException { Map<String, Object> params = new HashMap<String, Object>();
params.put("orderNum", meta.getRequest().getParameter("orderNum"));
meta.setParams(params); IDispApi service; meta.setParams(params);
meta.setMapkey("OneThingAndOneYardService.queryProductCodeListExport");
try {
service = find(ModulePbDesc.PB_SALES_02, meta.getMapkey());
service.disp(meta);
} catch (DispException e) {
e.printStackTrace();
}
ArrayList<Map<String,Object>> list = (ArrayList<Map<String, Object>>) meta.getResult(); HSSFWorkbook workbook = new HSSFWorkbook ();// 创建一个Excel文件
HSSFSheet sheet = workbook.createSheet("一物一码");// 创建一个Excel的Sheet
sheet.setColumnWidth(0, 40 * 256);
sheet.setColumnWidth(1, 40 * 256);
sheet.setColumnWidth(2, 40 * 256);
sheet.setColumnWidth(3, 40 * 256);
sheet.setColumnWidth(4, 40 * 256);
sheet.setColumnWidth(5, 40 * 256);
sheet.setColumnWidth(6, 40 * 256);
sheet.setColumnWidth(7, 40 * 256); HSSFRow row4 = sheet.createRow(0);
HSSFCell cell4_1 = row4.createCell(0);
HSSFCell cell4_2 = row4.createCell(1);
HSSFCell cell4_3 = row4.createCell(2);
HSSFCell cell4_4 = row4.createCell(3);
HSSFCell cell4_5 = row4.createCell(4);
HSSFCell cell4_6 = row4.createCell(5);
HSSFCell cell4_7 = row4.createCell(6);
HSSFCell cell4_8 = row4.createCell(7);
cell4_1.setCellValue( "一物一码" );
cell4_2.setCellValue( "订单编号" );
cell4_3.setCellValue( "商品编码" );
cell4_4.setCellValue( "规格材质" );
cell4_5.setCellValue( "主人账号" );
cell4_6.setCellValue( "柜号" );
cell4_7.setCellValue( "配置号" );
cell4_8.setCellValue( "生效日期" ); HSSFCellStyle style4= workbook.createCellStyle();
style4.setAlignment(HSSFCellStyle. ALIGN_CENTER);//水平居中
style4.setVerticalAlignment(HSSFCellStyle. VERTICAL_CENTER);//垂直居中
style4.setWrapText( true);//自动换行
style4.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
style4.setFillPattern(CellStyle.SOLID_FOREGROUND);
HSSFFont font4 = workbook.createFont();
// font.setFontName("华文行楷");//设置字体名称
font4.setFontHeightInPoints(( short)15);//设置字号
// font4.setBoldweight(boldweight)( true);
style4.setFont(font4); // cell4.setCellStyle(style4);
cell4_1.setCellStyle(style4);
cell4_2.setCellStyle(style4);
cell4_3.setCellStyle(style4);
cell4_4.setCellStyle(style4);
cell4_5.setCellStyle(style4);
cell4_6.setCellStyle(style4);
cell4_7.setCellStyle(style4);
cell4_8.setCellStyle(style4);
//row 4 - items List header -------------------------end //row 5 - items List -------------------------start
for (int i = 0; i < list.size(); i++) {
HSSFRow row5 = sheet.createRow( i+1);
HSSFCell cell5_1 = row5.createCell(0);
HSSFCell cell5_2 = row5.createCell(1);
HSSFCell cell5_3 = row5.createCell(2);
HSSFCell cell5_4 = row5.createCell(3);
HSSFCell cell5_5 = row5.createCell(4);
HSSFCell cell5_6 = row5.createCell(5);
HSSFCell cell5_7 = row5.createCell(6);
HSSFCell cell5_8 = row5.createCell(7);
Map<String, Object> map = list.get(i);
// img --start
ByteArrayOutputStream outStream_item = new ByteArrayOutputStream();
// 将图片写入流中
BufferedImage bufferImg_item = ImageIO.read(new FileInputStream("D:"+map.get("img_code").toString()));
ImageIO. write(bufferImg_item, "PNG", outStream_item); row5.setHeightInPoints(80);
int width = bufferImg_item.getWidth();//原始宽度
int height = bufferImg_item.getHeight();//原始高度
// 一个12号字体的宽度为13,前面已设置了列的宽度为30*256,故这里的等比例高度计算如下
height = (int) Math.round((height * (30 * 13) * 1.0 / width));
// excel单元格高度是以点单位,1点=2像素; POI中Height的单位是1/20个点,故设置单元的等比例高度如下
row5.setHeight((short) (height / 2 * 20));
// 利用HSSFPatriarch将图片写入EXCEL
HSSFPatriarch patri_item = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor_item = new HSSFClientAnchor(0, 0, 0, 0, (short ) 0, i+1, (short) 1, i+2);
patri_item.createPicture(anchor_item, workbook.addPicture(outStream_item.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
// img -- end //订单编号
cell5_2.setCellValue(map.get("order_num").toString());
//商品编码
cell5_3.setCellValue(map.get("product_num").toString());
//规格材质
cell5_4.setCellValue(map.get("size_name").toString());
//主人账号
cell5_5.setCellValue(map.get("host_account")==null?"":map.get("host_account").toString());
//柜号
cell5_6.setCellValue(map.get("cabinet_number").toString());
//配置号
cell5_7.setCellValue(map.get("config_num").toString());
//生效日期
cell5_8.setCellValue(map.get("effective_time").toString()); HSSFCellStyle style5=workbook.createCellStyle();
style5.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style5.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
style5.setWrapText( true);//自动换行 cell5_2.setCellStyle(style5);
cell5_3.setCellStyle(style5);
cell5_4.setCellStyle(style5);
cell5_5.setCellStyle(style5);
cell5_6.setCellStyle(style5);
cell5_7.setCellStyle(style5);
cell5_8.setCellStyle(style5); }
//row 5 - items List -------------------------end // 第六步,将文件存到指定位置
try
{
String excelName="一物一码.xls"; meta.getResponse().addHeader( "Content-Disposition", "attachment;filename="
+ new String(excelName.getBytes("gb2312" ), "ISO-8859-1" ));
// response.addHeader("Content-Length", "" + 1024);
meta.getResponse().setContentType("application/vnd.ms-excel;charset=utf-8" );
BufferedOutputStream out = new BufferedOutputStream(meta.getResponse().getOutputStream());
workbook.write(out);
out.close();
}
catch (Exception e )
{
e.printStackTrace();
}
}
poi 导出excel 生成等比例图片的更多相关文章
- POI导出EXCEL经典实现
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...
- Java POI 导出EXCEL经典实现 Java导出Excel
转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...
- java中使用poi导出excel表格数据并且可以手动修改导出路径
在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下
- 重构:以Java POI 导出EXCEL为例
重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...
- 重构:以Java POI 导出EXCEL为例2
前言 上一篇博文已经将一些对象抽象成成员变量以及将一些代码块提炼成函数.这一节将会继续重构原有的代码,将一些函数抽象成类,增加成员变量,将传入的参数合成类等等. 上一篇博文地址:http://www. ...
- 关于poi导出excel三种方式HSSFWorkbook,SXSSFWorkbook,csv的总结
poi导出excel最常用的是第一种方式HSSFWorkbook,不过这种方式数据量大的话会产生内存溢出问题,SXSSFWorkbook是一种大数据量导出格式,csv是另一种excel导出的一种轻快的 ...
- 关于poi导出excel方式HSSFWorkbook(xls).XSSFWorkbook(xlsx).SXSSFWorkbook.csv的总结
1.HSSFWorkbook(xls) import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermo ...
- POI导出EXCEL经典实现(转)
http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1.Apache POI简介 Apache POI是Apache软件基 ...
- 使用POI导出excel基础篇
最近搞了下POI导出Excel,听说很多次,却是第一次搞. 在pom.xml中引入依赖 <dependency> <groupId>org.apache.poi</gro ...
随机推荐
- zookeeper_action
连接串 从节点列表本地缓存主节点对未分配的任务,随机分配给从节点(不合理??)从节点保存一个本地待执行任务列表单独的线程对节点已分配任务进行循环 进程p为了获锁——>创建节点znode_/loc ...
- ORACLE DATABASE 10g EXPRESS EDITION LICENSE AGREEMENT
启动Tomcat之后出现全是英文错误: ORACLE DATABASE 10g EXPRESS EDITION LICENSE AGREEMENT To use this license, yo ...
- Nginx + Tomcat 应用证书启用 SSL
第一部分 简述 - 附:相关概念 1 Nginx 是什么? - 2 Tomcat 是什么? - 3 SSL 是什么? Secure Sockets Layer,现在应该叫"TLS" ...
- php 获取优酷视频的真实地址(2014.6月新算法)
上个礼拜发现优酷改版了,各种过滤优酷广告的插件都失效了,于是我百度了一下(谷歌也不能用了)发现优酷改算法了,在ckplayer论坛发现有人在6月25号发了个php 的优酷代理文件,下载下来发现,能用但 ...
- Java for LeetCode 136 Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- curl下载安装
curl下载地址 https://curl.haxx.se/download.html 选择windows generic 下的 下载安装 安装完后解压配置系统环境变量 CURL_HOME ...
- android MVP模式思考
在软件开发设计中,有多种软件设计模式,如web开发中经典的MVC, 将后台分为三层:Model层,View层和Controller层,其中,Model主要是数据处理,如数据库,文件,或网络数据等:Vi ...
- POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串
题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS Memory Limit: 10000K Total ...
- 如何让你的手机U盘集PE工具、系统安装、无线破解等众多功能于一身
不久前,手里的U盘坏了,于是乎,又在网上淘了一个Type-C U盘,刚好手机电脑都可以用. 那么现在U有了,我们要做什么呢? 第一:让U盘插在手机上时,可以供手机读写,实现手机存储扩容,随插随用,简单 ...