导入Apache POI Maven jar包
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>
编写工具类
import java.util.List;
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;
/**
* Excel工具类
* @author YeZhiJian
*
*/
public class ExcelUtil {
/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,List<List<String>> contentList, HSSFWorkbook wb){
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//声明列对象
HSSFCell cell = null;
//创建标题
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//创建内容
for(int i=0;i<contentList.size();i++){
row = sheet.createRow(i + 1);
for(int j=0;j<contentList.get(i).size();j++){
// 调整宽度(15在这里表示一个像素)
sheet.setColumnWidth(j, 256 * 15);
//将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(contentList.get(i).get(j));
}
}
return wb;
}
}
编写业务层以及响应流接口
/**
* 导出人员管理报表
* @return
*/
@SuppressWarnings("all")
@RequestMapping(value = "/PersionnelManagementListVOExport")
@ResponseBody
public void PersionnelManagementListVOExport(
HttpServletRequest request,
HttpServletResponse response,
Long activityId
) throws Exception {
//获取数据
List<PersionnelManagementListVO> PersionnelManagementListVOs = userService.getPersionnelManagementListVOExecl(activityId);
//excel标题
String[] title = {"序号","发起人","队伍名称","昵称","真实姓名","性别","公司"
,"职务","手机号码","填写时间","审核状态(审核资料)"};
//excel文件名
String fileName = "人员管理表"+System.currentTimeMillis()+".xls";
//sheet名
String sheetName = "人员管理表";
// 内容
List<List<String>> contentList = new ArrayList<>();
// 数据需要对应标题字段位置
for (int i = 0; i < PersionnelManagementListVOs.size(); i++) {
PersionnelManagementListVO obj = PersionnelManagementListVOs.get(i);
List<String> shell = new LinkedList();
shell.add(i + 1 + "");
shell.add(obj.getInitiator());
shell.add(obj.getGroupName());
shell.add(obj.getNickName());
shell.add(obj.getRealname());
shell.add(obj.getGender());
shell.add(obj.getCompany());
shell.add(obj.getDuty());
shell.add(obj.getPhone());
shell.add("");
if (obj.getState() == 0) {
shell.add("否");
} else if (obj.getState() == 1){
shell.add("是");
}
contentList.add(shell);
}
//创建HSSFWorkbook
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, contentList, null);
//响应到客户端
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
System.out.println("导出报表发送响应流异常 : " + e.getMessage());
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
System.out.println("导出报表发送响应流异常 : " + ex.getMessage());
ex.printStackTrace();
}
}