您的位置:首页 > 其它

poi导入导出excel文件,兼容.xls和.xlsx两种格式

2015-07-08 15:49 886 查看
这个是测试类:

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;

	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: