<span style="white-space:pre"> </span>--request中获取Excel文件
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile billfile = (CommonsMultipartFile) multipartRequest.getFile("file");
<span style="white-space:pre"> </span>InputStream is=billfile.getInputStream();
<span style="white-space:pre"> </span>--读取Excel文件
try
{
book = new XSSFWorkbook(is); // Excel 2007获取方法
}catch (Exception e) {
book = new HSSFWorkbook(is); // Excel 2003获取方法
}
Sheet sheet = book.getSheetAt(0); // 创建工作表。
// 定义 row、cell
Row row; Cell c; String cell;
int totalRows = sheet.getLastRowNum(); // 总共有多少行,从0开始
for (int i = 1; i <= totalRows; i++) // 循环输出表格中的内容,首先循环取出行,再根据行循环取出列
{
row = sheet.getRow(i);
if (row == null) // 处理空行
{
continue;
}
LotteryUserBean lotteryUserBean = new LotteryUserBean(); //定义Bean存储Excel中数据
int totalCells = row.getLastCellNum(); <span style="white-space:pre"> </span> // 总共有多少列,从0开始
for (int j = row.getFirstCellNum(); j < totalCells; j++)
{
if (row.getCell(j) == null) // 处理空列
{
continue;
}
c = row.getCell(j); // 通过 row.getCell(j).toString() 获取单元格内容
c.setCellType(1);
cell = c.toString();
cell = row.getCell(j).toString();
if (j == 0) //第一列
{
lotteryUserBean.setF_user_num(cell); //将Excel中数据封装到L<span style="font-family: Arial, Helvetica, sans-serif;">otteryUserBean中</span>
}
logger.info(cell + "\t");
}
list.add(lotteryUserBean); //将<span style="font-family: Arial, Helvetica, sans-serif;">L</span><span style="font-family: Arial, Helvetica, sans-serif;">otteryUserBean添加到List中保存</span>
}
//list中保存了excel数据的的对象
} catch (FileNotFoundException e) {
logger.error("文件没找到 : " + e);
} catch (IOException e) {
logger.error("已运行IO异常: " + e);
}