poi生成excel
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
public static String readJsonFile(String fileName) {
String jsonStr = "";
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void generateExcel(List<Map> mapList) {
try {
Workbook wb = new HSSFWorkbook();
FileOutputStream fout = new FileOutputStream("D:\\poi.xls");
Sheet sheet = wb.createSheet("第一个sheet页");
for (int i = 0; i < mapList.size(); i++) {
Row row = sheet.createRow(i);
int index = 0;
for (Object entry : mapList.get(i).entrySet()) {
Map.Entry<String, Object> entryV = (Map.Entry<String, Object>) entry;
row.createCell(index).setCellValue(entryV.getValue() + "");
index++;
}
}
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
String path = Demo01.class.getClassLoader().getResource("getWorkflowStatisticTotal.json").getPath();
String s = readJsonFile(path);
List<Map> maps = JSON.parseArray(s, Map.class);
generateExcel(maps);
}