Java数据下载生成excle表格
1.添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
<exclusions>
<exclusion>
<artifactId>commons-codec</artifactId>
<groupId>commons-codec</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.8</version>
</dependency>
2.查看jar包是否导入
3.java后台代码
```java
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@RequestMapping(value = "/exportssl/{kssj}/{jssj}/{username}", method = RequestMethod.GET)
@ResponseBody
public void excelssl(HttpServletResponse response ,HttpServletRequest request, @PathVariable String kssj,
@PathVariable String jssj,
@PathVariable String username)throws Exception {
Response res = new Response();
try {
response.setCharacterEncoding("UTF-8");
List<TongGiFenXi> list = null;
if (username.equals("1")) {
list = tonggifenxiservice.query(kssj, jssj);
} else if (username.equals("2")) {
list = tonggifenxiservice.queryYS(kssj, jssj);
}
//创建excel文件
HSSFWorkbook wb = new HSSFWorkbook();
//创建sheet页
HSSFSheet sheet = wb.createSheet("上市量");
//设置列宽 参数一:第几列,参数二:列宽(列宽有限制,会出现空指针)
sheet.setColumnWidth(0, 4000);
sheet.setColumnWidth(1, 7000);
sheet.setColumnWidth(2, 5000);
sheet.setColumnWidth(3, 20000);
sheet.setColumnWidth(4, 4000);
sheet.setColumnWidth(5, 4000);
//默认打开第几个sheet下标
wb.setSelectedTab(0);
//创建标题行
HSSFRow titleRow = sheet.createRow(0);
HSSFCell cells = titleRow.createCell(0);
cells.setCellValue("区域");
//水平居中
cells.getCellStyle().setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
cells.getCellStyle().setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
titleRow.createCell(1).setCellValue("楼盘名");
titleRow.createCell(2).setCellValue("主力户型");
titleRow.createCell(3).setCellValue("预计上市房源信息");
titleRow.createCell(4).setCellValue("装修标准");
titleRow.createCell(5).setCellValue("预计套数");
//遍历将数据放到excel列中
for (TongGiFenXi suf : list) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(suf.getQs());
dataRow.createCell(1).setCellValue(suf.getXmmc());
dataRow.createCell(2).setCellValue(suf.getZxmj()+"-"+suf.getZdmj());
dataRow.createCell(3).setCellValue(suf.getGazh());
dataRow.createCell(4).setCellValue(suf.getZxbz());
dataRow.createCell(5).setCellValue(suf.getYjts());
}
// 设置下载时客户端Excel的名称 (上面注释的改进版本,上面的中文不支持)
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
+dateformat.format(new Date())+ new String("上市量统计".getBytes(),"iso-8859-1") + ".xls");
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
res.data = list;
ouputStream.flush();
ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
res.hasError = true;
res.message = e.getMessage();
}
}
----启动项目调这个接口就会下载,下载地址是客户端设置的地址