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 生成等比例图片的更多相关文章

  1. POI导出EXCEL经典实现

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  2. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

  3. java中使用poi导出excel表格数据并且可以手动修改导出路径

    在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下

  4. 重构:以Java POI 导出EXCEL为例

    重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...

  5. 重构:以Java POI 导出EXCEL为例2

    前言 上一篇博文已经将一些对象抽象成成员变量以及将一些代码块提炼成函数.这一节将会继续重构原有的代码,将一些函数抽象成类,增加成员变量,将传入的参数合成类等等. 上一篇博文地址:http://www. ...

  6. 关于poi导出excel三种方式HSSFWorkbook,SXSSFWorkbook,csv的总结

    poi导出excel最常用的是第一种方式HSSFWorkbook,不过这种方式数据量大的话会产生内存溢出问题,SXSSFWorkbook是一种大数据量导出格式,csv是另一种excel导出的一种轻快的 ...

  7. 关于poi导出excel方式HSSFWorkbook(xls).XSSFWorkbook(xlsx).SXSSFWorkbook.csv的总结

    1.HSSFWorkbook(xls) import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermo ...

  8. POI导出EXCEL经典实现(转)

    http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1.Apache POI简介 Apache POI是Apache软件基 ...

  9. 使用POI导出excel基础篇

    最近搞了下POI导出Excel,听说很多次,却是第一次搞. 在pom.xml中引入依赖 <dependency> <groupId>org.apache.poi</gro ...

随机推荐

  1. about gnu bash shell

    1 定义字符串不需要引号 var=NONE echo $var ==>NONE 2 支持基本的整数计算 a=1 b=2 echo $((a+b)) ==>3 必须用$(()),双括号的形式 ...

  2. MLGBZ

    April cloudy, boss rainy, told me he want to kick But coming so,Formosa Heart sad , Down,down,down W ...

  3. PAT天梯赛 L2-002. 链表去重 【STL】

    题目链接 https://www.patest.cn/contests/gplt/L2-002 思路 用结构体 存储 一个结点的地址 值 和下一个地址 然后从首地址开始 往下走 并且每个值的绝对值 都 ...

  4. Zookeeper四字命令

    ZooKeeper 支持某些特定的四字命令(The Four Letter Words)与其进行交互.它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息.用户在客户端可以通过 ...

  5. ManualResetEvent使用

    1.定义 MSDN定义: 通知一个或多个正在等待的线程已发生事件.此类不能被继承. 详细说明: ManualResetEvent 允许线程通过发信号互相通信.通常,此通信涉及一个线程在其他线程进行之前 ...

  6. SDUT 2133 数据结构实验之栈三:后缀式求值

    数据结构实验之栈三:后缀式求值 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 对于一个基于二元运算符的后缀表示式(基本操作数都是 ...

  7. nginx.config配置文件模板

    #user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#error_l ...

  8. Mac系统存储-其他存储无故增大

    解决办法:打开Finder:安全倾倒废纸篓就会减少很大一部分存储.

  9. 分享知识-快乐自己:什么是MVC

    1.什么是mvc: Model View Controller,是模型-视图-控制器的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个组件里,在改进和个性 ...

  10. mac快速正确的安装 Ruby, Rails 运行环境

    Mac OS X 任意 Linux 发行版本(Ubuntu,CentOS, Redhat, ArchLinux ...) 强烈新手使用 Ubuntu 省掉不必要的麻烦! 以下代码区域,带有 $ 打头的 ...