javaweb中如何获取项目的绝对根路径,及javaExcelApi示例

HttpSession session = request.getSession();

ServletContext servletContext = session.getServletContext();

String realPath = servletContext.getRealPath("");//F:\workspacesvn\wuyuan-web\src\main\webapp

try {

String filePath = "C:\\Users\\Joshua\\Desktop\\网点清单.xls"; 

realPath = realPath+File.separator+"lib"+File.separator+"网点清单.xls";

System.out.println("realPath:"+realPath);// F:\workspacesvn\wuyuan-web\src\main\webapp\lib\网点清单.xls

File file = new File(filePath);

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

Sheet sheet = wb.getSheet(0);

for (int i = 0; i < sheet.getRows(); i++) {  //外for是遍历每一行。

    if(i == 0){ //跳过列标题,从第二列读数据

    continue;

    }

for (int j = 0; j < sheet.getColumns(); j++) {  //内for循环,是遍历行中的每一列。

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

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

}

}

你可能感兴趣的:(javaweb中如何获取项目的绝对根路径,及javaExcelApi示例)