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 ...
随机推荐
- EasyDSS RTMP流媒体服务器的HTTP接口query url的C++实现方法
EasyDSS支持HTTP GET接口访问,我们需要获取url的各种参数信息 比如http://ip:port/action?a=1&b=2&c=3 我们需要知道对应的a.b.c的值 ...
- css position 几种定位
绝对定位:position:absolute 绝对定位使元素的位置与文档流无关,因此不占据空间. 绝对定位的元素的位置相对于最近的已定位祖先元素(absoulte.relative),如果元素没有已定 ...
- Chrome性能分析工具lightHouse用法指南
本文主要讲如何使用Chrome开发者工具linghtHouse进行页面性能分析. 1.安装插件 非常简单,点击右上角的“添加至Chrome”即可. 2.使用方式 1)打开要测试的页面,点击浏览器右上角 ...
- python数据分析之Pandas:基本功能介绍
Pandas有两个主要的数据结构:Series和DataFrame. Series是一种类似于一维数组的对象,它由一组数据以及一组与之相关的数据标签构成.来看下它的使用过程 In [1]: from ...
- Java for LeetCode 121 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 网页布局的应用(float或absolute)
一个浮动(左浮动或右浮动) 垂直环绕布局(float.clear) 左右两列布局(float.absolute) 三栏网页宽度自适应布局(float.absolute) 注意:网页设计中应该尽量避免使 ...
- cinder挂载卷的过程原理
LVM名称介绍 PV:物理磁盘 VG:卷组,多个PV可以组成一个VG,一个VG可以划分成多个LV(逻辑卷). PP:物理区块,将一个VG逻辑的划分成连续的小块. LP:逻辑区块,若干个PP组成一个LP ...
- HTML5/CSS3动画下拉菜单
在线演示 本地下载
- GDB调试core文件(2)
使用gdb和core dump迅速定位段错误 关键字:gdb.段错误.core dump 一.什么是core dump core:内存.核心的意思: dump:抛出,扔出: core dump:前提: ...
- Java里的阻塞队列
JDK7提供了7个阻塞队列,如下: ArrayBlockingQueue : 一个数组结构组成的有界阻塞队列. LinkedBlockingQueue : 一个由链表结构组成的有界阻塞队列 . Pr ...