这个是测试类:
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());
}
}
}
UtilTools.java是一个工具类,主要用到了下面两个方法:
/**
* 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;
}