### Question:
ExcelUtils { public static void genExcelFile(String content,String title,String xlsFilePath){ List<LinkedHashMap> itemsList; itemsList = JSONUtils.toList(content, LinkedHashMap.class); if (CollectionUtils.isEmpty(itemsList)){ logger.error("itemsList is null"); throw new RuntimeException("itemsList is null"); } LinkedHashMap<String, Object> headerMap = itemsList.get(0); List<String> headerList = new ArrayList<>(); Iterator<Map.Entry<String, Object>> iter = headerMap.entrySet().iterator(); while (iter.hasNext()){ Map.Entry<String, Object> en = iter.next(); headerList.add(en.getKey()); } HSSFWorkbook wb = null; FileOutputStream fos = null; try { wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); HSSFRow row = sheet.createRow(0); row.setHeight((short)500); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.RIGHT); for (int i = 0; i < headerList.size(); i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(cellStyle); cell.setCellValue(headerList.get(i)); } int rowIndex = 1; for (LinkedHashMap<String, Object> itemsMap : itemsList){ Object[] values = itemsMap.values().toArray(); row = sheet.createRow(rowIndex); row.setHeight((short)500); rowIndex++; for (int j = 0 ; j < values.length ; j++){ HSSFCell cell1 = row.createCell(j); cell1.setCellStyle(cellStyle); cell1.setCellValue(String.valueOf(values[j])); } } for (int i = 0; i < headerList.size(); i++) { sheet.setColumnWidth(i, headerList.get(i).length() * 800); } File file = new File(xlsFilePath); if (!file.exists()) { file.mkdirs(); } fos = new FileOutputStream(xlsFilePath + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS); wb.write(fos); }catch (Exception e){ logger.error("generate excel error",e); throw new RuntimeException("generate excel error",e); }finally { if (wb != null){ try { wb.close(); } catch (IOException e) { logger.error(e.getMessage(),e); } } if (fos != null){ try { fos.close(); } catch (IOException e) { logger.error(e.getMessage(),e); } } } }  static void genExcelFile(String content,String title,String xlsFilePath);  }### Answer:
@Test public void testGenExcelFile() { String xlsFilePath = rootPath + System.getProperty("file.separator"); logger.info("xlsFilePath: "+xlsFilePath); String correctContent = "[{\"name\":\"ds name\",\"value\":\"ds value\"}]"; String incorrectContent1 = "{\"name\":\"ds name\",\"value\":\"ds value\"}"; String title = "test report"; ExcelUtils.genExcelFile(correctContent, title, xlsFilePath); File xlsFile = new File(xlsFilePath + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS); assertTrue(xlsFile.exists()); expectedException.expect(RuntimeException.class); expectedException.expectMessage("itemsList is null"); ExcelUtils.genExcelFile(incorrectContent1, title, xlsFilePath); }
@Test public void testGenExcelFileByCheckDir() { ExcelUtils.genExcelFile("[{\"a\": \"a\"},{\"a\": \"a\"}]", "t", "/tmp/xls"); File file = new File("/tmp/xls" + Constants.SINGLE_SLASH + "t" + Constants.EXCEL_SUFFIX_XLS); file.delete(); }