码迷,mamicode.com
首页 > 其他好文 > 详细

XSSFWorkbook 读取xlsx

时间:2015-04-09 17:12:14      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

package cn.action.serviceImpl;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class InputExcelService {

public static void inputExcel() throws IOException{
String path = "F:/Student.xlsx";

try {
InputStream is = new FileInputStream(path); //读取指定目录下的文件
XSSFWorkbook hssfWorkbook = new XSSFWorkbook(is); //创建一个工作薄
for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){ //循环取Sheet
XSSFSheet shset = hssfWorkbook.getSheetAt(numSheet);
if(shset == null){
continue;
}
for(int rowNum = 0; rowNum <= shset.getLastRowNum(); rowNum++){ //循环一个工作薄里面的所有行
XSSFRow hssfRow = shset.getRow(rowNum);
if(hssfRow == null){
continue;
}
int hssFCell = hssfRow.getPhysicalNumberOfCells(); //取一个行里面的所有列
for(int cellNum = 0; cellNum < hssFCell; cellNum++){ //循环取列
XSSFCell hsc = hssfRow.getCell(cellNum);
if(hsc == null){
continue;
}
System.out.print(getValue(hsc)+" "); //取值
}
System.out.println("\n");
}

hssfWorkbook.close();
is.close();
}

} catch (Exception e) {
e.printStackTrace();
}
}

@SuppressWarnings("static-access")
private static String getValue(XSSFCell hssfCell){
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {

hssfCell.setCellType(hssfCell.CELL_TYPE_STRING);
// 返回数值类型的值
return hssfCell.getStringCellValue();
}
else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue()); }
}

public static void outputExcel(){

}

}

XSSFWorkbook 读取xlsx

标签:

原文地址:http://www.cnblogs.com/luoge/p/4409834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!