poi导出excel

public String poiDownExcel(HttpServletResponse response,HttpServletRequest req) throws IOException{
CommodityList commodityList = new CommodityList();
int company_id = this.currentOperator(req).getCompanyId();
commodityList.setCompany_id(company_id);
commoditysMap=commodityService.findPoiComList(commodityList);
//创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
//建立新的sheet对象(excel的表单)
HSSFSheet sheet=wb.createSheet("商品表");
//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1=sheet.createRow(0);
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cell=row1.createCell(0);
//设置单元格内容
cell.setCellValue("商品信息");
//合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
//在sheet里创建第二行
HSSFRow row2=sheet.createRow(1);
//创建单元格并设置单元格内容
//row2.createCell(0).setCellValue("商品图片");
row2.createCell(0).setCellValue("商品名");
row2.createCell(1).setCellValue("商品条码");
row2.createCell(2).setCellValue("商品分类");
row2.createCell(3).setCellValue("商品规格");
//在sheet里创建第三行
for (int i = 0; i < commoditysMap.size(); i++) {
HSSFRow row3=sheet.createRow(2+i);
//row3.createCell(0).setCellValue("111");
row3.createCell(0).setCellValue(commoditysMap.get(i).get("name").toString());
row3.createCell(1).setCellValue(commoditysMap.get(i).get("barcode").toString());
row3.createCell(2).setCellValue(commoditysMap.get(i).get("sort_name").toString());
row3.createCell(3).setCellValue(commoditysMap.get(i).get("dimension")+"/"+commoditysMap.get(i).get("units"));
}
//输出Excel文件
OutputStream output=response.getOutputStream();
// FileOutputStream output=new FileOutputStream("C:\\Users\\htc\\Desktop\\workbook.xls");
response.reset();
response.setHeader("Content-disposition", "attachment; filename=ComList.xls");
response.setContentType("application/msexcel");
wb.write(output);
output.close();
return null;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值