您的位置:首页 > 编程语言 > Java开发

JAVA 数据导入Excel

2009-12-03 19:49 369 查看
    <!---Action调用---!>

  public String makeExcel()throws ApplicationException{

        this.getDownloadFile();

        return SUCCESS;  

    }

   

    public InputStream getDownloadFile()
throws ApplicationException{

        shpShopDto = (ShpShopDto) this.getSession().get("shpShopDtoSession");

        if(shpShopDto!=null){

        Pagination pagination = new Pagination(pageNo, rowsPerPage);

        InputStream is=ServiceManager.agent.getShpSecModelService().makeExcel(shpShopDto.getCode(), pagination);

        getRequest().setAttribute("pagination", pagination);

        return is;

        }else{

            return null;

        }

    }

<!---Service相关业务---->

 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<list.size();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;

    }

 

 

<!---XML配置--!>

 

   <action name="generateExcel"

           class="com.fzdna.application.agent.actions.shop.manage.ShpSecuritiesAction" method="makeExcel">

             <result name="success" type="stream">

             <param name="contentType">application/vnd.ms-excel</param>

             <param name="contentDisposition">attachment;filename="goodNumbers.xls" </param>

             <param name="inputName">downloadFile
</param>  

             <!--downloadFile必须配置成事务拦截,downloadFile必须和Action的方法名对应,如getdownloadFile()
-->

             </result>

            </action>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息