java导出excel

<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("&quot;", "\""));
		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);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值