您的位置:首页 > 编程语言 > Java开发

java 读取excel文件内容

2015-05-15 09:18 555 查看
代码扫描被读取excel单元格内容

/**

* 读取excel文件单元格内容

* @param model

* @param request

* @param response

* @return

*/

@RequestMapping(method = RequestMethod.GET, value = "/excelIndex")

public String excel() {

try {

String fileName = "E:/test/testIntoExcel.xls"; // Excel文件所在路径

File file = new File(fileName); // 创建文件对象

Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象(WorkBook)

// Sheet st = rwb.getSheet("original");//读取页的方法有两种,一种是使用页名读取,另一种是使用页码读取(从0开始)

Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)

for (int i = 0; i < sheet.getRows(); i++) { // 扫描行

for (int j = 0; j < sheet.getColumns(); j++) { // 扫描列

Cell cell = sheet.getCell(j, i);

System.err.println(i);

// System.err.println(j);

System.out.println(""+i+""+j+":"+cell.getContents());

System.out.println(cell.getType());

}

System.out.println();

}

} catch (BiffException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return "excelIndex";

}

读取指定列内每个单元格数据,读取每行的可以根据读取列的进行变通,这里不做累述。

/**

* 获得指定excel指定列的内容

* @param model

* @param request

* @param response

* @return

*/

@RequestMapping(method = RequestMethod.GET, value = "/getExcelCell")

public String getExcelCell() {

String s="测试3";

try {

String fileName = "E:/test/testIntoExcel.xls"; // Excel文件所在路径

File file = new File(fileName); // 创建文件对象

Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象(WorkBook)

// Sheet st = rwb.getSheet("original");//读取页的方法有两种,一种是使用页名读取,另一种是使用页码读取(从0开始)

Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)

for (int j = 0; j < sheet.getColumns(); j++) {

Cell cell = sheet.getCell(j, 0);

if(cell.getContents().equals(s)){

for (int i = 0; i < sheet.getRows(); i++) {

cell = sheet.getCell(j, i);

System.out.println(""+0+""+j+":"+cell.getContents());

System.out.println(cell.getType());

}

}

}

} catch (BiffException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return "excelIndex";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: