1.构建弹窗,选择文件, 筛选excel文件(后缀名为,xls、xlsx)97-2003版和2007版本。
需要注意的是包不要导错了 import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; import java.io.File;
JFileChooser fileChooser = new JFileChooser(); fileChooser.setMultiSelectionEnabled(false); fileChooser.setFileFilter(new FileFilter() { @Override public boolean accept(File file) { return file.getName().toLowerCase().endsWith(".xlsx") || file.getName().toLowerCase().endsWith(".xls"); } // 设置可以选择的文件类型 @Override public String getDescription() { return "Excel工作薄(*.xlsx)"; } }); //就会出现一个文件选择框 fileChooser.showOpenDialog(null); //获取选中的文件 File file = fileChooser.getSelectedFile();2.引入解析Excel需要的jar包:
<!-- 操作excel --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> <!-- 操作excel -->最好是引入3.10-FINAL版本的,原以为不同版本差别不大。但是我踩坑了, 引入4.0或者4.1版本时, Cell类报错,手动引入import org.apache.poi.ss.usermodel.Cell;还是会把报错,经过对比发现,原因是Cell这个接口里面的内容不同, 3.10版是这样的 4.0,4.1里面没有,所以会报错。
3.解析excel文件数据(需要注意:处理解析数据所用的接口不同)。
FileInputStream fis = null; try { //传入之前获取的file fis = new FileInputStream(file); // 查看文件路径 String str = file.getPath(); // 获取文件名后缀,Excel,97-2003,2007两个版本处理方式不同 String theFormat = str.substring(str.indexOf("."), str.length()); // 2007版本处理 if (theFormat.equals(".xlsx")) { XSSFWorkbook wb = new XSSFWorkbook(fis); // 根据index获取excel文件内部的sheet,也是0开始 XSSFSheet sheet = wb.getSheetAt(0); int lastRowNum = sheet.getLastRowNum();// 统计行数(从0开始算) // 行数和列数都是从0开始 // System.out.println(lastRowNum); // i可以控制筛选掉表头 for (int i = 1; i <= lastRowNum; i++) { // 获取当前行 XSSFRow row = sheet.getRow(i); // 获取当前行的第一列元素 String KHDDH = getColumnByRow(row, 0); // 获取当前行的第二列元素 String KH = getColumnByRow(row, 1); // 获取当前行的第三列元素 String YWY = getColumnByRow(row, 2); ... //自行拼成实体对象 } } else if (theFormat.equals(".xls")) { //97-2003版本处理 inStream = new FileInputStream(file); HSSFWorkbook wb = new HSSFWorkbook(inStream); HSSFSheet sheet = wb.getSheetAt(0); int lastRowNum = sheet.getLastRowNum();// 统计行数(从0开始算) for (int i = 1; i <= lastRowNum; i++) { // 获取当前行 HSSFRow row = sheet.getRow(i); // 获取当前行的第一列元素 String KHDDH = getColumnByRow(row, 0); // 获取当前行的第二列元素 String KH = getColumnByRow(row, 1); // 获取当前行的第三列元素 String YWY = getColumnByRow(row, 2); ... //自行拼成实体对象 } } } catch (IOException | BusinessException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } 异常: Exception in thread "main" java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell // //由于getStringCellValue读取整型时会报异常,先设置Cell的类型,然后就可以把纯数字作为String类型读进来了 row.getCell(index).setCellType(Cell.CELL_TYPE_STRING); // 列元素,通过下标索引,从0开始 String Column = row.getCell(index).getStringCellValue();将获取某一行数据封装成方法,便于复用
// 获取当前行的某一列,97-03版的Excel public static String getColumnByRow(HSSFRow row, int index) { // 由于getStringCellValue读取整型时会报异常,先设置Cell的类型,然后就可以把纯数字作为String类型读进来了 row.getCell(index).setCellType(Cell.CELL_TYPE_STRING); String Column = row.getCell(index).getStringCellValue();// 列元素,通过下标索引,从0开始 return Column; } // 获取当前行的某一列,2007版的Excel public static String getColumnByRow(XSSFRow row, int index) { // 由于getStringCellValue读取整型时会报异常,先设置Cell的类型,然后就可以把纯数字作为String类型读进来了 row.getCell(index).setCellType(Cell.CELL_TYPE_STRING); String Column = row.getCell(index).getStringCellValue();// 列元素,通过下标索引,从0开始 return Column; }4.将获取到的数据封装成单个实体。 得到的String,一个个set进入实体类(无需多言) 5.插入数据库 无非就是访问数据库,insert进去。
注意,在演示之前,需要先存在Excel,并且里面有数据。
package com.leo.springboot.excel; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; 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.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImpData { public static void main(String[] args) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setMultiSelectionEnabled(false); fileChooser.setFileFilter(new FileFilter() { @Override public boolean accept(File file) { return file.getName().toLowerCase().endsWith(".xlsx") || file.getName().toLowerCase().endsWith(".xls"); } // 设置可以选择的文件类型 @Override public String getDescription() { return "Excel工作薄(*.xlsx)"; } }); fileChooser.showOpenDialog(null); File file = fileChooser.getSelectedFile(); if (file != null) { //解析Excel文件数据 setDataFromExcel(file); } } public static void setDataFromExcel(File file) { FileInputStream fis = null; try { fis = new FileInputStream(file); // 查看文件路径 String str = file.getPath(); // 获取文件名后缀,Excel,97-2003,2007两个版本处理方式不同 String theFormat = str.substring(str.indexOf("."), str.length()); /* * System.out.println(str); System.out.println(theFormat); */ // 2007版本处理 if (theFormat.equals(".xlsx")) { XSSFWorkbook wb = new XSSFWorkbook(fis); // 根据index获取excel文件内部的sheet,也是0开始 XSSFSheet sheet = wb.getSheetAt(0); int lastRowNum = sheet.getLastRowNum();// 统计行数(从0开始算) // 行数和列数都是从0开始 // System.out.println(lastRowNum); // i可以控制筛选掉表头 System.out.println("客户订单号\t" + "客户\t" + "业务员\t" + "产品编号\t" + "发货数量\t" + "成交金额\t" + "测试行数据"); for (int i = 1; i <= lastRowNum; i++) { // 获取当前行 XSSFRow row = sheet.getRow(i); // 客户订单号 String KHDDH = getColumnByRow(row, 0); // 客户 String KH = getColumnByRow(row, 1); // 业务员 String YWY = getColumnByRow(row, 2); // 产品编号 String CPBH = getColumnByRow(row, 3); // 发货数量 String FHSL = getColumnByRow(row, 4); // 成交金额 String CJJE = getColumnByRow(row, 5); // 测试行数据 String test = getColumnByRow(row, 6); System.out.println(KHDDH + "\t" + KH + "\t" + YWY + "\t" + CPBH + "\t" + FHSL + "\t" + CJJE + "\t" + test); } } else if (theFormat.equals(".xls")) { fis = new FileInputStream(file); HSSFWorkbook wb = new HSSFWorkbook(fis); HSSFSheet sheet = wb.getSheetAt(0); int lastRowNum = sheet.getLastRowNum();// 统计行数(从0开始算) for (int i = 1; i <= lastRowNum; i++) { // 获取当前行 HSSFRow row = sheet.getRow(i); // 客户订单号 String KHDDH = getColumnByRow(row, 0); // 客户 String KH = getColumnByRow(row, 1); // 业务员 String YWY = getColumnByRow(row, 2); // 产品编号 String CPBH = getColumnByRow(row, 3); // 发货数量 String FHSL = getColumnByRow(row, 4); // 成交金额 String CJJE = getColumnByRow(row, 5); // 测试行数据 String test = getColumnByRow(row, 6); System.out.println(KHDDH + "\t" + KH + "\t" + YWY + "\t" + CPBH + "\t" + FHSL + "\t" + CJJE + "\t" + test); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 获取当前行的某一列,97-03版的Excel public static String getColumnByRow(HSSFRow row, int index) { // 由于getStringCellValue读取整型时会报异常,先设置Cell的类型,然后就可以把纯数字作为String类型读进来了 row.getCell(index).setCellType(Cell.CELL_TYPE_STRING); String Column = row.getCell(index).getStringCellValue();// 列元素,通过下标索引,从0开始 return Column; } // 获取当前行的某一列,2007版的Excel public static String getColumnByRow(XSSFRow row, int index) { // 由于getStringCellValue读取整型时会报异常,先设置Cell的类型,然后就可以把纯数字作为String类型读进来了 row.getCell(index).setCellType(Cell.CELL_TYPE_STRING); String Column = row.getCell(index).getStringCellValue();// 列元素,通过下标索引,从0开始 return Column; } }