版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sgl520lxl/article/details/82464988
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.mail.internet.MimeUtility;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequestMapping(value = "/downloadexcel")
public void downloadexcel(@RequestParam Map<String,String> reqPara, HttpServletResponse response, HttpServletRequest request){
String BeginDate = reqPara.get("BeginDate");
String EndDate = reqPara.get("EndDate");
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("beginDate", BeginDate);
map.put("endDate", EndDate);
List<AniuAccountExtendXylm> list = service.queryListByDate(map);
if(list != null){
// 开始输出到表格
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb =null;
HSSFSheet sheet=null;
HSSFRow row;
wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
sheet = wb.createSheet("高校炒股大赛报名列表");
//设置列宽
sheet.setColumnWidth(0, 7000);
sheet.setColumnWidth(1, 6500);
sheet.setColumnWidth(2, 6500);
sheet.setColumnWidth(3, 6500);
sheet.setColumnWidth(4, 6500);
sheet.setColumnWidth(5, 6500);
sheet.setColumnWidth(6, 6500);
sheet.setColumnWidth(7, 6500);
sheet.setColumnWidth(8, 6500);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
row = sheet.createRow((int) 0);
//设置第一行颜色的style
CellStyle style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 第四步,创建单元格,并设置值表头 设置表头居中
//HSSFCellStyle style = wb.createCellStyle();
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(style);
cell.setCellValue("aniuuid");
cell = row.createCell((short) 1);
cell.setCellStyle(style);
cell.setCellValue("学生名称");
cell = row.createCell((short) 2);
cell.setCellStyle(style);
cell.setCellValue("学校");
cell = row.createCell((short) 3);
cell.setCellStyle(style);
cell.setCellValue("手机号码");
cell = row.createCell((short) 4);
cell.setCellStyle(style);
cell.setCellValue("专业");
cell = row.createCell((short) 5);
cell.setCellStyle(style);
cell.setCellValue("年级");
cell = row.createCell((short) 6);
cell.setCellStyle(style);
cell.setCellValue("报名时间");
cell = row.createCell((short) 7);
cell.setCellStyle(style);
cell.setCellValue("老师aniuuid");
cell = row.createCell((short) 8);
cell.setCellStyle(style);
cell.setCellValue("团队名称");
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i+1);
AniuAccountExtendXylm qu = list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(qu.getAniuUid());
row.createCell((short) 1).setCellValue(qu.getStudentName());
row.createCell((short) 2).setCellValue(qu.getSchoolName());
row.createCell((short) 3).setCellValue(qu.getMobile());
row.createCell((short) 4).setCellValue(qu.getSpecialty());
row.createCell((short) 5).setCellValue(qu.getGrade());
row.createCell((short) 6).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(qu.getInsertTime()));
row.createCell((short) 7).setCellValue("");
row.createCell((short) 8).setCellValue("");
}
try {
String filename = "炒股大赛名单.xls";//设置下载时客户端Excel的名称
filename = encodeFilename(filename, request);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename="+filename);
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String encodeFilename(String filename, HttpServletRequest request) {
/**
* 获取客户端浏览器和操作系统信息
* 在IE浏览器中得到的是:User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
* 在Firefox中得到的是:User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
*/
String agent = request.getHeader("USER-AGENT");
try {
if ((agent != null) && (-1 != agent.indexOf("MSIE"))) {
String newFileName = URLEncoder.encode(filename, "UTF-8");
newFileName = StringUtils.replace(newFileName, "+", "%20");
if (newFileName.length() > 150) {
newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");
newFileName = StringUtils.replace(newFileName, " ", "%20");
}
return newFileName;
}
if ((agent != null) && (-1 != agent.indexOf("Mozilla")))
return MimeUtility.encodeText(filename, "UTF-8", "B");
return filename;
} catch (Exception ex) {
return filename;
}
}