上一篇的博客中我们介绍了如何Java是如何解析html并从HTML中获取到有关的信息,今天我们来看看Java如何来读取Excel中的数据,首先我们是要知道的应该需要哪些jar.下面的截图就介绍了需要哪些jar
当然我也将上面截图涉及到的jar上传到我们资源,大家可以去下载,声明:在这里我并不是为了让大家下载这个资源才去写这个博客的,毕竟对我来说积分有点严重不足了,下载其他的也需要积分啊,so.....
地址:https://download.csdn.net/download/datouniao1/10312118
我们将这几个jar导入到工程里面,下面我们就要开始操作了,如何来读取Excel,首先在读取Excel的时候,我们应该是提供一个地址。就是我们excel的地址:
package com.wdg.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.wdg.api.IPubUtil; public class PubUtil implements IPubUtil{ @Override public void ShowExcelData(String filePath) { Workbook wb = null; Sheet sheet = null; Row row = null; List<Map<String, String>> list = null; String cellData = null; String columns[] = { "name", "age", "score" }; wb = readExcel(filePath); if (wb != null) { // 用来存放表中数据 list = new ArrayList<Map<String, String>>(); // 获取第一个sheet sheet = wb.getSheetAt(0); // 获取最大行数 int rownum = sheet.getPhysicalNumberOfRows(); // 获取第一行 row = sheet.getRow(0); // 获取最大列数 int colnum = row.getPhysicalNumberOfCells(); for (int i = 1; i < rownum; i++) { Map<String, String> map = new LinkedHashMap<String, String>(); row = sheet.getRow(i); if (row != null) { for (int j = 0; j < colnum; j++) { cellData = (String) getCellFormatValue(row.getCell(j)); map.put(columns[j], cellData); } } else { break; } list.add(map); } } // 遍历解析出来的list for (Map<String, String> map : list) { for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getKey() + ":" + entry.getValue() + ","); } System.out.println(); } } @Override public Workbook readExcel(String filePath) { Workbook wb = null; if (filePath == null) { return null; } String extString = filePath.substring(filePath.lastIndexOf(".")); InputStream is = null; try { is = new FileInputStream(filePath); if (".xls".equals(extString)) { return wb = new HSSFWorkbook(is); } else if (".xlsx".equals(extString)) { return wb = new XSSFWorkbook(is); } else { return wb = null; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return wb; } @Override public Object getCellFormatValue(Cell cell) { Object cellValue = null; if (cell != null) { // 判断cell类型 switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: { cellValue = String.valueOf(cell.getNumericCellValue()); break; } case Cell.CELL_TYPE_FORMULA: { // 判断cell是否为日期格式 if (DateUtil.isCellDateFormatted(cell)) { // 转换为日期格式YYYY-mm-dd cellValue = cell.getDateCellValue(); } else { // 数字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING: { cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = ""; } } else { cellValue = ""; } return cellValue; } }
上面就是我们读取excel的代码了,大家按照这个操作,将上面啊jar导入到工程里面然后再用下面的代码就差不多了
希望对你有所帮助