ExportExcel

主要java代码: 需要用到 :HttpServletResponse+HSSFWorkbook+HSSFSheet+HSSFCellStyle+ ExportExcel+HSSFRow

public void selectMyInfoExport(Map<String, Object> param, HttpServletResponse response) throws IOException {

HSSFWorkbook rankPoints = new HSSFWorkbook();

HSSFSheet sheet = rankPoints.createSheet();

// 样式

HSSFCellStyle style = rankPoints.createCellStyle();

style.setAlignment(CellStyle.ALIGN_RIGHT);

sheet.setDefaultColumnStyle(7, style);

List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

dataList = dbEleven2020Mapper.selectMyInfoExport(param);

ExportExcel exportExcel = new ExportExcel(rankPoints, sheet);

// 计算该报表的列数

int number = 9;

// 给工作表列定义列宽(实际应用自己更改列数)

for (int i = 0; i < number; i++) {

sheet.setColumnWidth(i, 5000);

}

// 设置列头

HSSFRow row0 = sheet.createRow(0);

// 设置行高

row0.setHeight((short) 800);

HSSFCell row3Cell = null;

// 创建不同的LIST的列标题

row3Cell = row0.createCell(0);

row3Cell.setCellValue(new HSSFRichTextString("用openID"));

row3Cell = row0.createCell(1);

row3Cell.setCellValue(new HSSFRichTextString("手机号"));

row3Cell = row0.createCell(2);

row3Cell.setCellValue(new HSSFRichTextString("着装"));

row3Cell = row0.createCell(3);

row3Cell.setCellValue(new HSSFRichTextString("选择的烦恼"));


// 循环创建中间的单元格的各项的值

for (int i = 0; i < dataList.size(); i++) {

HSSFRow row = sheet.createRow((short) i + 1);

Map<String, Object> draw = dataList.get(i);

String openId = "";

if (draw.get("openId") != null) {

openId = String.valueOf(draw.get("openId"));

}

String mobileNumber = "";

if (draw.get("mobileNumber") != null) {

mobileNumber = String.valueOf(draw.get("mobileNumber"));

}

String clothtype = "";

if (draw.get("clothtype") != null) {

clothtype = String.valueOf(draw.get("clothtype"));

}

String troubletype = "";

if (draw.get("troubletype") != null) {

// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// answer1At = sdf.format(draw.get("answer1At"));

troubletype = String.valueOf(draw.get("troubletype"));

}

 

exportExcel.cteateCell(rankPoints, row, 0, openId);

exportExcel.cteateCell(rankPoints, row, 1, mobileNumber);

exportExcel.cteateCell(rankPoints, row, 2, clothtype);

exportExcel.cteateCell(rankPoints, row, 3, troubletype);

 

}

OutputStream os = response.getOutputStream();

SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

date.format(new Date());

String currentTime = date.format(new Date()).toString();

String newDate = currentTime.substring(2, 4) + currentTime.substring(5, 7) + currentTime.substring(8, 10) + "_";

newDate += currentTime.substring(11, 13) + currentTime.substring(14, 16) + currentTime.substring(17, 19);

try {

response.reset();

response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("2020双11 互动信息", "UTF8") + "_" + newDate + ".xls");

response.setContentType("application/octet-stream; charset=utf-8");

os = response.getOutputStream();

rankPoints.write(os);

os.flush();

}

catch (Exception e) {

log.getLogger("festival2020_s").error("exportAnswer() err : ? ", e);

}

finally {

if (os != null) {

os.close();

}

}

}

 

 

posted @ 2020-10-23 15:10  夏之  阅读(842)  评论(0)    收藏  举报