jxl工具类包装类

处理excel表格的工具类:
package net.csdn.util;

import com.google.common.collect.Lists;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.awt.*;
import java.io.*;
import java.util.List;

/**
 * Title:操作excel表格
 * Description:用到jxl包,再次进行封装,采用单例模式
 * Create Time: 13-9-14 下午10:56
 *
 * @author: hqiq
 * @version: 1.0
 */
public class ExcelManager {

    private InputStream is = null;
    private jxl.Workbook rwb = null;

    public void setGlobalSheet(int rsNum) {
        this.rs = rwb.getSheet(rsNum);
    }

    public Sheet getGlobalSheet() {
        return rs;
    }

    private Sheet rs = null;

    public ExcelManager(File sourceFile) {
        //构建Workbook对象, 只读Workbook对象
        //直接从本地文件创建Workbook
        //从输入流创建Workbook
        try {
            is = new FileInputStream(sourceFile);

            rwb = Workbook.getWorkbook(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * get the choiced sheet
     *
     * @param choice 从0开始算第一张表
     * @return sheet
     */
    public Sheet getSheet(int choice) {
        //获取第choice张Sheet表
        Sheet rs = rwb.getSheet(choice);
        return rs;
    }

    public String getCellValue(int row, int column, Sheet rs) {

        Cell cell = rs.getCell(row, column);

        return cell.getContents();
    }

    /**
     * calulate计算全局sheet有多少行
     *
     * @param skip v是否跳过第一行
     * @return
     */
    public int getRows(boolean skip) {
        int rows;
        if (skip) {
            rows = rs.getRows() - 1;
        } else {
            rows = rs.getRows();
        }
        return rows;
    }

    /**
     * v1从自己指定sheet读取指定列的内容
     *
     * @param sheet     v1自己指定的sheet
     * @param colNumber v1读取指定列
     * @param skip      v1是否跳过第一行
     * @return v1返回一列单元格内容列表
     */
    public List
   
   
    
     getColumnsContent(int sheet, int colNumber, boolean skip) {
        List
    
    
     
      colValList = Lists.newArrayList();
        Sheet rs = rwb.getSheet(sheet);
        Cell[] cells = rs.getColumn(colNumber);

        if (skip) {

            for (int i = 1; i < cells.length; i++) {
                colValList.add(cells[i].getContents());
            }

        } else {
            for (int i = 0; i < cells.length; i++) {
                colValList.add(cells[i].getContents());
            }
        }
        return colValList;

    }

    /**
     * v1从全局读取一列单元格内容
     *
     * @param colNumber
     * @param skip
     * @return
     */
    public List
     
     
      
       getColumnsContent(int colNumber, boolean skip) {
        List
      
      
       
        colValList = Lists.newArrayList();
        Cell[] cells = rs.getColumn(colNumber);

        if (skip) {

            for (int i = 1; i < cells.length; i++) {
                colValList.add(cells[i].getContents());
            }

        } else {
            for (int i = 0; i < cells.length; i++) {
                colValList.add(cells[i].getContents());
            }
        }
        return colValList;

    }

    /**
     * write to excel
     *
     * @param filename
     */
    //TODO
    public void writeExcel(String filename) {
        //创建新的excel工作簿
        WritableWorkbook wwb = null;
        try {
            //创建一个可写入的工作簿
            wwb = Workbook.createWorkbook(new File(filename));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (wwb != null) {
            //创建一个可写入的工作表
            //createSheet有两个参数:一个是工作表名,一个是工作表所在工作簿中位置
            WritableSheet ws = wwb.createSheet("sheet1", 0);

            //下面开始添加单元格
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 5; j++) {
                    Label labelC = new Label();
                    labelC.setText("这是第" + (i + 1) + "行,第" + (j + 1) + "列");
                }
            }
        }
    }

    /**
     * 搜索excel文件里头是否有特定文字
     *
     * @param file
     * @param keyWord
     * @return
     */
    public boolean searchKeyWord(File file, String keyWord) {
        boolean res = false;
        Workbook wb = null;
        try {
            wb = Workbook.getWorkbook(file);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        if (wb == null) {
            return res;
        }

        Sheet[] sheets = wb.getSheets();

        boolean breakSheet = false;

        if (sheets != null && sheets.length > 0) {
            //对每个工作比进行循环
            for (int i = 0; i < sheets.length; i++) {
                if (breakSheet) {
                    break;
                }

                int rowNum = sheets[i].getRows();

                boolean breakRow = false;

                for (int j = 0; j < rowNum; j++) {
                    if (breakRow) {
                        break;
                    }

                    Cell[] cells = sheets[i].getRow(j);
                    if (cells != null && cells.length > 0) {
                        boolean breakCell = false;
                        //对每个单元格进行循环
                        for (int k = 0; k < cells.length; k++) {
                            if (breakCell) {
                                break;
                            }
                            String cellValue = cells[k].getContents();
                            if (cellValue == null) {
                                continue;
                            }
                            if (cellValue.contains(keyWord)) {
                                res = true;
                                breakCell = true;
                                breakRow = true;
                                breakSheet = true;
                            }
                        }
                    }
                }
            }
        }
        wb.close();
        return res;
    }

    /**
     * 用完别忘记关哦~释放占用内存空间
     */
    public void close() {
        if (is != null)
            try {
                is.close();
                if (rwb != null) {
                    rwb.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

      
      
     
     
    
    
   
   

以上来自code.csdn.net
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值