您的位置:首页 > 数据库

POI导出数据库数据到excel

2016-04-26 11:31 357 查看
废话就不说了,直接上代码。

public class TestPoi{

/**
*
*
*/
private InputStream istream;
private String fileName;
public String exportSatistics() {
try {
list = orderService.getExportCount(map);
String userAgent = this.getCurrentOperator().getUserAgent();//自己封装的获取使用的浏览器
// 乱码
if (fileName == null)
fileName = "";
String name = "统计";//导出文件名称
//处理在不同浏览器导出文件乱码
if (null != userAgent) {
if (-1 != userAgent.indexOf("firefox")) {
fileName = "=?UTF-8?B?"	+ (new String(Base64.encodeBase64(name.getBytes("UTF-8"))))+ "?=";

} else if (-1 != userAgent.indexOf("chrome")) {
fileName = new String(name.getBytes(), "ISO8859-1");
} else {
fileName = URLEncoder.encode(name, "UTF-8");
fileName = fileName.replace("+", "%20");
}
}
fileName = fileName + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".xls";
this.createCountExcel(list, fileName);
} catch (Exception e) {
addActionError("导出文件异常!");
flag = false;
e.printStackTrace();
return ERROR;
}
return "filedownload";
}

/**
*
*
*/
private InputStream createCountExcel(List<OrderStatisticVO> list, String fileName) {
// 第①步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第②步,在webbook中添加一个sheet,对应Excel文件中的sheet
String name = "商户订单统计报表";
HSSFSheet sheet = wb.createSheet(name);
// 指定合并区域
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, (short) 0,0, (short) 25);
sheet.addMergedRegion(cellRangeAddress);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 指定单元格垂直居中对齐
// 设置单元格字体
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 300);
cellStyle.setFont(font);

// 第③步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("商户订单统计报表");
cell.setCellStyle(cellStyle);
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("日期");
cell = row.createCell(1);
cell.setCellValue("订单号");
cell = row.createCell(2);
cell.setCellValue("商品名称");
cell = row.createCell(3);
cell.setCellValue("商品价格");
cell = row.createCell(4);
cell.setCellValue("商品数量");
cell = row.createCell(5);
cell.setCellValue("商品总价");
cell = row.createCell(6);
cell.setCellValue("收货人");
cell = row.createCell(7);
cell.setCellValue("收货地址");
cell = row.createCell(8);
cell.setCellValue("订单状态");
// 第⑤步,写入实体数据 实际应用中这些数据从数据库得到
if (list != null && !list.isEmpty()) {
Iterator it=list.iterator();
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 2);
OrderStatisticVO vo = (OrderStatisticVO) it.next();
// 得到所有区域
sheet.getNumMergedRegions();
// 第④步,创建单元格,并设置值
if (vo != null) {
cell = row.createCell(0);
cell.setCellValue(vo.getOrderDate());
cell = row.createCell(1);
cell.setCellValue(vo.getOrderId());
cell = row.createCell(2);
cell.setCellValue(vo.getProductName());
cell= row.createCell(3);
cell.setCellValue(vo.getProductPrice().doubleValue());
cell = row.createCell(4);
cell.setCellValue(vo.getProductCount());
cell = row.createCell(5);
cell.setCellValue(vo.getTotalPrice().doubleValue());
cell = row.createCell(6);
cell.setCellValue(vo.getUserName());
cell = row.createCell(7);
cell.setCellValue(vo.getShipAddress());
cell = row.createCell(8);
cell.setCellValue(vo.getOrderState());
}
}
}

// 第六步,将文件存到指定位置
try {
fileName = new String(fileName.getBytes("utf-8"), "utf-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
wb.write(bos);
istream = new ByteArrayInputStream(bos.toByteArray());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return istream;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: