利用POI实现Excel的导入导出

相关API总结

一 从EXCEL读取数据

创建文件流file

创建EXCEL工作薄

XSSFWorkbook workBook = new XSSFWorkbook(file);

获取EXCEL的sheet标签

XSSFSheet sheetAt = workBook.getSheetAt(0);

遍历所有行内所有格子内所有值

 for(int j=0;j<sheetAt.getLastRowNum();j++) {
for(int i=0;i<sheetAt.getRow(j).getLastCellNum();i++) {
String value = sheetAt.getRow(j).getCell(i).getStringCellValue();

System.out.println(value);
}     

}

二 读取数据库创建EXCEL

public class ExcelDatabase{
private static  String sql;
    public static void main(String[] args) throws Exception
   
    
    {
   
    //连接oracle驱动
//        Class.forName("oracle.jdbc.driver.OracleDriver");
//        //创建连接
//        Connection connect = DriverManager.getConnection(
//                "jdbc:oracle:thin:@192.9.200.226:1521:pcva" ,
//                "aums" ,
//                "aums"
//        );
        读取配置文件
    Properties cfg=new Properties();
InputStream inStream=
DBUtils.class.getClassLoader()
.getResourceAsStream("sql.properties");
System.out.println("inStream"+inStream);

cfg.load(inStream);
sql=cfg.getProperty("sql");

    Connection conn=null;

        通过连接池获取连接

    conn=DBUtils.getConnection();
    
        Statement statement = conn.createStatement();
        //读取结果
       
        ResultSet resultSet = statement
                .executeQuery(sql);
        //创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建工作表  工作表的名字叫sheet
        XSSFSheet spreadsheet = workbook.createSheet("sheet");
        XSSFRow row=spreadsheet.createRow(0);
        XSSFCell cell;
        
        //获取列名 resultSet数据下标从1开始

        ResultSetMetaData metaData = resultSet.getMetaData();

        //metaData有关整个数据库的信息:表名、表的索引、数据库产品的名称和版本、数据库支持的操作。 

         //获取并创建列标题放入Cell的第一列

  for (int i = 0; i < metaData.getColumnCount(); i++) {

            int index=i+1;
            String columnName = metaData.getColumnName(index);
            System.out.println(columnName + "\t");
            cell=row.createCell(i);
            cell.getCellStyle().setWrapText(true);
            cell.setCellValue(columnName);
        }

        //从第二列开始,获取数据信息到Cell中,第一行已被列标题占用
        int i=1;
        while(resultSet.next())
        {
            row=spreadsheet.createRow(i);
            for (int j = 0; j< metaData.getColumnCount(); j++) {
                int index=j+1;
                cell=row.createCell(j);
                //表格样式
                XSSFCellStyle cellStyle=workbook.createCellStyle();
                cellStyle.setAlignment(HorizontalAlignment.LEFT);
                cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
                cellStyle.setWrapText(false);
                cell.setCellStyle(cellStyle);
                
                cell.setCellValue(resultSet.getString(index));
            }
            i++;
        }

        //将文件写出
        FileOutputStream out = new FileOutputStream(new File("1.xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("excel_database.xlsx written successfully");
    }
}


猜你喜欢

转载自blog.csdn.net/handis/article/details/80283508