`
huanglz19871030
  • 浏览: 241786 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

poi方式将数据库中的数据导出到execl文件

阅读更多

 通过java,要将数据写入到excel文件,有两种比较简单的方式可以选择。
         1.一种是POI方式。
         2.一种是JXl方式。
       于是,我选择了POI方式,按照POI的方式,一天的时间将任务完美的完成了。

       完成任务后,我自己又总结了一下,这是我自己学习总结的例子,接下来我会将我的感受和完整代码分享给大家。

 

      我会分别介绍POI和jxl方式。

一。使用POI操作excel.
   目标:将Oracle数据库查询到的大量数据导入excel文件。

    1.使用此方式,首先需要下载poi-2.5.1.jar文件。这个官网有提供。

    2.将poi-2.5.1.jar导入工程。

    3.接下来就可以放心写你的Java代码就可以了。
      

Java代码 复制代码
  1. <PRE class=java name="code">import org.apache.poi.hssf.usermodel.HSSFCell;   
  2. import org.apache.poi.hssf.usermodel.HSSFCellStyle;   
  3. import org.apache.poi.hssf.usermodel.HSSFDataFormat;   
  4. import org.apache.poi.hssf.usermodel.HSSFRow;   
  5. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  7. import org.apache.poi.hssf.util.HSSFColor;   
  8.   
  9. import common.Utils;   
  10.   
  11. public class ToExcel {   
  12.     /**  
  13.      * poi方式   
  14.      * @author 
  15.      */  
  16.     public void poiToExcel() {   
  17.         FileOutputStream fout = null;   
  18.         try {   
  19.             fout = new FileOutputStream(new File("file/data.xls"));   
  20.         } catch (FileNotFoundException e1) {   
  21.             e1.printStackTrace();   
  22.         }   
  23.         // 创建工作簿   
  24.         HSSFWorkbook workbook = new HSSFWorkbook();   
  25.   
  26.         // 由工作簿创建工作表   
  27.         HSSFSheet sheet = workbook.createSheet();   
  28.   
  29.         // 创建行   
  30.         HSSFRow row = null;   
  31.         row = sheet.createRow(0);   
  32.   
  33.         // 创建单元格,设置每个单元格的值(作为表头)   
  34.         HSSFCell cell = null;   
  35.         cell = row.createCell(0);   
  36.         cell.setCellValue("编号");   
  37.         cell = row.createCell(1);   
  38.         cell.setCellValue("姓名");   
  39.         cell = row.createCell(2);   
  40.         cell.setCellValue("出生年月");   
  41.         // totalList存放的是一条条完整的记录   
  42.         List totalList = Utils.getAllDatas();   
  43.         // list存放的是每一条记录的所有列   
  44.         List l = null;   
  45.         if (totalList != null) {   
  46.   
  47.             for (int i = 0; i < totalList.size(); i++) {   
  48.                 l = (List) totalList.get(i);   
  49.                 row = sheet.createRow(i + 1);   
  50.   
  51.                 for (int j = 0; j < l.size(); j++) {   
  52.                     cell = row.createCell(j);   
  53.                     cell.setCellValue(l.get(j).toString());   
  54.                 }   
  55.   
  56.             }   
  57.         }   
  58.   
  59.         try {   
  60.             workbook.write(fout);   
  61.   
  62.         } catch (IOException e) {   
  63.   
  64.             e.printStackTrace();   
  65.         } finally {   
  66.             try {   
  67.                 fout.close();   
  68.             } catch (IOException e) {   
  69.                 e.printStackTrace();   
  70.             }   
  71.         }   
  72.   
  73.     }   
  74.   
  75.     /**  
  76.      * poi控制单元格样式  
  77.      */  
  78.     public void setCellStyle() {   
  79.         FileOutputStream out = null;   
  80.         try {   
  81.             out = new FileOutputStream(new File("file/data2.xls"));   
  82.             HSSFWorkbook workbook = new HSSFWorkbook();   
  83.             HSSFSheet sheet = workbook.createSheet();   
  84.             HSSFRow row = sheet.createRow(0);   
  85.             HSSFCell cell = row.createCell(0);   
  86.             cell.setCellValue("hello");   
  87.             // 创建HSSFCellStyle对象   
  88.   
  89.             HSSFCellStyle style = workbook.createCellStyle();   
  90.             // 设置此样式(样式属性)   
  91.             style.setFillBackgroundColor(HSSFColor.BLUE.index2);   
  92.             workbook.write(out);   
  93.         } catch (FileNotFoundException e) {   
  94.             e.printStackTrace();   
  95.         } catch (IOException e) {   
  96.             e.printStackTrace();   
  97.         } finally {   
  98.             if (out != null) {   
  99.                 try {   
  100.                     out.close();   
  101.                 } catch (IOException e) {   
  102.                     e.printStackTrace();   
  103.                 }   
  104.             }   
  105.         }   
  106.   
  107.     }   
  108.   
  109.     /**  
  110.      *   
  111.      * PIO设置日期格式  
  112.      */  
  113.     public void setDateFormat() {   
  114.         FileOutputStream fout = null;   
  115.         try {   
  116.             fout = new FileOutputStream(new File("file/data3.xls"));   
  117.             HSSFWorkbook workbook = new HSSFWorkbook();   
  118.             HSSFSheet sheet = workbook.createSheet();   
  119.             HSSFRow row = sheet.createRow(0);   
  120.             HSSFCell cell = row.createCell(0);   
  121.             cell.setCellValue(new Date());   
  122.   
  123.             // 设置一种数据格式   
  124.             HSSFCellStyle cellstyle = workbook.createCellStyle();   
  125.             cellstyle.setDataFormat(HSSFDataFormat   
  126.                     .getBuiltinFormat("m/d/yy h:mm"));   
  127.   
  128.             // 设置此单元格日期样式   
  129.             cell.setCellStyle(cellstyle);   
  130.             workbook.write(fout);   
  131.         } catch (FileNotFoundException e) {   
  132.             e.printStackTrace();   
  133.         } catch (IOException e) {   
  134.             e.printStackTrace();   
  135.         } finally {   
  136.             try {   
  137.                 fout.close();   
  138.             } catch (IOException e) {   
  139.                 e.printStackTrace();   
  140.             }   
  141.         }   
  142.   
  143.     }   
      其实大家看到,使用poi方式将数据写入excel的步骤就是这样的。(完全按照我们打开一个.xls文件,写入内容的步骤  来操作即可)
      1.首先创建excel工作簿。
      2.在这个工作簿上创建工作表
      3.在这个工作表中创建行。
      4.每一行添加单元格,每一个单元格加入值就可以了。

      综上所述,我的代码部分也写了详细的注释,大家应该很容易看明白的。方法poiToExcel()就可以成功的将从数据库查询到的数据写入excel文件了。

     如果读者还需要设置一些额外的东西,例如excel的样式,那么我们使用HSSFCell类即可。如上的方法setCellStyle()即简单的设置了一下样式。(当然,因为我的主管给我安排的任务并没有要求设置样式,所以,并没有去设置太多东西),还有就是一个关于日期的设置,也是比较繁琐的,通过上述的方法setDateFormat()可以让日期正确的显示在excel中。

 

   下面是我查询数据用到的Utils,也供大家参考。

Java代码 复制代码
  1. package common;   
  2.   
  3. import java.sql.Connection;   
  4. import java.sql.ResultSet;   
  5. import java.sql.SQLException;   
  6. import java.sql.Statement;   
  7. import java.util.ArrayList;   
  8. import java.util.List;   
  9.   
  10. public class Utils {   
  11.     /**  
  12.      * 查询表test中的所有数据,将返回的数据全部放入List集合  
  13.      * @return  
  14.      */  
  15.     public static List getAllDatas(){   
  16.         List totalList=new ArrayList();   
  17.         Connection conn=null;   
  18.         Statement stm=null;   
  19.         ResultSet rs=null;   
  20.         conn=ConnectionFactory.getOracleConnection();   
  21.         String sql="select id ,name,birthday from test";   
  22.         try {   
  23.             stm=conn.createStatement();   
  24.             rs=stm.executeQuery(sql);   
  25.             List list=null;   
  26.             while(rs.next()){   
  27.                 list=new ArrayList();   
  28.                 list.add(rs.getObject(1));   
  29.                 list.add(rs.getObject(2));   
  30.                 list.add(rs.getObject(3));     
  31.                 totalList.add(list);   
  32.             }   
  33.         } catch (SQLException e) {   
  34.             e.printStackTrace();   
  35.         }   
  36.            
  37.         return totalList;   
  38.     }   
  39. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics