gradle
compile group: 'org.apache.poi', name: 'poi', version: '3.9';
compile group: 'org.apache.poi', name: 'poi-excelant', version: '3.9';
compile group: 'org.apache.poi', name: 'poi-scratchpad', version: '3.9';
demo
@PostMapping(value = "/excelWrite")
@ResponseBody
public HttpResult excelWrite(@RequestParam("file") MultipartFile multipartFile) throws IOException {
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet();
Row row0 = sheet.createRow(0);
int columnIndex = 0;
row0.createCell(columnIndex).setCellValue("");
row0.createCell(++columnIndex).setCellValue("名称");
row0.createCell(++columnIndex).setCellValue("规格");
row0.createCell(++columnIndex).setCellValue("单位");
row0.createCell(++columnIndex).setCellValue("数量");
row0.createCell(++columnIndex).setCellValue("单价");
row0.createCell(++columnIndex).setCellValue("总额");
for (int i = 0; i < productList.size(); i++) {
Product product = productList.get(i);
Row row = sheet.createRow(i + 1);
for (int j = 0; j < columnIndex + 1; j++) {
row.createCell(j);
}
columnIndex = 0;
// row.getCell(columnIndex).setCellValue(i + 1);
row.getCell(++columnIndex).setCellValue(product.getName());
row.getCell(++columnIndex).setCellValue(product.getType());
row.getCell(++columnIndex).setCellValue(product.getNum());
}
FileOutputStream out = new FileOutputStream("D:\汇总.xls");
hssfWorkbook.write(out);
out.flush();
return HttpResult.ok();
}