用POI导出数据的导出方式

POI包导出数据

// 读取模板

           InputStream in = this.getClass().getResourceAsStream(

                  "..//..//..//..//..//excelview//moban6.xls");

           Workbook workbook = WorkbookFactory.create(in);

           in.close();

           Sheet sheet = workbook.getSheetAt(0);

           // 取模板的样式

           CellStyle[] cellstyles = new CellStyle[10];

           Row row1 = sheet.getRow(2);

           for (int i = 0; i < 10; i++) {

              System.out.println("取第"+i+"列样式");

              cellstyles[i] = row1.getCell(i).getCellStyle();

           }

           // 得到数据集合

           List<PointCardSell> list = new ArrayList<PointCardSell>();

           // 打印点卡销售部门报表

              String sql = "";

              list = (List<PointCardSell>) cardbiz.getAllPointCardSell(sql);

 

标记处: /****方法一*导出数据的直列输出发***************************************/

 

int i = 0;

           for (PointCardSell pc : list) {

              Row row = sheet.createRow(i + 2);

 

              // 行的第1个单元格

              Cell cell0 = row.createCell(0);

              cell0.setCellStyle(cellstyles[0]); //设置样式

              cell0.setCellValue(pc.getCardno());//填值

              // 行的第2个单元格

              Cell cell1 = row.createCell(1);

              cell1.setCellStyle(cellstyles[1]);

              cell1.setCellValue(pc.getCustomername());

              // 行的第3个单元格

              Cell cell2 = row.createCell(2);

              cell2.setCellStyle(cellstyles[2]);

              cell2.setCellValue(pc.getRealname()+"123");

              // 行的第4个单元格

              Cell cell3 = row.createCell(3);

              cell3.setCellStyle(cellstyles[3]);

              cell3.setCellValue(pc.getActualprices());

              // 行的第5个单元格

              Cell cell4 = row.createCell(4);

              cell4.setCellStyle(cellstyles[4]);

              cell4.setCellValue(pc.getPoint());

 

             

 

              i++;

           }

 

 

           Row rowdate=sheet.createRow(i+2);

           rowdate.createCell(0).setCellStyle(cellstyles[0]);

           rowdate.createCell(1).setCellStyle(cellstyles[1]);

           rowdate.createCell(2).setCellStyle(cellstyles[2]);

          

           Cell cell3=rowdate.createCell(3);

           cell5.setCellStyle(cellstyles[3]);

           cell3.setCellValue("导出日期:");

           Cell cell4=rowdate.createCell(4);

           Cell4.setCellStyle(cellstyles[4]);

           Cell4.setCellValue(currenttime.toLocaleString().substring(0, 10));

          

标记结束处:/**********************************************************************/

           response.reset();

           String fileName = "点卡销售报表";

           fileName += ".xls";

           response.addHeader("Content-Disposition", "attachment; filename=/""

                  + new String(fileName.getBytes("GBK"), "iso8859-1") + "/"");

           response.setCharacterEncoding("GBK");

           response.setContentType("application/x-msdownload");

           OutputStream out = response.getOutputStream();

           workbook.write(out);

           out.flush();

           out.close();

 

 

 

 

 

 

 

/*******方法二*导出数据的左右交替输出法(奇偶法,左奇右偶)**************************************/

int i = 0;

           for (int j=0;j<list.size();j++){

//         for (PointCardSell pc : list) {

              PointCardSell pc = (PointCardSell)list.get(j);

              if(j%2==0){

             

             

              Row row = sheet.createRow(i + 2);

              // 行的第1个单元格

              Cell cell0 = row.createCell(0);

              cell0.setCellStyle(cellstyles[0]); //设置样式

              cell0.setCellValue(pc.getCardno());//填值

              // 行的第2个单元格

              Cell cell1 = row.createCell(1);

              cell1.setCellStyle(cellstyles[1]);

              cell1.setCellValue(pc.getCustomername());

              // 行的第3个单元格

              Cell cell2 = row.createCell(2);

              cell2.setCellStyle(cellstyles[2]);

              cell2.setCellValue(pc.getRealname());

              // 行的第4个单元格

              Cell cell3 = row.createCell(3);

              cell3.setCellStyle(cellstyles[3]);

              cell3.setCellValue(pc.getActualprices());

              // 行的第5个单元格

              Cell cell4 = row.createCell(4);

              cell4.setCellStyle(cellstyles[4]);

              cell4.setCellValue(pc.getPoint());

             

              i++;

             

              }else{

                 

              Row row = sheet.getRow(i+1);   //注意因为在单数时有(i++)自加了1,所以在此处相对于单数时的i+2应该要少1,即i+1

              // 行的第6个单元格

              Cell cell5 = row.createCell(5);

              cell5.setCellStyle(cellstyles[5]); //设置样式

              cell5.setCellValue(pc.getCardno());//填值

              // 行的第7个单元格

              Cell cell6 = row.createCell(6);

              cell6.setCellStyle(cellstyles[6]);

              cell6.setCellValue(pc.getCustomername());

              // 行的第8个单元格

              Cell cell7 = row.createCell(7);

              cell7.setCellStyle(cellstyles[7]);

              cell7.setCellValue(pc.getRealname()+"123");

              // 行的第9个单元格

              Cell cell8 = row.createCell(8);

              cell8.setCellStyle(cellstyles[8]);

              cell8.setCellValue(pc.getActualprices());

              // 行的第10个单元格

              Cell cell9 = row.createCell(9);

              cell9.setCellStyle(cellstyles[9]);

              cell9.setCellValue(pc.getPoint());

             

              }

 

              Row rowdate=sheet.createRow(i+2);

           rowdate.createCell(0).setCellStyle(cellstyles[0]);

           rowdate.createCell(1).setCellStyle(cellstyles[1]);

           rowdate.createCell(2).setCellStyle(cellstyles[2]);

           rowdate.createCell(3).setCellStyle(cellstyles[3]);

           rowdate.createCell(4).setCellStyle(cellstyles[4]);

           rowdate.createCell(5).setCellStyle(cellstyles[4]);

           rowdate.createCell(6).setCellStyle(cellstyles[4]);

           rowdate.createCell(7).setCellStyle(cellstyles[4]);

          

           Cell cell8=rowdate.createCell(8);

           cell8.setCellStyle(cellstyles[8]);

           cell8.setCellValue("导出日期:");

           Cell cell9=rowdate.createCell(9);

           cell9.setCellStyle(cellstyles[9]);

           cell9.setCellValue(currenttime.toLocaleString().substring(0, 10));

             

           }

/****方法二结束*******************************************************/

 

 

/***方法三*导出数据的左右法先半后半法,即先在左边显示前半部分数据记录,后在右边显示后半部分***/

 

// 得到数据集合

                     List<PointCardSell> list = new ArrayList<PointCardSell>();

                    

                     // 打印点卡销售部门报表

                     String sql = "";

                     list = (List<PointCardSell>) cardbiz.getAllPointCardSell(sql);

                    

                     List<PointCardSell> list1 = new ArrayList<PointCardSell>();

                     List<PointCardSell> list2 = new ArrayList<PointCardSell>();

                     int t = 0 ;

                     int k = list.size();

                     if(k%2==1){

                             t = k/2+1;

                            }else{

                             t = k/2;

                            }

                    

                     list1 = list.subList(0, t);

                     list2 = list.subList(t, k);

                    

                    

                    

                     int i = 0;

                     for (int j=0;j<list1.size();j++){

//                   for (PointCardSell pc : list) {

                            System.out.println("循环list1:的"+j);

                            PointCardSell pc = (PointCardSell)list1.get(j);

                           

                            Row row = sheet.createRow(i + 2);

                            // 行的第1个单元格

                            Cell cell0 = row.createCell(0);

                            cell0.setCellStyle(cellstyles[0]); //设置样式

                            cell0.setCellValue(pc.getCardno());//填值

                            // 行的第2个单元格

                            Cell cell1 = row.createCell(1);

                            cell1.setCellStyle(cellstyles[1]);

                            cell1.setCellValue(pc.getCustomername());

                            // 行的第3个单元格

                            Cell cell2 = row.createCell(2);

                            cell2.setCellStyle(cellstyles[2]);

                            cell2.setCellValue(pc.getRealname());

                            // 行的第4个单元格

                            Cell cell3 = row.createCell(3);

                            cell3.setCellStyle(cellstyles[3]);

                            cell3.setCellValue(pc.getActualprices());

                            // 行的第5个单元格

                            Cell cell4 = row.createCell(4);

                            cell4.setCellStyle(cellstyles[4]);

                            cell4.setCellValue(pc.getPoint());

                           

                            i++;

                     }

                     response.reset();

                     int a = 0;

                     for (int y=0;y<list2.size();y++){

//                   for (PointCardSell pc : list) {

                            System.out.println("循环list21:的"+y);

                            PointCardSell pc = (PointCardSell)list2.get(y);

                           

                            Row row = sheet.getRow(a+2);   //注意因为在单数时有(i++)自加了1,所以在此处相对于单数时的i+2应该要少1,即i+1

                            // 行的第6个单元格

                            Cell cell5 = row.createCell(5);

                            cell5.setCellStyle(cellstyles[5]); //设置样式

                            cell5.setCellValue(pc.getCardno());//填值

                            // 行的第7个单元格

                            Cell cell6 = row.createCell(6);

                            cell6.setCellStyle(cellstyles[6]);

                            cell6.setCellValue(pc.getCustomername());

                            // 行的第8个单元格

                            Cell cell7 = row.createCell(7);

                            cell7.setCellStyle(cellstyles[7]);

                            cell7.setCellValue(pc.getRealname());

                            // 行的第9个单元格

                            Cell cell8 = row.createCell(8);

                            cell8.setCellStyle(cellstyles[8]);

                            cell8.setCellValue(pc.getActualprices());

                            // 行的第10个单元格

                            Cell cell9 = row.createCell(9);

                            cell9.setCellStyle(cellstyles[9]);

                            cell9.setCellValue(pc.getPoint());

                           

                            a++;

                     }

                     Row rowdate=sheet.createRow(i+2);

                     rowdate.createCell(0).setCellStyle(cellstyles[0]);

                     rowdate.createCell(1).setCellStyle(cellstyles[1]);

                     rowdate.createCell(2).setCellStyle(cellstyles[2]);

                     rowdate.createCell(3).setCellStyle(cellstyles[3]);

                     rowdate.createCell(4).setCellStyle(cellstyles[4]);

                     rowdate.createCell(5).setCellStyle(cellstyles[4]);

                     rowdate.createCell(6).setCellStyle(cellstyles[4]);

                     rowdate.createCell(7).setCellStyle(cellstyles[4]);

                    

                     Cell cell8=rowdate.createCell(8);

                     cell8.setCellStyle(cellstyles[8]);

                     cell8.setCellValue("导出日期:");

                     Cell cell9=rowdate.createCell(9);

                     cell9.setCellStyle(cellstyles[9]);

                     cell9.setCellValue(currenttime.toLocaleString().substring(0, 10));

/****方法三结束处*****************************************************/

 

方法一:

  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值