JAVA導(dǎo)出表格模板,利用反射條件自動填充List列表

/**

*

* @param response 請求流

* @param headers 列表頭

* @param dataset list列表

* @param fileName 導(dǎo)出名稱

* @param <T>

* @throws IOException

*/

public static <T> void exportExcel(HttpServletResponse response,String[] headers, List<T> dataset,String fileName) throws IOException{

? ? // 聲明一個工作薄

? ? HSSFWorkbook workbook =new HSSFWorkbook();

? ? // 生成一個表格

? ? HSSFSheet sheet = workbook.createSheet();

? ? // 設(shè)置表格默認(rèn)列寬度為15個字節(jié)

? ? sheet.setDefaultColumnWidth((short) 15);

? ? // 聲明一個畫圖的頂級管理器

? ? HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

? ? // 定義注釋的大小和位置,詳見文檔

? ? HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));

? ? // 設(shè)置注釋內(nèi)容

? ? comment.setString(new HSSFRichTextString("可以在POI中添加注釋!"));

? ? // 設(shè)置注釋作者,當(dāng)鼠標(biāo)移動到單元格上是可以在狀態(tài)欄中看到該內(nèi)容.

? ? comment.setAuthor("leno");

? ? // 產(chǎn)生表格標(biāo)題行

? ? 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);

? ? }

? ? // 遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行

? ? Iterator<T> it = dataset.iterator();

? ? int index =0;

? ? while (it.hasNext()) {

? ? ? ? index++;

? ? ? ? row = sheet.createRow(index);

? ? ? ? T t =(T) it.next();

? ? ? ? // 利用反射,根據(jù)javabean屬性的先后順序,動態(tài)調(diào)用getXxx()方法得到屬性值

? ? ? ? Field[] fields = t.getClass().getDeclaredFields();

? ? ? ? for (short i =0; i < fields.length; i++) {

? ? ? ? ? ? HSSFCell cell = row.createCell(i);

? ? ? ? ? ? Field field = fields[i];

? ? ? ? ? ? String fieldName = field.getName();

? ? ? ? ? ? String getMethodName ="get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? //Class? tCls = t.getClass();//原有的

? ? ? ? ? ? ? ? Class<?>? tCls = t.getClass();

? ? ? ? ? ? ? ? Method getMethod = tCls.getMethod(getMethodName);

? ? ? ? ? ? ? ? Object value = getMethod.invoke(t);

? ? ? ? ? ? ? ? // 判斷值的類型后進(jìn)行強(qiáng)制類型轉(zhuǎn)換

? ? ? ? ? ? ? ? String textValue =null;

? ? ? ? ? ? ? ? if (valueinstanceof Integer) {

? ? ? ? ? ? ? ? ? ? int intValue =(Integer) value;

? ? ? ? ? ? ? ? ? ? cell.setCellValue(intValue);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (valueinstanceof Float) {

? ? ? ? ? ? ? ? ? ? float fValue =(Float) value;

? ? ? ? ? ? ? ? ? ? textValue =new String(String.valueOf(fValue));

? ? ? ? ? ? ? ? ? ? cell.setCellValue(textValue);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (valueinstanceof Double) {

? ? ? ? ? ? ? ? ? ? double dValue =(Double) value;

? ? ? ? ? ? ? ? ? ? textValue =new String(String.valueOf(dValue));

? ? ? ? ? ? ? ? ? ? cell.setCellValue(textValue);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (valueinstanceof Long) {

? ? ? ? ? ? ? ? ? ? long longValue =(Long) value;

? ? ? ? ? ? ? ? ? ? cell.setCellValue(longValue);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (valueinstanceof Boolean) {

? ? ? ? ? ? ? ? ? ? boolean bValue =(Boolean) value;

? ? ? ? ? ? ? ? ? ? textValue ="男";

? ? ? ? ? ? ? ? ? ? if (!bValue) {

? ? ? ? ? ? ? ? ? ? ? ? textValue ="女";

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? } else if (valueinstanceof Date) {

? ? ? ? ? ? ? ? ? ? Date date =(Date) value;

? ? ? ? ? ? ? ? ? ? SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

? ? ? ? ? ? ? ? ? ? textValue = sdf.format(date);

? ? ? ? ? ? ? ? } else if (valueinstanceof byte[]) {

? ? ? ? ? ? ? ? ? ? // 有圖片時,設(shè)置行高為60px;buffer

? ? ? ? ? ? ? ? ? ? row.setHeightInPoints(60);

? ? ? ? ? ? ? ? ? ? // 設(shè)置圖片所在列寬度為80px,注意這里單位的一個換算

? ? ? ? ? ? ? ? ? ? sheet.setColumnWidth(i, (short) (35.7 *80));

? ? ? ? ? ? ? ? ? ? // sheet.autoSizeColumn(i);

? ? ? ? ? ? ? ? ? ? byte[] bsValue =(byte[]) value;

? ? ? ? ? ? ? ? ? ? HSSFClientAnchor anchor =new HSSFClientAnchor(0, 0, 1023, 255, (short) 6, index, (short) 6,

? ? ? ? ? ? ? ? ? ? ? ? ? ? index);

? ? ? ? ? ? ? ? ? ? anchor.setAnchorType(2);

? ? ? ? ? ? ? ? ? ? patriarch.createPicture(anchor, workbook.addPicture(bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? // 其它數(shù)據(jù)類型都當(dāng)作字符串簡單處理

? ? ? ? ? ? ? ? ? ? textValue = value ==null || value =="" ?"" : value.toString();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? // 如果不是圖片數(shù)據(jù),就利用正則表達(dá)式判斷textValue是否全部由數(shù)字組成

? ? ? ? ? ? ? ? if (textValue !=null) {

? ? ? ? ? ? ? ? ? ? Pattern p = Pattern.compile("^//d+(//.//d+)?$");

? ? ? ? ? ? ? ? ? ? Matcher matcher = p.matcher(textValue);

? ? ? ? ? ? ? ? ? ? if (matcher.matches()) {

? ? ? ? ? ? ? ? ? ? ? ? // 是數(shù)字當(dāng)作double處理

? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(Double.parseDouble(textValue));

? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? HSSFRichTextString richString =new HSSFRichTextString(textValue);

? ? ? ? ? ? ? ? ? ? ? ? HSSFFont font3 = workbook.createFont();

? ? ? ? ? ? ? ? ? ? ? ? font3.setColor(HSSFColor.BLUE.index);

? ? ? ? ? ? ? ? ? ? ? ? richString.applyFont(font3);

? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(richString);

? ? ? ? ? ? ? ? ? ? }

}

? ? ? ? ? ? } catch (SecurityException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {

? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? }

}

}

? ? ByteArrayOutputStream os =new ByteArrayOutputStream();

? ? workbook.write(os);

? ? byte[] content = os.toByteArray();

? ? InputStream is =new ByteArrayInputStream(content);

? ? response.reset();

? ? response.setContentType("application/vnd.ms-excel;charset=utf-8");

? ? response.setHeader("Content-Disposition", "attachment;filename=" +new String((fileName +".xls").getBytes(), "iso-8859-1"));

? ? ServletOutputStream out = response.getOutputStream();

? ? BufferedInputStream bis =null;

? ? BufferedOutputStream bos =null;

? ? try {

? ? ? ? bis =new BufferedInputStream(is);

? ? ? ? bos =new BufferedOutputStream(out);

? ? ? ? byte[] buff =new byte[2048];

? ? ? ? int bytesRead;

? ? ? ? while (-1 !=(bytesRead = bis.read(buff, 0, buff.length))) {

? ? ? ? ? ? bos.write(buff, 0, bytesRead);

? ? ? ? }

? ? ? ? bos.flush();? //不可少

? ? ? ? response.flushBuffer();//不可少

? ? } catch (Exception e) {

? ? ? ? e.printStackTrace();

? ? } finally {

? ? ? ? //關(guān)閉流,不可少

? ? ? ? if (bis !=null) {

? ? ? ? ? ? bis.close();

? ? ? ? }

? ? ? ? if (bos !=null) {

? ? ? ? ? ? bos.close();

? ? ? ? }

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容