<button class="btn btn-success btn-sm pull-right" ng-click="export()">导出</button>
$scope.export = function() {
window.open('/expert/expertInfo/export?param=' + encodeURIComponent(angular.toJson($scope.param)));
}
package com.jy.expert.vo;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsView;;
@SuppressWarnings("deprecation")
public class ExcelView extends AbstractXlsView {
// HSSFWorkbook用来对excel进行读写
@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String filename = model.get("title").toString();
response.setContentType("application/octet-stream;charset=utf-8");
String reqCharset = request.getCharacterEncoding(); /* 根据request的getCharacterEncoding得到请求时的编码 */
filename = new String(filename.getBytes(reqCharset), "ISO8859-1");
response.setCharacterEncoding(reqCharset);
response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
// 创建sheet工作薄
Sheet sheet = workbook.createSheet(model.get("title").toString());
// 设置单元格格式
CellStyle style = workbook.createCellStyle();
// 设置颜色
style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.index);// 设置背景颜色
// 灰色
// 百分之四十
style.setFillPattern(CellStyle.SOLID_FOREGROUND);//
style.setAlignment(CellStyle.ALIGN_CENTER);// 设置格式 居中
List<Map<String, Object>> users = (List<Map<String, Object>>) model.get("param");
List<String> rowtype = (List<String>) model.get("type");
List<String> cellKey = (List<String>) model.get("cellKey");
Row row = null;// 行
Cell cell = null;// 列
int colCount = 0;
// 设置header部分
row = sheet.createRow(0);
for (int i = 0; i < rowtype.size(); i++) {
// 设置第一行i列
cell = row.createCell(colCount++);
cell.setCellStyle(style);
cell.setCellValue(rowtype.get(i));
}
for (int i = 1; i <= users.size(); i++) {
colCount = 0;
row = sheet.createRow(i);
for (int j = 0; j < cellKey.size(); j++) {
if (users.get(i - 1).get(cellKey.get(j).toString()) != null
&& "" != users.get(i - 1).get(cellKey.get(j).toString())) {
row.createCell(colCount++).setCellValue(users.get(i - 1).get(cellKey.get(j).toString()).toString());
} else {
row.createCell(colCount++).setCellValue("未知");
}
}
}
}
}
/**
* 导出
*
* @param modelMap
* @param param
* @return
*/
@ApiOperation("导出")
@GetMapping("/export")
public Object myExcel1(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> model = new HashMap<String, Object>();
@SuppressWarnings("unchecked")
Map<String, Object> param = JSONObject.fromObject(request.getParameter("param").replaceAll(""", "\""));
param.put("pageSize", 9999);
param.put("pageNum", 1);
List<Map<String, Object>> p = expertInfoService.selectbyparam(param).getRecords();
List<String> t = new ArrayList<String>();
List<String> cellKey = new ArrayList<String>();
Date dates = new Date();
cellKey.add("ep_library");
cellKey.add("name");
cellKey.add("sex");
cellKey.add("birthday");
cellKey.add("age");
cellKey.add("workunit");
cellKey.add("job");
cellKey.add("Educ11");
cellKey.add("Educ21");
cellKey.add("eptitle");
cellKey.add("proname");
cellKey.add("cityname");
cellKey.add("homearea");
cellKey.add("politicalOutlook");
//cellKey.add("ordernum");
t.add("专家库号");
t.add("姓名");
t.add("性别");
t.add("出生年月");
t.add("年龄");
t.add("工作单位");
t.add("职务");
t.add("学历1");
t.add("学历2");
t.add("职称");
t.add("现居住地(省)");
t.add("现居住地(市)");
t.add("现居住地(区)");
t.add("政治面貌");
// t.add("专长领域");
model.put("param", p);
model.put("cellKey", cellKey);
model.put("title", dates.getTime());
model.put("type", t);
return new ModelAndView(new ExcelView(), model);
}