POI 操作 Excel 實例

介紹

  POI是Apache用Java編寫的免費開源的跨平臺的 Java API,Apache POI提供API給Java程序對Microsoft Office格式檔案讀和寫的功能。

使用

  導入jar包:
在這裏插入圖片描述
在這裏插入圖片描述

public class ExeclUtil {

    //獲取單元格的值
    @SuppressWarnings("all")
    public  static String getCellValue(Cell c){
        String cellValue = "";
        switch (c.getCellType()){
            case Cell.CELL_TYPE_NUMERIC:
                cellValue = c.getNumericCellValue()+"";
                break;
            case  Cell.CELL_TYPE_BLANK:
                cellValue = "";
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                cellValue = String.valueOf(c.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                cellValue = c.getStringCellValue();
                break;
            case Cell.CELL_TYPE_FORMULA:
                cellValue = String.valueOf(c.getCellFormula());
            default:
                cellValue = "";
                break;
        }
        return cellValue;
    }
}
public class ExcelService  implements IExcelService{

    /*
    * 2013版本excel .xls  HSSF開頭
    * 2017版本 .xlsx XSSF開頭
    * workbook 工作簿
    * sheet 工作表
    * row 行
    * cell 單元格
    *
    * */

    @Override
    public void leadInExcel() throws Exception{
        //Workbook
        //HSSFWorkbook
        //XSSFWorkbook
        //創建excel工作簿
        File f = new File("D://123.xlsx");
        Workbook wb = WorkbookFactory.create(new FileInputStream(f));

        //創建工作表 create 或者get
        Sheet s = wb.createSheet("studnet");
        IStudentDao dao = new StudentDao();
        List<Student> list = dao.findAllStudent();
        //創建行和單元
        //創建表頭
        Row biaotou = s.createRow(0);
        Cell a1 =biaotou.createCell(0);
        a1.setCellValue("編號");
        Cell a2 =biaotou.createCell(1);
        a2.setCellValue("姓名");
        Cell a3 =biaotou.createCell(2);
        a3.setCellValue("性別");
        Cell a4 =biaotou.createCell(3);
        a4.setCellValue("出生年月");
        Cell a5 =biaotou.createCell(4);
        a5.setCellValue("班級");
        Cell a6 =biaotou.createCell(5);
        a6.setCellValue("密碼");
        //創建行和列 單元格和行的下標都是從0 開始
        for (int i = 0; i<list.size(); i++) {
            Row st = s.createRow(i+1);
            Cell s1 =st.createCell(0);
            s1.setCellValue(list.get(i).getNo());
            Cell s2 =st.createCell(1);
            s2.setCellValue(list.get(i).getName());
            Cell s3 =st.createCell(2);
            s3.setCellValue(list.get(i).getSex());
            Cell s4 =st.createCell(3);
            s4.setCellValue(list.get(i).getBirthday());
            Cell s5 =st.createCell(4);
            s5.setCellValue(list.get(i).getSclass());
            Cell s6 =st.createCell(5);
            s6.setCellValue(list.get(i).getPassword());
        }
        OutputStream fo  = new FileOutputStream(f);
        //將程序的wb寫入真實的文件
        wb.write(new FileOutputStream(f));
        wb.close();
    }

    @Override
    public void leadFromExcel() throws Exception{
        Connection con = DruidUtil.getConn();
        //獲取已經存在的文件的wb
        Workbook wb = WorkbookFactory.create(new FileInputStream("d://123.xlsx"));
        try{
            con.setAutoCommit(false);
            IStudentDao dao = new StudentDao();

            //獲取我們需要導入的sheet
            Sheet s = wb.getSheet("studnet");

            //獲取需要導入的數據
            int max = s.getPhysicalNumberOfRows();

            for(int i=1;i<max;i++){
                //獲取一行數據
                Row rw = s.getRow(i);
                Student st = new Student();
                st.setNo(ExeclUtil.getCellValue(rw.getCell(0)));
                st.setName(ExeclUtil.getCellValue(rw.getCell(1)));
                st.setSex(ExeclUtil.getCellValue(rw.getCell(2)));
                st.setBirthday(ExeclUtil.getCellValue(rw.getCell(3)));
                st.setSclass(ExeclUtil.getCellValue(rw.getCell(4)));
                st.setPassword(ExeclUtil.getCellValue(rw.getCell(5)));
                dao.insert(st);
            }
            DbUtils.commitAndCloseQuietly(con);
        }catch (Exception e){
            e.printStackTrace();
            DbUtils.rollbackAndCloseQuietly(con);
        }
        wb.close();
    }
}

這裏使用的編碼是GB2312,和windows系統中文編碼統一

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章