java后台生成Excel文件需要引入poi,
以下是poi的pom可以引入自己的项目里面去
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
生成Excel的demo
@RequestMapping(value = "/exportExcel/{ids}")
@ResponseBody
public void exportExcel( HttpServletResponse response) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("店铺信息");// 创建工作表(Sheet)
int i = 0;
//设置头部内容
HSSFRow row = sheet.createRow(i);// 创建行,从0开始
HSSFCell cell = row.createCell(0);// 创建行的单元格,也是从0开始
cell.setCellValue("店铺名称");// 设置单元格内容
row.createCell(1).setCellValue("店铺联系电话");
row.createCell(2).setCellValue("店铺营业时间");
row.createCell(3).setCellValue("店铺所属行业");
row.createCell(4).setCellValue("店铺详细地址");
row.createCell(5).setCellValue("店铺logo");// 设置单元格内容,重载
// HSSFRow row1 = sheet.createRow(1);// 创建行,从0开始
// HSSFCell cell1 = row1.createCell(0);// 创建行的单元格,也是从0开始
// cell.setCellValue("店铺名称");// 设置单元格内容
// row.createCell(1).setCellValue("店铺地址");// 设置单元格内容,重载
// row.createCell(2).setCellValue("店铺营业时间");// 设置单元格内容,重载
//==需要填充的数据
int j = ++i;
HSSFRow zrow = sheet.createRow(j);// 创建行,从0开始
HSSFCell zcell = zrow.createCell(0);// 创建行的单元格,也是从0开始
zcell.setCellValue("xxx国际大酒店");
zrow.createCell(1).setCellValue("131xxxxxxxxxx");
zrow.createCell(2).setCellValue("上午11点");
zrow.createCell(3).setCellValue("软件");
zrow.createCell(4).setCellValue("北京市xxxxxxxxxxx");
zrow.createCell(5).setCellValue("http://www.xxxxxxxxxx.jpg");
OutputStream output;
try {
output = response.getOutputStream();
//清空缓存
response.reset();
//定义浏览器响应表头,顺带定义下载名,比如students(中文名需要转义)
String s = "店铺入驻信息"+System.currentTimeMillis();
response.setHeader("Content-disposition", "attachment;filename=" + new String(s.getBytes(), "iso-8859-1") + ".xls");
//定义下载的类型,标明是excel文件
response.setContentType("application/vnd.ms-excel");
//这时候把创建好的excel写入到输出流
workbook.write(output);
//养成好习惯,出门记得随手关门
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}