一、后台代码
package com.aitiman.bos.web.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.aitiman.bos.domain.Region;
import com.aitiman.bos.domain.Subarea;
import com.aitiman.bos.service.ISubareaService;
import com.aitiman.bos.utils.FileUtils;
import com.aitiman.bos.web.action.base.BaseAction;
@Controller
@Scope("prototype")
public class SubareaAction extends BaseAction<Subarea> {
@Autowired
private ISubareaService subareaService;
public String exportXLS() throws IOException {
//第一步查询数据
List<Subarea> list = subareaService.findAll();
//第二步使用poi将数据写到excel中
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet("分区数据");
HSSFRow row1 = sheet.createRow(0);
row1.createCell(0).setCellValue("分区编号");
row1.createCell(1).setCellValue("开始编号");
row1.createCell(2).setCellValue("结束编号");
row1.createCell(3).setCellValue("位置信息");
row1.createCell(4).setCellValue("省市区");
for(Subarea subarea : list) {
HSSFRow row = sheet.createRow(sheet.getLastRowNum()+1);
row.createCell(0).setCellValue(subarea.getId());
row.createCell(1).setCellValue(subarea.getStartnum());
row.createCell(2).setCellValue(subarea.getEndnum());
row.createCell(3).setCellValue(subarea.getPosition());
row.createCell(4).setCellValue(subarea.getRegion().getName());
}
//第三步 使用输出流进行文件下载
String filename = "分区数据.xls";
//获取文件类型
String mimeType = ServletActionContext.getServletContext().getMimeType(filename);
ServletOutputStream os = ServletActionContext.getResponse().getOutputStream();
//设置文件类型
ServletActionContext.getResponse().setContentType(mimeType);
//设置客户端浏览器类型
String agent = ServletActionContext.getRequest().getHeader("User-Agent");
//使用工具类编码文件名(解决无法传递中文文件名的问题)
filename = FileUtils.encodeDownloadFilename(filename, agent);
ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+filename);
hssfWorkbook.write(os);
return null;
}
}
二、中文编码工具类
package com.aitiman.bos.utils;
import java.io.IOException;
import java.net.URLEncoder;
import sun.misc.BASE64Encoder;
public class FileUtils {
/**
* 下载文件时,针对不同浏览器,进行附件名的编码
*
* @param filename
* 下载文件名
* @param agent
* 客户端浏览器
* @return 编码后的下载附件名
* @throws IOException
*/
public static String encodeDownloadFilename(String filename, String agent)
throws IOException {
if (agent.contains("Firefox")) { // 火狐浏览器
filename = "=?UTF-8?B?"
+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
+ "?=";
filename = filename.replaceAll("\r\n", "");
} else { // IE及其他浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+"," ");
}
return filename;
}
}