java excel to dto_\t\tJAVA导出Excel | 学步园

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值