java map导出excel_java 导出excel (通过map传参)

这篇博客展示了如何在Java中使用Map作为参数导出Excel文件。首先,从userService获取Map类型的用户数据,然后创建HSSFWorkbook对象并设置表头。通过遍历Map,将数据填充到Excel表格的相应列中,最后设置响应头并输出到HTTP响应,实现Excel文件下载。
摘要由CSDN通过智能技术生成

@RequestMapping("/exportExcel")

public void exportExcel(Map map, HttpServletResponse response){

List> mapList = userService.getUserList(map);//通过Map map传参

HSSFWorkbook wb = new HSSFWorkbook();//声明工

Sheet sheet = wb.createSheet("数据权限申请审批表");//新建表

sheet.setDefaultColumnWidth(15);//设置表宽

HSSFCellStyle style = wb.createCellStyle();

HSSFFont font = wb.createFont();

font.setFontHeightInPoints((short) 12);

HSSFCellStyle headerStyle = wb.createCellStyle();

HSSFFont headerFont = wb.createFont();

headerFont.setFontHeightInPoints((short) 14);

headerStyle.setFont(headerFont);

CellRangeAddress cra0 = new CellRangeAddress(0, 1, 0, 9);

sheet.addMergedRegion(cra0);

sheet.setDefaultColumnWidth((short) 15);

Row row = sheet.createRow(0);

Cell cell1 = row.createCell(0);

cell1.setCellValue("数据权限申请审批表");

cell1.setCellStyle(headerStyle);

//设置字体样式

HSSFFont titleFont = wb.createFont();

Row row1 = sheet.createRow(2);

Cell cell = row1.createCell(0);

cell.setCellValue("ID");

cell.setCellStyle(style);

cell = row1.createCell(1);

cell.setCellValue("username");

cell.setCellStyle(style);

cell = row1.createCell(2);

cell.setCellValue("password");

cell.setCellStyle(style);

System.out.println("mapList:"+mapList);

int createRow = 0;

for(Map m:mapList){

row1 = sheet.createRow(3+createRow);

for (String key:m.keySet()){

if(key.equals("id")){

//createCell 0 表示第一列

row1.createCell(0).setCellValue(MapUtil.getStringValeByKey(m,key));

}

if(key.equals("username")){

//createCell 1 表示第二列

row1.createCell(1).setCellValue(MapUtil.getStringValeByKey(m,key));

}

if(key.equals("password")){

//createCell 2 表示第三列

row1.createCell(2).setCellValue(MapUtil.getStringValeByKey(m,key));

}

}

createRow++;

}

response.reset();

response.setContentType("application/msexcel;charset=UTF-8");

try {

SimpleDateFormat newsdf = new SimpleDateFormat("yyyyMMddHHmmss");

String date = newsdf.format(new Date());

response.addHeader("Content-Disposition", "attachment;filename=\""

+ new String(("数据权限申请审批表" + date + ".xls").getBytes("GBK"),

"ISO8859_1") + "\"");

OutputStream out = response.getOutputStream();

wb.write(out);

out.flush();

out.close();

} catch (FileNotFoundException e) {

JOptionPane.showMessageDialog(null, "导出失败!");

e.printStackTrace();

} catch (IOException e) {

JOptionPane.showMessageDialog(null, "导出失败!");

e.printStackTrace();

}

}

public class MapUtil {

public static String getStringValeByKey(Map,?> map,String key){

return map.get(key).toString().trim();

}

}

标签:传参,map,java,wb,row1,cell,createCell,key,setCellValue

来源: https://blog.csdn.net/qq_38198180/article/details/90741601

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值