public InputStream makeExcel(Long shopcode,Pagination pagination) throws ApplicationException {
HSSFWorkbook book=new HSSFWorkbook();
HSSFSheet sheet=book.createSheet("sheet1");
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell((short) 0);
cell.setCellValue("编号");
cell=row.createCell((short) 1);
cell.setCellValue("名称");
cell=row.createCell((short) 2);
cell.setCellValue("金额");
cell=row.createCell((short)3);
cell.setCellValue("开始日期");
cell=row.createCell((short)4);
cell.setCellValue("结束日期");
cell=row.createCell((short)5);
cell.setCellValue("数量");
//list为要生成Excel的数据的集合
List list=shpSecModelDao.find(shopcode, pagination);
if(list.size()>0){
for(int i=0;i
ShpSecModelDto secModel=(ShpSecModelDto)list.get(i);
row=sheet.createRow(i+1); //i=0,有个list(0),有一行数据
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
cell=row.createCell((short)0);
cell.setCellValue(secModel.getId());
cell=row.createCell((short)1);
cell.setCellValue(secModel.getName());
cell=row.createCell((short)2);
cell.setCellValue(secModel.getAmount());
cell=row.createCell((short)3);
cell.setCellValue(sf.format(secModel.getStarttime()));
cell=row.createCell((short)4);
cell.setCellValue(sf.format(secModel.getEndtime()));
cell=row.createCell((short)5);
cell.setCellValue(secModel.getAllquantity());
}
}
ByteArrayOutputStream arrayIo=new ByteArrayOutputStream();
try {
book.write(arrayIo);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] by=arrayIo.toByteArray();
InputStream is = new ByteArrayInputStream(by);
if(arrayIo !=null){
try {
arrayIo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return is;
}
本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/1203/18435.php