bootstrap-table集成poi的下载功能

1,在bootstrap-table内集成表格下载按钮

bootstrap-table集成poi的下载功能_第1张图片

bootstrap-table集成poi的下载功能_第2张图片

 

bootstrap-table集成poi的下载功能_第3张图片

 

 

public static HSSFWorkbook downExcel(List entityArray,HttpServletResponse response,String excelName,BaseEntity01 baseEntity01) throws Exception {

// List strArray = new ArrayList();
String[] headers = (URLDecoder.decode(baseEntity01.getHeadExcel(), "UTF-8")).split(",");
String[] strModal = (URLDecoder.decode(baseEntity01.getBodyExcel(), "UTF-8")).split(",");

// 表头
// String[] headers = strArray.get(0);
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet();
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 18);
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++) { // 设置表头
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行
//表体
// String[] strModal = strArray.get(1);
for (int j = 0; j < entityArray.size(); j++) {// 行数据
Object objEntity = (Object) entityArray.get(j);
row = sheet.createRow(j+1);
// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
for (short i = 0; i < strModal.length; i++) {
HSSFCell cell = row.createCell(i);
String fieldName = strModal[i];
String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
String textValue = null;
if(i != 0){
//利用反射获取entity的字段值
Class tCls = objEntity.getClass();
Method getMethod = tCls.getMethod(getMethodName, new Class[] {});
Object value = getMethod.invoke(objEntity, new Object[] {});

if (value instanceof Date && value != null) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
textValue = sdf.format(date);
} else {
// 其它数据类型都当作字符串简单处理
if(value != null){
textValue = value.toString();
}
}
}else{
textValue = (j+1) +"";
}


HSSFRichTextString richString = new HSSFRichTextString(textValue);
HSSFFont font3 = workbook.createFont();
font3.setColor(HSSFColor.BLUE.index);// 定义Excel数据颜色
richString.applyFont(font3);
cell.setCellValue(richString);


response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment;filename="+new String(excelName.getBytes("gb2312"),"iso8859-1"));
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}


}
return workbook;// 返回工作簿
}

你可能感兴趣的:(bootstrap-table集成poi的下载功能)