public void info(){
// 创建一个map集合,这里设置单元格的数据,如果是从数据库拿则将数据库数据放入就行
Map<String, Object> map = new HashMap<String, Object>();
map.put("sequence", "数据");
map.put("date", "数据");
map.put("chetaihao", "数据");
map.put("productName", "数据");
map.put("specification", "数据");
map.put("memo", "数据");
map.put("inspectRecordBizList", "数据");
map.put("chanpingmingcheng", "数据");
map.put("shifouquanchengluyun", "数据");
HSSFWorkbook wb = new HSSFWorkbook();//创建一个webbook,对应一个Excel文件
HSSFCellStyle textStyle = wb.createCellStyle();//用来设置单元格文本格式的属性参数
// textStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFDataFormat format = wb.createDataFormat();
textStyle.setDataFormat(format.getFormat("@"));
Sheet sheet = wb.createSheet("模板名称");//在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
Row row = sheet.createRow(0);//创建单元格,并设置值表头 设置表头居中
row.setRowStyle(textStyle);
int i = 0;
// 将map集合循环放入excel对应的单元格
for(String key : map.keySet()){
Cell cell = row.createCell(i);
cell.setCellStyle(textStyle);//设置单元格为文本格式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue((String) map.get(key));
i++;
}
Map<String, Object> maps = new HashMap<String, Object>();
map.put("sequence", "第二行数据");
map.put("date", "第二行数据");
map.put("chetaihao", "第二行数据");
map.put("productName", "第二行数据");
map.put("specification", "第二行数据");
map.put("memo", "第二行数据");
map.put("inspectRecordBizList", "第二行数据");
map.put("chanpingmingcheng", "第二行数据");
map.put("shifouquanchengluyun", "第二行数据");
Row rows = sheet.createRow(1);
i = 0;
// 循环map设置到对应的单元格,若要从数据库数据也同理
for(String key : map.keySet()){
Cell cell = rows.createCell(i);
cell.setCellStyle(textStyle);//设置单元格为文本格式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue((String) map.get(key));
i++;
}
OutputStream fos = null;
try {
fos = getResponse().getOutputStream();
String userAgent = getRequest().getHeader("USER-AGENT");
String fileName = "一体机客户产品配置填报表规范模板";
try {
if(StringUtils.contains(userAgent, "Mozilla")){
fileName = new String(fileName.getBytes(), "ISO8859-1");
}else {
fileName = URLEncoder.encode(fileName, "utf8");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
getResponse().setCharacterEncoding("UTF-8");
getResponse().setContentType("application/vnd.ms-excel;charset=utf-8");// 设置contentType为excel格式
//响应给浏览器已下载的形式(让浏览器下载文件)
getResponse().setHeader("Content-Disposition", "Attachment;Filename="+ fileName+".xls");
wb.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}