poi匯入匯出excel檔案,相容.xls和.xlsx兩種格式
阿新 • • 發佈:2019-02-05
這個是測試類:
UtilTools.java是一個工具類,主要用到了下面兩個方法:package com.fishroad.util; import java.io.File; import java.io.FileInputStream; import java.math.BigDecimal; import java.text.SimpleDateFormat; import javax.swing.JOptionPane; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; 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; public class ImportExcel { private final static String XLS = "xls"; private final static String XLSX = "xlsx"; public static void main(String[] args) { String path = UtilTools.getDirPath(); String extensionName = UtilTools.getFileExtName(path); // System.out.println(extensionName); Workbook workbook = null; try { if (extensionName.toLowerCase().equals(XLS)) { workbook = new HSSFWorkbook(new FileInputStream(new File(path))); } else if (extensionName.toLowerCase().equals(XLSX)) { workbook = new XSSFWorkbook(new FileInputStream(new File(path))); } Sheet sheet = workbook.getSheetAt(0); int minRowIx = sheet.getFirstRowNum(); int maxRowIx = sheet.getLastRowNum(); for (int rowIx = minRowIx; rowIx <= maxRowIx; rowIx++) { Row row = sheet.getRow(rowIx); //Cell cell = row.getCell(0); int minCellIx = row.getFirstCellNum(); int maxCellIx = row.getLastCellNum(); for(int cellIx = minCellIx;cellIx<=maxCellIx;cellIx++){ Cell cell = row.getCell(cellIx); if (cell != null) { String a = getValue(cell); if(a==null || "".equals(a)){ System.out.println("---******----"); } System.out.println(a); }else{ System.out.println(cellIx+"---******----"); } } /*if (cell != null) { String a = cell.getStringCellValue(); System.out.println(a); }*/ } } catch (Exception e) { JOptionPane.showMessageDialog(null, "匯入檔案出錯!"); } } private static String getValue(Cell hssfCell) { if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { // 返回布林型別的值 return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { if(HSSFDateUtil.isCellDateFormatted(hssfCell)){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue())).toString(); } // 返回數值型別的值(記得將數字型別的值轉化為BigDecimal,不然的話,取出來會帶有小數點,切記!!!!!) return String.valueOf(new BigDecimal(hssfCell.getNumericCellValue()) ); } else { // 返回字串型別的值 return String.valueOf(hssfCell.getStringCellValue()); } } }
/** * swing頁面形式的選擇檔案儲存位置 * * @return */ public static String getDirPath() { JFileChooser parseDir = new JFileChooser(); parseDir.setAcceptAllFileFilterUsed(true); parseDir.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int result = parseDir.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { System.out.println(parseDir.getSelectedFile().getAbsolutePath()); return parseDir.getSelectedFile().getAbsolutePath(); } else { return ""; } }
/**
* 獲取檔案的字尾名
*
* @param filePath
* @return
*/
public static String getFileExtName(String filePath) {
File f = new File(filePath);
String fileName = f.getName();
String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
System.out.println(prefix);
return prefix;
}