日期:2014-05-19  浏览次数:20854 次

j2ee报表导出
做法是不是后台生成一个excel文件,然后再提供给用户下载?

------解决方案--------------------
你要做什么?
------解决方案--------------------
jasperreports 很强大,而且网上的例子也很多
------解决方案--------------------
/**
* 导出Excel
*/
public boolean userExcel(String userName,Date startDate,Date endDate) throws Exception {
List<ChargeInfo> list = chargeInfoDao.findChargeInfoByUserName(userName,startDate,endDate);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("chargeInfo");
HSSFRow rowTitle = sheet.createRow(0);
HSSFCell cellTitle1=rowTitle.createCell((short)0);
cellTitle1.setCellType(HSSFCell.CELL_TYPE_STRING);
cellTitle1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellTitle1.setCellValue("日期");
HSSFCell cellTitle2=rowTitle.createCell((short)1);
cellTitle2.setCellType(HSSFCell.CELL_TYPE_STRING);
cellTitle2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellTitle2.setCellValue("收费业务类型");
HSSFCell cellTitle3=rowTitle.createCell((short)2);
cellTitle3.setCellType(HSSFCell.CELL_TYPE_STRING);
cellTitle3.setEncoding(HSSFCell.ENCODING_UTF_16);
cellTitle3.setCellValue("单价");
HSSFCell cellTitle4=rowTitle.createCell((short)3);
cellTitle4.setCellType(HSSFCell.CELL_TYPE_STRING);
cellTitle4.setEncoding(HSSFCell.ENCODING_UTF_16);
cellTitle4.setCellValue("数量");
HSSFCell cellTitle5=rowTitle.createCell((short)4);
cellTitle5.setCellType(HSSFCell.CELL_TYPE_STRING);
cellTitle5.setEncoding(HSSFCell.ENCODING_UTF_16);
cellTitle5.setCellValue("部门名称");
HSSFCell cellTitle6=rowTitle.createCell((short)5);
cellTitle6.setCellType(HSSFCell.CELL_TYPE_STRING);
cellTitle6.setEncoding(HSSFCell.ENCODING_UTF_16);
cellTitle6.setCellValue("发票号码");

for (int i = 0; i < list.size(); i++) {
HSSFRow row = sheet.createRow(i+1);

ChargeInfo chargeInfo = list.get(i);

row.createCell((short) 0).setCellValue(chargeInfo.getInvoiceNumber()); //entity 属性字段

HSSFCell cell1 = row.createCell((short) 1);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue(chargeInfo.getChargeOperationInfo().getOperationName()); //收费业务类型

HSSFCell cell2 = row.createCell((short) 2);
cell2.setCellValue(chargeInfo.getChargePrice()); //单价

HSSFCell cell3 = row.createCell((short) 3);
cell3.setCellValue(chargeInfo.getChargeQuantity()); //数量

HSSFCell cell4 = row.createCell((short) 4);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
cell4.setCellValue(chargeInfo.getDeptInfo().getDeptName()); //部门名称

HSSFCellStyle cellStyle=workbook.createCellStyle(); //建立新的cell样式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("yy-m-d h:mm")); //设置cell样式为定制的日期格式
HSSFCell cell5 = row.createCell((short) 5);
cell5.setCellValue(chargeInfo.getChargeDate()); //日期
cell5.setCellStyle(cellStyle);
}
byte[] bytes = workbook.getBytes();
// workbook.ENCODING_COMPRESSED_UNICODE;
workbook.setSheetName(0, "第一页",HSSFWorkbook.ENCODING_UTF_16);
FileOutputStream file = new FileOutputStream("E:/ChargeInfo.xls");
workbook.write(file);
file.flush();
//file.write(bytes, 0, bytes.length);
file.close();
return true;
}

------解决方案--------------------
这只是一个例子,具体的需要你去看咯
------解决方案--------------------