POI excel導出
阿新 • • 發佈:2018-01-19
步驟 input 輸入 return 行數 final null sfc form
******************************************************************************************
excel表格導出,使用POI實現
******************************************************************************************
實現導出步驟
——配置導出excel模板,推薦使用03版xls格式,可以兼容高級版本
——處理導出數據List<Map<String, String>>
private List<Map<String, String>> getData(){ List<Map<String, String>> data = new ArrayList<>(); Map<String, String> map = null; for (int i = 0; i < 5; i++) { map = new HashMap<String, String>(); map.put("listNo", i + ""); map.put("userName", "name" + i); map.put("userAge", "" + (i + 20)); data.add(map); } return data; }
——獲取模板xls文件,通過輸入流讀到HSSFWorkbook 對象中
private HSSFWorkbook workbookTemplate() { File file = new File("f:/etom/pmms/src/test/resources/bdRoute.xls"); InputStream is = null; HSSFWorkbook workbook = null; try { is = new FileInputStream(file); workbook = new HSSFWorkbook(is); } catch (IOException e) { e.printStackTrace(); }finally { if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return workbook; }
——通過頁簽名獲取頁簽
private HSSFSheet getSheet(HSSFWorkbook workbook) {
String sheetName = "用戶信息";
HSSFSheet sheet = workbook.getSheet(sheetName);
if (null != sheet) {
return sheet;
}
return null;
}
——獲取規則,如${userName}
private String getValue(HSSFCell cell){ int type = cell.getCellType(); switch (type) { case Cell.CELL_TYPE_NUMERIC: return Double.toString(cell.getNumericCellValue()); case Cell.CELL_TYPE_BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); default: return ""; } }
——設置規則,如${userName}
private void setCellRule(HSSFRow firstDataRow, HSSFRow newRow) {
HSSFCell cellInRule = null;
HSSFCell newCell = null;
for (int i = 0, cellNum = firstDataRow.getLastCellNum(); i < cellNum; i++) {
cellInRule = firstDataRow.getCell(i);
newCell = newRow.createCell(i);
HSSFCellStyle style = cellInRule.getCellStyle();
newCell.setCellStyle(style);
String rule = getValue(cellInRule);
newCell.setCellValue(rule);
}
}
——設置頁簽模板,將內容行復制,行數與數據data的條數一致
// loop復制行的根據,rowCount復制多少行,上面截圖中,復制行是從行號為4開始
private void setSheet(HSSFSheet sheet, int loop, int rowCount) {
HSSFRow newRow = null;
HSSFRow firstDataRow = sheet.getRow(loop - 1);
for (int i = loop, maxRow = loop + rowCount; i < maxRow - 1; i++) {
newRow = sheet.createRow(i);
setCellRule(firstDataRow, newRow);
}
}
——具體設置單元格內容
private void setCellValue(HSSFRow row, Map<String, String> rowData) {
for (int i = 0, cellNum = row.getLastCellNum(); i < cellNum; i++) {
HSSFCell cell = row.getCell(i);
if (null == cell) {
continue;
}
String oldValue = getValue(cell);
Pattern pattern = Pattern.compile(patternRule);
Matcher matcher = pattern.matcher(oldValue);
if (matcher.find()) {
String key = matcher.group(1);
String value = rowData.getOrDefault(key, "");
cell.setCellValue(value);
}
}
}
——設置行內容(通過設置單元格內容)
// 設置內容,單元的範圍是從第loop行開始,到第loop+data.size()行結束
private void setValue(HSSFSheet sheet, int loop, List<Map<String, String>> data) {
HSSFRow row = null;
for (int i = loop - 1, rowNum = loop + data.size(), index = 0; i < rowNum - 1; i++) {
row = sheet.getRow(i);
setCellValue(row, data.get(index++));
}
}
——生成excel文件,格式為xls
private void createExcel(HSSFWorkbook workbook,String targetPath){
File excelFile = new File(targetPath);
OutputStream os = null;
try {
os = new FileOutputStream(excelFile);
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (null != os) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
——測試
@Test
public void test(){
List<Map<String, String>> data = getData();
String templatePath = "f:/etom/pmms/src/test/resources/user.xls";
HSSFWorkbook workbook = workbookTemplate(templatePath);
HSSFSheet sheet = getSheet(workbook);
setSheet(sheet, 5, data.size());
setValue(sheet, 5, data);
Long timeMillis = System.currentTimeMillis();
String targetPath = String.format("f:/etom/pmms/src/test/resources/%s.xls", Long.toString(timeMillis));
createExcel(workbook, targetPath);
}
——結果展示
POI excel導出