mybatis使用foreach批量插入

/**
* 将数据存入excel中
* @param rankList
* @param fileName
* @throws IOException
*/
public void insertRank(List<Rank> rankList, String fileName, String sheetName) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
XSSFSheet sheet = workbook.createSheet(sheetName);
sheet.setColumnWidth(0, 10*256);
sheet.setColumnWidth(1, 18*256);
sheet.setColumnWidth(2, 10*256);
sheet.setColumnWidth(3, 12*256);
sheet.setColumnWidth(4, 12*256);
sheet.setColumnWidth(5, 12*256);
sheet.setColumnWidth(6, 12*256);
sheet.setColumnWidth(7, 12*256);
for(Rank r : rankList) {
/** 记录不存在插入新的一条*/
Row row = sheet.createRow(sheet.getLastRowNum() + 1);t
row.createCell(0).setCellValue(r.getRank().toString());
row.createCell(1).setCellValue(r.getTeamName());
row.createCell(2).setCellValue(r.getLeagueMatch());
row.createCell(3).setCellValue(r.getWin().toString());
row.createCell(4).setCellValue(r.getDraw().toString());
row.createCell(5).setCellValue(r.getLost().toString());
row.createCell(6).setCellValue(r.getGa().toString());
row.createCell(7).setCellValue(r.getGs().toString());
}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
outputStream.close();
createCellStyle(fileName, "");
}


public void createCellStyle(String fileName, String sheetName) throws IOException {
Workbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
Sheet sheet = workbook.getSheet(sheetName);
Font font = workbook.createFont();
font.setFontName("Consolas");
// font.setFontHeightInPoints((short) 12);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
Row row = sheet.getRow(i);
row.getCell(0).setCellStyle(cellStyle);
row.getCell(1).setCellStyle(cellStyle);
row.getCell(2).setCellStyle(cellStyle);
row.getCell(3).setCellStyle(cellStyle);
row.getCell(4).setCellStyle(cellStyle);
row.getCell(5).setCellStyle(cellStyle);
row.getCell(6).setCellStyle(cellStyle);
row.getCell(7).setCellStyle(cellStyle);
}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
outputStream.close();
}
posted @ 2024-07-22 17:05  狗狗听话  阅读(19)  评论(0编辑  收藏  举报