bootstrap-table整合poi的下載功能
1,在bootstrap-table內整合表格下載按鈕
public static HSSFWorkbook downExcel(List<?> entityArray,HttpServletResponse response,String excelName,BaseEntity01 baseEntity01) throws Exception {
// List<String[]> strArray = new ArrayList<String[]>();
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;// 返回工作簿
}