67,514
社区成员
发帖
与我相关
我的任务
分享
package com.fdway.tool;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
/**
* 读取excel表格
*
*/
public class InExcels {
/**
*
* @param filepath: excel文件路径
* @param sheetindex:工作表(sheet)的索引
* @return :返回所有单元格的值
*/
public static String[][] InputExcel(String filePath,Integer sheetIndex){
String result[][] = null;
try{
InputStream is = new FileInputStream(filePath);
if(is != null){
Workbook book = Workbook.getWorkbook(is);
if(book != null){
Sheet sheet = book.getSheet(sheetIndex);
if(sheet != null){
Cell cell = null;
/*求工作表的总列数*/
int iColumCount = sheet.getColumns();
/*求工作表的总行数*/
int iRowCount = sheet.getRows();
result = new String[iRowCount][iColumCount];
/*遍历每行每列,获得每个单元格的内容:getCell(j, i);参数顺序->先列后行*/
for(int i = 0;i<iRowCount ;i++){
for(int j=0;j<iColumCount;j++){
cell = sheet.getCell(j, i);
if(cell != null ){
result[i][j] = cell.getContents();
}
}
}
book.close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
InExcels ie = new InExcels();
String path = "D:\\test.xls";
String result[][]=ie.InputExcel(path, 0);
//遍历每行
for(int i=0;i<result.length;i++){
//遍历每行中的每列(单元格)
for(int j=0;j<result[i].length;j++){
System.out.print(result[i][j]+" ");
}
System.out.print("\n");
}
}
}
自己写的读取 excel工具类,拿走不谢,需要jxl.jar包。