代码如下 复制代码
/*
* 使用poi读取excel文件
*/
import java.io.file;
import java.io.fileinputstream;
import java.util.arraylist;
import org.apache.poi.hssf.usermodel.hssfcell;
import org.apache.poi.hssf.usermodel.hssfrow;
import org.apache.poi.hssf.usermodel.hssfsheet;
import org.apache.poi.hssf.usermodel.hssfworkbook;
/**
*
* @author hanbin
*/
public class readexcel {
/**
* @param args the command line arguments
*/
public static void main(string[] args)throws exception {
read(d:demo.xls);
}
public static arraylist read(string filename){
arraylist list = new arraylist();
string sql = ;
try{
file f = new file(filename);
fileinputstream fis = new fileinputstream(f);
hssfworkbook wbs = new hssfworkbook(fis);
hssfsheet childsheet = wbs.getsheetat(0);
system.out.println(行数: + childsheet.getlastrownum());
for(int i = 4;i hssfrow row = childsheet.getrow(i);
system.out.println(列数: + row.getphysicalnumberofcells());
if(null != row){
for(int k=1;k hssfcell cell;
cell = row.getcell((short)k);
// system.out.print(getstringcellvalue(cell) + t);
list.add(getstringcellvalue(cell) + t);
}
}
}
}catch(exception e){
e.printstacktrace();
}
return list;
}
/**
* 获取单元格数据内容为字符串类型的数据
*
* @param cell excel单元格
* @return string 单元格数据内容
*/
private static string getstringcellvalue(hssfcell cell) {
string strcell = ;
switch (cell.getcelltype()) {
case hssfcell.cell_type_string:
strcell = cell.getstringcellvalue();
break;
case hssfcell.cell_type_numeric:
strcell = string.valueof(cell.getnumericcellvalue());
break;
case hssfcell.cell_type_boolean:
strcell = string.valueof(cell.getbooleancellvalue());
break;
case hssfcell.cell_type_blank:
strcell = ;
break;
default:
strcell = ;
break;
}
if (strcell.equals() || strcell == null) {
return ;
}
if (cell == null) {
return ;
}
return strcell;
}
}
http://www.bkjia.com/phpjc/444639.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/444639.htmltecharticle代码如下 复制代码 /* * 使用poi读取excel文件 */ import java.io.file; import java.io.fileinputstream; import java.util.arraylist; import org.apache.poi.hssf.usermodel.h...
