使用POI操作excel.

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

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

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

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

Java代码  收藏代码
<pre name="code" class="java">import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
import org.apache.poi.hssf.usermodel.HSSFDataFormat;  
import org.apache.poi.hssf.usermodel.HSSFRow;  
import org.apache.poi.hssf.usermodel.HSSFSheet;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.poi.hssf.util.HSSFColor;  
  
import common.Utils;  
  
public class ToExcel {  
    /** 
     * poi方式  
     * @author xulx 
     */  
    public void poiToExcel() {  
        FileOutputStream fout = null;  
        try {  
            fout = new FileOutputStream(new File("file/data.xls"));  
        } catch (FileNotFoundException e1) {  
            e1.printStackTrace();  
        }  
        // 创建工作簿  
        HSSFWorkbook workbook = new HSSFWorkbook();  
  
        // 由工作簿创建工作表  
        HSSFSheet sheet = workbook.createSheet();  
  
        // 创建行  
        HSSFRow row = null;  
        row = sheet.createRow(0);  
  
        // 创建单元格,设置每个单元格的值(作为表头)  
        HSSFCell cell = null;  
        cell = row.createCell(0);  
        cell.setCellValue("编号");  
        cell = row.createCell(1);  
        cell.setCellValue("姓名");  
        cell = row.createCell(2);  
        cell.setCellValue("出生年月");  
        // totalList存放的是一条条完整的记录  
        List totalList = Utils.getAllDatas();  
        // list存放的是每一条记录的所有列  
        List l = null;  
        if (totalList != null) {  
  
            for (int i = 0; i < totalList.size(); i++) {  
                l = (List) totalList.get(i);  
                row = sheet.createRow(i + 1);  
  
                for (int j = 0; j < l.size(); j++) {  
                    cell = row.createCell(j);  
                    cell.setCellValue(l.get(j).toString());  
                }  
  
            }  
        }  
  
        try {  
            workbook.write(fout);  
  
        } catch (IOException e) {  
  
            e.printStackTrace();  
        } finally {  
            try {  
                fout.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
  
    }  
  
    /** 
     * poi控制单元格样式 
     */  
    public void setCellStyle() {  
        FileOutputStream out = null;  
        try {  
            out = new FileOutputStream(new File("file/data2.xls"));  
            HSSFWorkbook workbook = new HSSFWorkbook();  
            HSSFSheet sheet = workbook.createSheet();  
            HSSFRow row = sheet.createRow(0);  
            HSSFCell cell = row.createCell(0);  
            cell.setCellValue("hello");  
            // 创建HSSFCellStyle对象  
  
            HSSFCellStyle style = workbook.createCellStyle();  
            // 设置此样式(样式属性)  
            style.setFillBackgroundColor(HSSFColor.BLUE.index2);  
            workbook.write(out);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (out != null) {  
                try {  
                    out.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
  
    }  
  
    /** 
     *  
     * PIO设置日期格式 
     */  
    public void setDateFormat() {  
        FileOutputStream fout = null;  
        try {  
            fout = new FileOutputStream(new File("file/data3.xls"));  
            HSSFWorkbook workbook = new HSSFWorkbook();  
            HSSFSheet sheet = workbook.createSheet();  
            HSSFRow row = sheet.createRow(0);  
            HSSFCell cell = row.createCell(0);  
            cell.setCellValue(new Date());  
  
            // 设置一种数据格式  
            HSSFCellStyle cellstyle = workbook.createCellStyle();  
            cellstyle.setDataFormat(HSSFDataFormat  
                    .getBuiltinFormat("m/d/yy h:mm"));  
  
            // 设置此单元格日期样式  
            cell.setCellStyle(cellstyle);  
            workbook.write(fout);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                fout.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
  
    }  
}</pre>  
 总结:我们耐心看我的代码之后,我想你肯定就豁然明白了。  

  

  其实大家看到,使用poi方式将数据写入excel的步骤就是这样的。(完全按照我们打开一个.xls文件,写入内容的步骤  来操作即可)
      1.首先创建excel工作簿。
      2.在这个工作簿上创建工作表
      3.在这个工作表中创建行。
      4.每一行添加单元格,每一个单元格加入值就可以了。

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

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

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

package common;  
  
import java.sql.Connection;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
import java.util.ArrayList;  
import java.util.List;  
  
public class Utils {  
    /** 
     * 查询表test中的所有数据,将返回的数据全部放入List集合 
     * @return 
     */  
    public static List getAllDatas(){  
        List totalList=new ArrayList();  
        Connection conn=null;  
        Statement stm=null;  
        ResultSet rs=null;  
        conn=ConnectionFactory.getOracleConnection();  
        String sql="select id ,name,birthday from test";  
        try {  
            stm=conn.createStatement();  
            rs=stm.executeQuery(sql);  
            List list=null;  
            while(rs.next()){  
                list=new ArrayList();  
                list.add(rs.getObject(1));  
                list.add(rs.getObject(2));  
                list.add(rs.getObject(3));    
                totalList.add(list);  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
          
        return totalList;  
    }  
}  

  来源:https://blog.csdn.net/yjdreams4it/article/details/8861861   作者: 

猜你喜欢

转载自www.cnblogs.com/cslgzl/p/10471393.html