poi 导出excel表格



前台代码部分

//导出按钮
<a class="btn btn-app" onclick="exportExcel(${applyParticipants.get(0).cid})"><i class="fa fa-edit"></i>导出Excel</a>

//js代码部分(就仅仅做了一个跳转)
<script type="text/javascript">
//导出excel
function exportExcel(cid) {
    //新开标签页跳转
window.location.href = "toSignUp/export?cid=" + cid;
}
</script>

后台代码部分

//Controller 层
/**
* @Description
* 导出功能
* 创建excel
* @author luying
*
*/

@RequestMapping(value = "/export")
@ResponseBody
public void export(HttpServletRequest request, HttpServletResponse response,Integer cid) throws Exception {
try
{
ExcelUtil excelUtil = new ExcelUtil();
      //列名,excel表第一行数据
String [] rowsName={"编号","姓名","是否团队联系人","报名类型","身份证号","联系电话","所在地区","工作单位","纳税人编号","工作单位","电子邮箱"};
      //创建一个集合,集合中存放需要在excel中显示的数据
List<Object[]> dataCode0 = new ArrayList<Object[]>();
ApplyParticipants zjgl=new ApplyParticipants();
      //通过sql语句取到需要的数据
List<ApplyParticipants> applyList=toSignUpService.selectPersonMessageByPrimaryKey(cid);
if(dbDatas!=null&&!dbDatas.isEmpty()){
            //初始化Object[]数组
Object[] ab=null;
            //for循环,将applyList中的数据放入Object[]数组中
for (ApplyParticipants applyParticipants : applyList) {
ab=new Object[11];
ab[0]=applyParticipants.getPid();
ab[1]=applyParticipants.getPname();
ab[2]=applyParticipants.getTeamcontact();
ab[3]=applyParticipants.getRegistrationtypes();
ab[4]=applyParticipants.getIdnum();
ab[5]=applyParticipants.getPhone();
ab[6]=applyParticipants.getArea();
              //非空判断(isEmpty是公司自己封装的方法),如果没有数据就显示 暂无
if(applyParticipants.getWorkunits()==null||applyParticipants.getWorkunits().isEmpty()){
ab[7]="暂无";
}else{
ab[7]=applyParticipants.getWorkunits();
}

if(applyParticipants.getRatepayer()==null){
ab[8]="暂无";
}else{
ab[8]=applyParticipants.getRatepayer();
}
if(applyParticipants.getJob()==null||applyParticipants.getJob().isEmpty()){
ab[9]="暂无";
}else{
ab[9]=applyParticipants.getJob();
}
if(applyParticipants.getEmail()==null||applyParticipants.getEmail().isEmpty()){
ab[10]="暂无";
}else{
ab[10]=applyParticipants.getEmail();
}
              //将数据Object[] ab 放入到List<Object[]> dataCode0中
dataCode0.add(ab);
}
}
      //定义表名
String title = "报名人信息详情表";
      //定义文件名称
String fileName= new String("报名人信息详情表.xls".getBytes("UTF-8"), "UTF-8");
       //进入excelUtil中的exportExcel()方法中
excelUtil.exportExcel(title, rowsName, dataCode0, fileName,response);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}

//所用的工具类 excelUtil.class(里面好像是没有需要改的信息,具体使用的时候在判断)

package com.jusfoun.uums.util;

import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

//import java.io.OutputStream;
//import java.io.UnsupportedEncodingException;
//import javax.servlet.http.HttpServletResponse;

/**
*
*
*
* @author wxy
* @version [版本号, 2019年2月1日]
*/
public class ExcelUtil
{
/**
* 导出excel
* @param title 导出表的标题
* @param rowsName 导出表的列名
* @param dataList 需要导出的数据
* @param fileName 生成excel文件的文件名
* @param response
*/
public void exportExcel(String title, String[] rowsName, List<Object[]> dataList, String fileName, HttpServletResponse response)
{
//创建HSSFWorkbook
//响应到客户端
try {
HSSFWorkbook wb = this.export(title, rowsName, dataList, fileName);
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void setResponseHeader(HttpServletResponse response, String fileName)
{
try
{
try
{
fileName = new String(fileName.getBytes(), "ISO8859-1");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}

/*
* 导出数据
*/
private HSSFWorkbook export(String title, String[] rowName, List<Object[]> dataList, String fileName)
throws Exception
{

HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
HSSFSheet sheet = workbook.createSheet(title); // 创建工作表
int columnNum = rowName.length; // 定义所需列数
HSSFRow rowRowName = sheet.createRow(0); // 在索引0的位置创建行(最顶端的行)
// 将列头设置到sheet的单元格中
for (int n = 0; n < columnNum; n++)
{
HSSFCell cellRowName = rowRowName.createCell(n); // 创建列头对应个数的单元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置列头单元格的数据类型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); // 设置列头单元格的值
}

// 将查询出的数据设置到sheet对应的单元格中
for (int i = 0; i < dataList.size(); i++)
{
Object[] obj = dataList.get(i); // 遍历每个对象
HSSFRow row = sheet.createRow(i + 1); // 创建所需的行数
for (int j = 0; j < obj.length; j++)
{
HSSFCell cell = null; // 设置单元格的数据类型
if (j == 0)
{
cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i + 1);
}
else
{
cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);
if (!"".equals(obj[j]) && obj[j] != null)
{
cell.setCellValue(obj[j].toString()); // 设置单元格的值
}
}
}
}

// 让列宽随着导出的列长自动适应
for (int colNum = 0; colNum < columnNum; colNum++)
{
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++)
{
HSSFRow currentRow;
// 当前行未被使用过
if (sheet.getRow(rowNum) == null)
{
currentRow = sheet.createRow(rowNum);
}
else
{
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null)
{
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING)
{
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length)
{
columnWidth = length;
}
}
}
}
if (colNum == 0)
{
sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
}
else
{
sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
}
}
return workbook;
}
}
 
posted @ 2019-09-26 14:12  heartwarming  阅读(180)  评论(0编辑  收藏  举报