1.引用jxls依赖
net.sf.jxls
jxls-core
1.0.5
2.EmployeeController.java
package com.exam.web;
import com.exam.domain.Employee;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
/**
* 这里接管了response,我们自己把response flush出去,所以返回类型是void(不要用什么String,否则会报响应已提交过的异常哦)
*/
@RequestMapping("/export")
public void export(HttpServletRequest request,HttpServletResponse response){
String templateFileName= request.getServletContext().getRealPath("/") + "/resources/templateFileName.xls";
String destFileName= "destFileName.xls";
//模拟数据
List staff = new ArrayList();
staff.add(new Employee("Derek", 35, 3000, 0.30));
staff.add(new Employee("Elsa", 28, 1500, 0.15));
staff.add(new Employee("Oleg", 32, 2300, 0.25));
Map beans = new HashMap();
beans.put("employees", staff);
XLSTransformer transformer = new XLSTransformer();
InputStream in=null;
OutputStream out=null;
//设置响应
response.setHeader("Content-Disposition", "attachment;filename=" + destFileName);
response.setContentType("application/vnd.ms-excel");
try {
in=new BufferedInputStream(new FileInputStream(templateFileName));
Workbook workbook=transformer.transformXLS(in, beans);
out=response.getOutputStream();
//将内容写入输出流并把缓存的内容全部发出去
workbook.write(out);
out.flush();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in!=null){try {in.close();} catch (IOException e) {}}
if (out!=null){try {out.close();} catch (IOException e) {}}
}
}
}
3.templateFileName.xls(内容是二进制,不容易贴出来)
上文源码:http://download.csdn.net/detail/xiejx618/7836191
如果不想使用上面的模板,还有一种更灵活自定义的方法(比如编码定义excel样式,动态增加多个sheet等等),也可以在Controller方法直接返回AbstractXlsView.下面是一个例子:
@RequestMapping(value = "listXls",method = RequestMethod.GET)
public View listXls() {
List list = getUsers();
return new AbstractXlsView() {
@Override
protected void buildExcelDocument(Map model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
addSheet(workbook.createSheet("UserStatistics1"), list.subList(0, 50));//不包括第50个
addSheet(workbook.createSheet("UserStatistics2"), list.subList(50,list.size()));
}
};
}
private List getUsers() {
List list = new ArrayList<>();
for (int i = 0; i < 103; i++) {
User user = new User();
user.setUsername("name" + i);
user.setPassword("pwd" + i);
user.setId((long) i);
list.add(user);
}
return list;
}
private void addSheet(Sheet sheet, List list) {
int rowNum=0;
Row row0 = sheet.createRow(rowNum++);
row0.createCell(0).setCellValue("ID");
row0.createCell(1).setCellValue("Username");
row0.createCell(2).setCellValue("Password");
row0.createCell(3).setCellValue("Height");
for (User user : list) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(user.getId());
row.createCell(1).setCellValue(user.getUsername());
row.createCell(2).setCellValue(user.getPassword());
row.createCell(3).setCellValue(user.getHeight());
}
}
顺便贴一下java项目(不是web项目)的例子源码:http://download.csdn.net/detail/xiejx618/7836227