ssm框架下的poi导入小记

时间:2021-01-11 16:37:01

POI导入的文件,在控制器(controller)中可以转换成inputstream用XSSFWorkbook(new inputstream(""));(XSSFWorkbook 接受的是07版的Excel)(HXXFWorkbook接受的是03版的Excel)例如:

在controller中:

public Result importExcelDemo(HttpServletRequest request, @RequestParam("file") MultipartFile file) {  InputStream is = null;  try {
         is = file.getInputStream();  } catch (IOException e) {
         e.printStackTrace();  }  Result result = demoService.importExcelDemo(is);  return result;  }
在service中接受inputStream:已07为例

public Result importExcelDemo(InputStream is) {
      Workbook workbook;  try {
         workbook = new XSSFWorkbook(is);  } catch (IOException e) {
         e.printStackTrace();  return new Result(1, "导入失败");  }
接受到后:

Sheet sheet = workbook.getSheetAt(0);
getSheetAt(index);代表Excel的

ssm框架下的poi导入小记

for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {

rowIndex代表行数sheet.getLastRowNum为Excel最后一条

Row row = sheet.getRow(rowIndex);
row代表列数

int cellIndex = 0; Cell cell = row.getCell(cellIndex); String name = formatter.formatCellValue(cell);
定义 读取从第一列下标为0 formmatter.formatCellValue得到第一列的值

得到的值放入对应的类的属性里面,写一条插入语句

至此POI导入成功













ssm框架下的poi导入小记