POI导出人事报表:代码实现

人事报表导出

步骤分析

构造Excel表格数据
创建工作簿
创建sheet
创建行对象
创建单元格对象
填充数据,设置样式
下载

代码实现

(1)配置controller

/**
 * 当月人事报表导出
 *  参数:
 *      年月-月(2018-02%)
 */
@RequestMapping(value = "/export/{month}", method = RequestMethod.GET)
public void export(@PathVariable String month) throws Exception {
	//1.获取报表数据
	List<EmployeeReportResult> list = userCompanyPersonalService.findByReport(companyId,month);
	//2.构造Excel
	//创建工作簿
	//SXSSFWorkbook : 百万数据报表
	//Workbook wb = new XSSFWorkbook();
	SXSSFWorkbook wb = new SXSSFWorkbook(100); //阈值,内存中的对象数量最大数量
	//构造sheet
	Sheet sheet = wb.createSheet();
	//创建行
	//标题
	String [] titles = "编号,姓名,手机,最高学历,国家地区,护照号,籍贯,生日,属相,入职时间,离职类型,离职原因,离职时间".split(",");
	//处理标题

	Row row = sheet.createRow(0);

	int titleIndex=0;
	for (String title : titles) {
		Cell cell = row.createCell(titleIndex++);
		cell.setCellValue(title);
	}

	int rowIndex = 1;
	Cell cell=null;
	for(int i=0;i<10000;i++){
	for (EmployeeReportResult employeeReportResult : list) {
		row = sheet.createRow(rowIndex++);
		// 编号,
		cell = row.createCell(0);
		cell.setCellValue(employeeReportResult.getUserId());
		// 姓名,
		cell = row.createCell(1);
		cell.setCellValue(employeeReportResult.getUsername());
		// 手机,
		cell = row.createCell(2);
		cell.setCellValue(employeeReportResult.getMobile());
		// 最高学历,
		cell = row.createCell(3);
		cell.setCellValue(employeeReportResult.getTheHighestDegreeOfEducation());
		// 国家地区,
		cell = row.createCell(4);
		cell.setCellValue(employeeReportResult.getNationalArea());
		// 护照号,
		cell = row.createCell(5);
		cell.setCellValue(employeeReportResult.getPassportNo());
		// 籍贯,
		cell = row.createCell(6);
		cell.setCellValue(employeeReportResult.getNativePlace());
		// 生日,
		cell = row.createCell(7);
		cell.setCellValue(employeeReportResult.getBirthday());
		// 属相,
		cell = row.createCell(8);
		cell.setCellValue(employeeReportResult.getZodiac());
		// 入职时间,
		cell = row.createCell(9);
		cell.setCellValue(employeeReportResult.getTimeOfEntry());
		// 离职类型,
		cell = row.createCell(10);
		cell.setCellValue(employeeReportResult.getTypeOfTurnover());
		// 离职原因,
		cell = row.createCell(11);
		cell.setCellValue(employeeReportResult.getReasonsForLeaving());
		// 离职时间
		cell = row.createCell(12);
		cell.setCellValue(employeeReportResult.getResignationTime());
	}
	}
	//3.完成下载
	ByteArrayOutputStream os = new ByteArrayOutputStream();
	wb.write(os);
	new DownloadUtils().download(os,response,month+"人事报表.xlsx");
}
package com.learn.common.utils;

import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class DownloadUtils {
    public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {
        response.setContentType("application/octet-stream");
        returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1"));			//保存的文件名,必须和页面编码一致,否则乱码
        response.addHeader("content-disposition","attachment;filename=" + returnName);
        response.setContentLength(byteArrayOutputStream.size());
        ServletOutputStream outputstream = response.getOutputStream();	//取得输出流
        byteArrayOutputStream.writeTo(outputstream);					//写到输出流
        byteArrayOutputStream.close();									//关闭
        outputstream.flush();											//刷数据
    }
}
发布了2417 篇原创文章 · 获赞 62 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/Leon_Jinhai_Sun/article/details/105306324
今日推荐