1. 程式人生 > >java關於excel讀取表單

java關於excel讀取表單

第一步:在maven中匯入poi相關的jar

<dependency><groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId
> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.9</version> </dependency> <!-- 支援Word文件的操作 --> <dependency> <groupId
>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.9</version> </dependency>
第二步:
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import 
org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.*; /** * Created by fanjunwei on 2017/2/14. */ public class excel{ private static Logger logger = LoggerFactory.getLogger(World1.class); private static Workbook wb; private static Sheet sheet; private static Row row; /** * 讀取Excel表格表頭的內容 * * @param * @return String 表頭內容的陣列 * @author zengwendong */ public static String[] readExcelTitle(Workbook wb) throws Exception{ if(wb==null){ throw new Exception("Workbook物件為空!"); } sheet = wb.getSheetAt(0); row = sheet.getRow(0); // 標題總列數 int colNum = row.getPhysicalNumberOfCells(); System.out.println("colNum:" + colNum); String[] title = new String[colNum]; for (int i = 0; i < colNum; i++) { // title[i] = getStringCellValue(row.getCell((short) i)); title[i] = row.getCell(i).getCellFormula(); } return title; } /** * 讀取Excel資料內容 * * @param * @return Map 包含單元格資料內容的Map物件 * @author zengwendong */ public static List<Map> readExcelContent(Workbook wb) throws Exception{ if(wb==null){ throw new Exception("Workbook物件為空!"); } List<Map> content = new ArrayList<Map>(); sheet = wb.getSheetAt(0); // 得到總行數 int rowNum = sheet.getLastRowNum(); row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); // 正文內容應該從第二行開始,第一行為表頭的標題 for (int i = 1; i <= rowNum; i++) { row = sheet.getRow(i); int j = 0; Map<String,Object> cellValue = new HashMap<String, Object>(); while (j < colNum) { Object obj = getCellFormatValue(row.getCell(j)); cellValue.put(j, obj); j++; } content.add(cellValue); } return content; } /** * * 根據Cell型別設定資料 * * @param cell * @return * @author zengwendong */ private static Object getCellFormatValue(Cell cell) { Object cellvalue = ""; if (cell != null) { // 判斷當前Cell的Type switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC:// 如果當前Cell的Type為NUMERIC case Cell.CELL_TYPE_FORMULA: { // 判斷當前的cell是否為Date if (DateUtil.isCellDateFormatted(cell)) { // 如果是Date型別則,轉化為Data格式 // data格式是帶時分秒的:2013-7-10 0:00:00 // cellvalue = cell.getDateCellValue().toLocaleString(); // data格式是不帶帶時分秒的:2013-7-10 Date date = cell.getDateCellValue(); cellvalue = date; } else {// 如果是純數字 // 取得當前Cell的數值 cellvalue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING:// 如果當前Cell的Type為STRING // 取得當前的Cell字串 cellvalue = cell.getRichStringCellValue().getString(); break; default:// 預設的Cell值 cellvalue = ""; } } else { cellvalue = ""; } return cellvalue; } /** * 根據路徑解析文件 * @param url */ public static List excel(String url){ String ext = url.substring(url.lastIndexOf(".")); try { InputStream is = new FileInputStream(url); if(".xls".equals(ext)){ wb = new HSSFWorkbook(is); }else if(".xlsx".equals(ext)){ wb = new XSSFWorkbook(is); }else{ wb=null; } return World1.readExcelContent(wb); } catch (FileNotFoundException e) { logger.error("FileNotFoundException", e); return null; } catch (IOException e) { logger.error("IOException", e); return null; }catch (Exception e){ logger.error("Exception", e); return null; } } }