java 操作excel

来源:互联网 发布:上海数据交易中心待遇 编辑:程序博客网 时间:2024/05/01 23:40


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.log4j.Logger;

/**
 * 操作Excel的工具类
 *
 * @author xin_luo
 * @version 2.0 2015/6/7
 */
public class Excel {
    private static Logger LOGGER = Logger.getLogger(Excel.class);
    private static Label label;
    private static WritableWorkbook book;
    private Workbook wb = null;
    private File file;
    private WritableSheet sheet = null;

    public class 猫{
        public void 吃饭(String food){
            System.out.println("猫吃了:" + food);
        }
    }
    
    /**
     * 构造方法,判断该文件是否存在。
     *
     * @param filePath
     *            文件路径
     */
    public Excel(String filePath) {
        file = new File(filePath);
        try {
            if (file.exists()) {
                LOGGER.info(filePath + "文件存在!");
            } else {
                LOGGER.info(filePath + "文件不存在!");
                // file.createNewFile();
            }
        } catch (Exception e) {
            LOGGER.error(e);
        }
    }

    /**
     * 读取Excel中某一列的数据。
     *
     * @param column
     *            列数
     * @return 该列的所有数据,list.get(0)为该列的第一行的数据。
     */
    public List<String> readExcleByColumn(int column) {
        List<String> list = new ArrayList<String>();
        getExcel();

        for (int j = 0; j < sheet.getRows(); j++) {
            Cell cell = sheet.getCell(column, j);
            list.add(cell.getContents());
        }
        writeAndClose();
        return list;
    }

    /**
     * 读取该Excel中的全部数据
     *
     * @return 根据列号可以查询该列所有数据的map
     */
    public Map<Integer, List<String>> readExcle() {
        Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
        getExcel();
        for (int i = 0; i < sheet.getColumns(); i++) {
            List<String> list = new ArrayList<String>();
            for (int j = 0; j < sheet.getRows(); j++) {
                Cell cell = sheet.getCell(i, j);
                list.add(cell.getContents());
            }
            map.put(i, list);
        }
        writeAndClose();
        return map;
    }

    /**
     * 保存并关闭Excel
     */
    private void writeAndClose() {
        try {
            book.write();
            book.close();
        } catch (Exception e) {
            LOGGER.error(e);
        }
    }

    /**
     * 获取到Excel文件和工作表
     */
    private void getExcel() {
        // Excel获得文件
        try {
            wb = Workbook.getWorkbook(file);
        } catch (Exception e) {
            LOGGER.error(e);
        }
        // 打开一个文件的副本,并且指定数据写回到原文件
        try {
            book = Workbook.createWorkbook(file, wb);
        } catch (Exception e) {
            LOGGER.error(e);
        }
        // 添加一个工作表
        sheet = book.getSheet(0);
    }

    /**
     * 更新Excel中的某一行
     *
     * @param column
     *            行号
     * @param list
     *            需要插入的数据
     */
    public void updataByColumn(int column, List<String> list) {
        getExcel();
        // 循环生成表
        // 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
        // 以及单元格内容为test,将定义好的单元格添加到工作表中
        for (int i = 0; i < list.size(); i++) {
            label = new jxl.write.Label(column, i, list.get(i));
            try {
                sheet.addCell(label);
            } catch (Exception e) {
                LOGGER.error(e);
            }
        }
        writeAndClose();
    }

    /**
     * 更新Excel中的某一行
     *
     * @param column
     *            行号
     * @param line 列号
     * @param list
     *            需要插入的数据
     */
    public void updataByColumnAndLine(int column, int line, String data) {
        getExcel();
        label = new jxl.write.Label(column, line, data);
        try {
            sheet.addCell(label);
        } catch (Exception e) {
            LOGGER.error(e);
        }
        writeAndClose();
    }
}

0 0