jxl实现对EXCEL读取操作代码实例

来源:互联网 发布:python 日期相差月份 编辑:程序博客网 时间:2024/06/05 07:42

import jxl.Cell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;

import org.apache.log4j.Logger;





private static Logger log = Logger.getLogger(ReadGzAndZcmxFromExcel.class);


    /**
     * 读取客户估值

     * @param in
     * @return
     * @throws Exception
     */
    public HashMap readKhgz(InputStream in) throws Exception {

        Workbook workbook = null;


        HashMap result = new HashMap();

        try {

            log.debug("--开始--从Excel文件中读取客户估值!");

            workbook = Workbook.getWorkbook(in);//获得excel文件

            Sheet sheet = workbook.getSheet(0);// 获得Excel表中的第一个工作表


            int rows = sheet.getRows();//获得表的所有行数

            Cell khbhCell = null;
            String khbh = null;
            NumberCell gzCell = null;

            Double gz = null;
            
//遍历获得表格数据
            for (int i = 2; i <= rows; i++) {

                khbhCell = sheet.getCell(0, i - 1);
                khbh = khbhCell.getContents().trim();

                if (khbh == null || "".equals(khbh))
                    break;

                if (khbh != null&& !"".equals(khbh) {
                    gzCell = (NumberCell) sheet.getCell(1, i - 1);
                    gz = new Double(gzCell.getValue());
                } else {
                    gz = new Double(0);
                }

                result.put(khbh, gz);
            }

            log.debug("--结束--从Excel文件中读取客户估值!");


            return result;

        } catch (Exception e) {

            log.error("从Excel文件中读取客户估值出现异常!");
            e.printStackTrace();
            throw e;

        } finally {

            in.close();
            if (workbook != null)
                workbook.close();
        }

    }

日期格式设置

0 0