日期:2014-05-20  浏览次数:20668 次

分享一个更改后的Execll工具类(上次上传的有bug)+散分
本帖最后由 AA5279AA 于 2013-01-30 12:37:41 编辑
首先抱个歉,上次上传的POI读取Execl的方式有问题,最后一行的execl读不到,这是我代码的问题。。
上次的帖子:http://bbs.csdn.net/topics/390358967
这次分享一个更新后的一个工具类:
添加了一些功能,暂时没有发现bug,如果有问题一定告诉我,我去改:
首先,jar包:

下面,代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

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.ss.usermodel.WorkbookFactory;

/*
 * Date:2013年1月30日11:39:36
 * Author:leilei
 */
//该类是一个工具类,实现的功能是对execl文件简单读和存的功能
public class ExeclHelper {
/*该方法实现的功能是读,读的时候全部以String的方式存储,如果小数点后面为0的全部忽略,*/
public static String[][] poiReader(String filepath,File file) throws Exception{

String[][] result;
InputStream is = null;
if(!filepath.equals("")){
//输入输出流
is = new FileInputStream(filepath);
}else if(file.exists()){
is = new FileInputStream(file);
}else{
System.out.println("输入的路径并且文件为空");
}

//创建工作空间
        Workbook wb = WorkbookFactory.create(is);
        //获取工作表
        Sheet sheet = wb.getSheetAt(0);//获取第一个工作表
        //工作行
        Row row ;
        //工作单元格
        Cell cell = null ;
        int rownum;//行
        int columnnum;//列
        rownum=sheet.getLastRowNum()+1;
        columnnum=sheet.getRow(0).getLastCellNum();
        //实例化返回的数组对象
        result= new String[rownum][columnnum];
      
        System.out.println("rownum:"+rownum);
        System.out.println("columnnum:"+columnnum);
        for(int i=0;i<rownum;i++){
         row= sheet.getRow(i);
         //获取第i行的工作行的第6个单元格的值
         for(int j=0;j<columnnum;j++){
         cell=row.getCell(j);
         if(cell==null){
         String str="";
         result[i][j]=str;
         }else{
         String str=cell.toString();      
         if(str.contains(".")){
         String[] s=str.split("\\."); 
         if(Integer.parseInt(s[1])==0){
         result[i][j]=s[0];
         }else{