Java操作Excel

package com.hpzx.data;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelOperate {

    private List<List<Object>> content = null;

    public List<List<Object>> getContent() {
        return content;
    }


    public void setContent(List<List<Object>> content) {
        this.content = content;
    }


    public void readExcel(String path) throws FileNotFoundException,
            IOException {
        int index = path.lastIndexOf(".");

        if (index > -1) {
            String pfix = path.substring(index + 1).toLowerCase();
            if (pfix.equals("xls")) {
                // ==========读取excel文件内容=====.xls格式
                content = readExcel2003(path);
            } else if (pfix.equals("xlsx")) {
                // ==========读取excel文件内容=====.xlsx格式
                content = readExcel2007(path);
            }
        }
    }

    
    /**
     * 读取Excel2007
     * 
     * @param path
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    private List<List<Object>> readExcel2007(String path)
            throws FileNotFoundException, IOException {
        List<List<Object>> list = new LinkedList<List<Object>>();
        // 构造 XSSFWorkbook 对象,strPath 传入文件路径
        XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(path));
        // 读取第一张表格内容
        XSSFSheet sheet = xwb.getSheetAt(0);
        Object value = null;
        XSSFRow row = null;
        XSSFCell cell = null;
//        System.out.println("读取office 2007 excel内容如下:");
        DecimalFormat df = new DecimalFormat("0");// 格式化 number String
        // 字符
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
        DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
        for (int i = sheet.getFirstRowNum(); i <= sheet
                .getPhysicalNumberOfRows(); i++) {
            row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            List<Object> linked = new LinkedList<Object>();
            for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
                cell = row.getCell(j);
                if (cell == null) {
                    continue;
                }
                switch (cell.getCellType()) {
                case XSSFCell.CELL_TYPE_STRING:
                    value = cell.getStringCellValue();
//                    System.out.print(value + "	");
                    break;
                case XSSFCell.CELL_TYPE_NUMERIC:
                    if ("@".equals(cell.getCellStyle().getDataFormatString())) {
                        value = df.format(cell.getNumericCellValue());

                    } else if ("General".equals(cell.getCellStyle()
                            .getDataFormatString())) {
                        value = nf.format(cell.getNumericCellValue());
                    } else {
                        value = sdf.format(HSSFDateUtil.getJavaDate(cell
                                .getNumericCellValue()));
                    }
//                    System.out.print(value + "	");
                    break;
                case XSSFCell.CELL_TYPE_BOOLEAN:
                    value = cell.getBooleanCellValue();
//                    System.out.print(value + "	");
                    break;
                case XSSFCell.CELL_TYPE_BLANK:
                    value = "#";
//                    System.out.print(value + "	");
                    break;
                default:
                    value = cell.toString();
//                    System.out.print(value + "	");
                }
                if (value == null || "".equals(value)) {
                    continue;
                }
                linked.add(value);
            }
//            System.out.println();
            list.add(linked);
        }
        return list;
    }

    /**
     * 读取Excel2003
     * 
     * @param path
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    private List<List<Object>> readExcel2003(String path)
            throws FileNotFoundException, IOException {
        List<List<Object>> list = new LinkedList<List<Object>>();
        HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(path));
        HSSFSheet sheet = hwb.getSheetAt(0);
        Object value = null;
        HSSFRow row = null;
        HSSFCell cell = null;
//        System.out.println("读取office 2003 excel内容如下:");
        for (int i = sheet.getFirstRowNum(); i <= sheet
                .getPhysicalNumberOfRows(); i++) {
            row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            List<Object> linked = new LinkedList<Object>();
            for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
                cell = row.getCell(j);
                if (cell == null) {
                    continue;
                }
                DecimalFormat df = new DecimalFormat("0");// 格式化 number String
                // 字符
                SimpleDateFormat sdf = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
                DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
                switch (cell.getCellType()) {
                case XSSFCell.CELL_TYPE_STRING:
                    value = cell.getStringCellValue();
//                    System.out.print(value + "	");
                    break;
                case XSSFCell.CELL_TYPE_NUMERIC:
                    if ("@".equals(cell.getCellStyle().getDataFormatString())) {
                        value = df.format(cell.getNumericCellValue());

                    } else if ("General".equals(cell.getCellStyle()
                            .getDataFormatString())) {
                        value = nf.format(cell.getNumericCellValue());
                    } else {
                        value = sdf.format(HSSFDateUtil.getJavaDate(cell
                                .getNumericCellValue()));
                    }
//                    System.out.print(value + "	");
                    break;
                case XSSFCell.CELL_TYPE_BOOLEAN:
                    value = cell.getBooleanCellValue();
//                    System.out.print(value + "	");
                    break;
                case XSSFCell.CELL_TYPE_BLANK:
                    value = "#";
//                    System.out.print(value + "	");
                    break;
                default:
                    value = cell.toString();
//                    System.out.print(value + "	");
                }
                if (value == null || "".equals(value)) {
                    continue;
                }
                linked.add(value);
            }
//            System.out.println("");
            list.add(linked);
        }

        return list;
    }

}
原文地址:https://www.cnblogs.com/yuwenfeng/p/4112946.html