首页 > 技术文章 > 如何用java使用POI读取excel文件,创建excel文件,实现批量导出和导入

chyxOne 2018-10-09 19:36 原文

例子1:

 1         //读取excel
 2         String path = "F:\\\\yujun\\\\javaweb\\\\第二章\\\\stu.xlsx";
 3         //工作表
 4         Workbook book = new XSSFWorkbook(path);
 5         //工作页
 6         Sheet sheet = book.getSheet("学生信息");
 7         //获取工作页的行数
 8         int rows = sheet.getLastRowNum(); //自动排除第一行
 9         System.out.println("总行数:"+rows);
10         
11         for(int i=0;i<=rows;i++) {
12             //每行
13             Row r = sheet.getRow(i);
14             //得到总列数
15             int cells =  r.getLastCellNum();
16 
17             String str = "";
18             
19             for(int k=0;k < cells;k++) {
20                 //取得每列
21                 Cell c = r.getCell(k);
22                 String data = c.toString(); //获取该列的值
23                 str += data+"\t";
24             } 
25             System.out.println(str);
26         }
27         book.close();

例子2:

 1         //创建工作本
 2         Workbook book = new XSSFWorkbook();//操作excel2007及以上
 3                            //HSSFWorkbook //操作excel2003及以下
 4         //工作页
 5         Sheet sheet = book.createSheet("学生信息");
 6         //创建第一行(标题行)
 7         Row title = sheet.createRow(0);
 8         //添加标题行的列
 9         Cell c0 = title.createCell(0);
10         c0.setCellValue("ID");
11         title.createCell(1).setCellValue("姓名");
12         title.createCell(2).setCellValue("性别");
13         title.createCell(3).setCellValue("年龄");
14         
15         //添加数据
16         Row r1 = sheet.createRow(1);
17         r1.createCell(0).setCellValue(1);
18         r1.createCell(1).setCellValue("abc");
19         r1.createCell(2).setCellValue("女");
20         r1.createCell(3).setCellValue(20);
21         
22         //写入文件
23         String path = "F:\\yujun\\javaweb\\第二章\\stu.xlsx";
24         book.write(new FileOutputStream(new File(path)));
25         book.close();
26         System.out.println("excel写入完成");

例子3:

 1 //批量添加
 2     public List<String[]> readExcel(String path) throws Exception {
 3         List<String[]> list = new ArrayList<>();
 4         // 工作表
 5         Workbook book = new XSSFWorkbook(path);
 6         // 工作页
 7         Sheet sheet = book.getSheetAt(0);
 8         // 获取工作页的行数
 9         int rows = sheet.getLastRowNum(); // 自动排除第一行
10 
11         for (int i = 1; i <= rows; i++) {
12             // 每行
13             Row r = sheet.getRow(i);
14             // 得到总列数
15             int cells = r.getLastCellNum();
16 
17             String[] sqlValue = new String[cells];
18             for (int k = 0; k < cells; k++) {
19                 // 取得每列
20                 Cell c = r.getCell(k);
21                 String data = c.toString(); // 获取该列的值
22                 sqlValue[k] = data;
23                 
24             }
25             list.add(sqlValue);
26         }
27         book.close();
28         return list;
29     }
30 
31     
32     public boolean batchInsert(List<String[]> values) {
33         Connection conn = null;
34         PreparedStatement ps = null;
35         ResultSet rs = null;
36         try {
37             conn = DBUtil.getConnection();
38             conn.setAutoCommit(false);
39             
40             String sql = "insert stuInfo values(?,?,?,?)";
41             ps = conn.prepareStatement(sql);
42             for(String[] value : values) {
43                 ps.setInt(1, 0);
44                 ps.setString(2, value[1]);
45                 ps.setString(3, value[2]);
46                 ps.setInt(4, (int)Double.parseDouble(value[3]));
47                 ps.addBatch();
48             }
49             ps.executeBatch();
50             conn.commit();
51             return true;
52         }catch(Exception e) {
53             try {
54                 conn.rollback();
55             } catch (SQLException e1) {
56                 e1.printStackTrace();
57             }
58             System.out.println(e);
59         }finally {
60             DBUtil.close(conn, ps, rs);
61         }
62         return false;
63     }
64     
65     public static void main(String[] args) throws Exception {
66         Work2 w = new Work2();
67         String path = "F:\\\\yujun\\\\javaweb\\\\第二章\\\\stu.xlsx";
68         List<String[]> values = w.readExcel(path);
69         w.batchInsert(values);
70         
71         System.out.println("ok");
72     }

 

推荐阅读