ava将list中的数据生成excel表格并返回excel表格列名为参数名
时间: 2023-06-03 09:01:54 浏览: 146
Java通过POI读取Excel遍历数据,批量生成word文档
好的,这个问题属于技术问题。可以使用Java提供的Apache POI库来操作Excel文件,具体实现可以参考如下代码:
```
public void createExcel(List<Map<String, Object>> dataList, String[] columnNames, HttpServletResponse response) throws Exception {
// 创建Excel文件
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
// 设置列名
XSSFRow row = sheet.createRow(0);
for (int i = 0; i < columnNames.length; i++) {
XSSFCell cell = row.createCell(i);
cell.setCellValue(columnNames[i]);
}
// 将数据填充至Excel表格中
for (int i = 0; i < dataList.size(); i++) {
Map<String, Object> dataMap = dataList.get(i);
row = sheet.createRow(i + 1);
for (int j = 0; j < columnNames.length; j++) {
Object value = dataMap.get(columnNames[j]);
if (value == null) {
continue;
}
XSSFCell cell = row.createCell(j);
cell.setCellValue(String.valueOf(value));
}
}
// 输出Excel文件
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=export.xlsx");
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}
```
阅读全文