2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > java excel poi 包_用java poi包读取Excel单元格

java excel poi 包_用java poi包读取Excel单元格

时间:2019-09-08 02:37:48

相关推荐

java excel poi 包_用java poi包读取Excel单元格

content = new hashmap();

string str = "";

try {

fs = new poifsfilesystem(is);

wb = new hssfworkbook(fs);

} catch (ioexception e) {

e.printstacktrace();

}

sheet = wb.getsheetat(0);

// 得到总行数

int rownum = sheet.getlastrownum();

row = sheet.getrow(0);

int colnum = row.getphysicalnumberofcells();

// 正文内容应该从第二行开始,第一行为表头的标题

for (int i = 1; i <= rownum; i++) {

row = sheet.getrow(i);

int j = 0;

while (j < colnum) {

// 每个单元格的数据内容用"-"分割开,以后需要时用string类的replace()方法还原数据

// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean

// str += getstringcellvalue(row.getcell((short) j)).trim() +

// "-";

str += getcellformatvalue(row.getcell((short) j)).trim() + " ";

j++;

}

content.put(i, str);

str = "";

}

return content;

}

private 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;

}

private string getdatecellvalue(hssfcell cell) {

string result = "";

try {

int celltype = cell.getcelltype();

if (celltype == hssfcell.cell_type_numeric) {

date date = cell.getdatecellvalue();

result = (date.getyear() + 1900) + "-" + (date.getmonth() + 1)

+ "-" + date.getdate();

} else if (celltype == hssfcell.cell_type_string) {

string date = getstringcellvalue(cell);

result = date.replaceall("[年月]", "-").replace("日", "").trim();

} else if (celltype == hssfcell.cell_type_blank) {

result = "";

}

} catch (exception e) {

system.out.println("日期格式不正确!");

e.printstacktrace();

}

return result;

}

private string getcellformatvalue(hssfcell cell) {

string cellvalue = "";

if (cell != null) {

// 判断当前cell的type

switch (cell.getcelltype()) {

// 如果当前cell的type为numeric

case hssfcell.cell_type_numeric:

case hssfcell.cell_type_formula: {

// 判断当前的cell是否为date

if (hssfdateutil.iscelldateformatted(cell)) {

// 如果是date类型则,转化为data格式

//方法1:这样子的data格式是带时分秒的:-10-12 0:00:00

//cellvalue = cell.getdatecellvalue().tolocalestring();

//方法2:这样子的data格式是不带带时分秒的:-10-12

date date = cell.getdatecellvalue();

simpledateformat sdf = new simpledateformat("yyyy-mm-dd");

cellvalue = sdf.format(date);

}

// 如果是纯数字

else {

// 取得当前cell的数值

cellvalue = string.valueof(cell.getnumericcellvalue());

}

break;

}

// 如果当前cell的type为strin

case hssfcell.cell_type_string:

// 取得当前的cell字符串

cellvalue = cell.getrichstringcellvalue().getstring();

break;

// 默认的cell值

default:

cellvalue = " ";

}

} else {

cellvalue = "";

}

return cellvalue;

}

public static void main(string[] args) {

try {

// 对读取excel表格标题测试

inputstream is = new fileinputstream("d:\\test2.xls");

excelreader excelreader = new excelreader();

string[] title = excelreader.readexceltitle(is);

system.out.println("获得excel表格的标题:");

for (string s : title) {

system.out.print(s + " ");

}

// 对读取excel表格内容测试

inputstream is2 = new fileinputstream("d:\\test2.xls");

mapmap = excelreader.readexcelcontent(is2);

system.out.println("获得excel表格的内容:");

for (int i = 1; i <= map.size(); i++) {

system.out.println(map.get(i));

}

} catch (filenotfoundexception e) {

system.out.println("未找到指定路径的文件!");

e.printstacktrace();

}

}

}

3.总结

因为excel单元格中的内容往往都有一定的格式,比如日期型,数字型,字符串型,因此在读取的时候要进行格式判断,不然会出现错误。常见的就是不能正常读取日期。在代码实例中有一个方法:

getcellformatvalue(hssfcell cell)

往这个方法中传入excel单元格就能识别单元格格式,并转化为正确的格式。

ps:-2-23

代码实例中有一段代码:

int colnum = row.getphysicalnumberofcells();

其中的hssfrow.getphysicalnumberofcells();这个方法是用于获取一行中存在的单元格数,poi的官方api中有给出getphysicalnumberofcells方法的解释

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。