摘要:第一步創(chuàng)建需要導(dǎo)出的實(shí)體類導(dǎo)出學(xué)生基本信息的實(shí)體類姓名學(xué)號(hào)學(xué)院專業(yè)年級(jí)宿舍性別民族出生日期說(shuō)明這個(gè)為自定義注解,如果有字段在中不需要導(dǎo)出,則不加自定義注解即可。
第一步:創(chuàng)建需要導(dǎo)出的excel實(shí)體類
/** * 導(dǎo)出學(xué)生基本信息的實(shí)體類 * Created by staunch on 2016/11/22. */ @Data public class ExcelBaseInfo { @ExcelAnno(head = "姓名") private String name; @ExcelAnno(head = "學(xué)號(hào)") private String studentId; @ExcelAnno(head = "學(xué)院") private String CollegeName; @ExcelAnno(head = "專業(yè)") private String majorName; @ExcelAnno(head = "年級(jí)") private String gradeName; @ExcelAnno(head = "宿舍") private String dorm; @ExcelAnno(head = "性別") private String sex; @ExcelAnno(head = "民族") private String nation; @ExcelAnno(head = "出生日期") private Date birthday; }
說(shuō)明:@ExcelAnno這個(gè)為自定義注解,如果有字段在excel中不需要導(dǎo)出,則不加自定義注解即可。
第二步:自定義注解如下:
/** * Created by staunch on 2016/11/25. */ @Documented @Target({ElementType.METHOD, ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface ExcelAnno { String head() default ""; }
第三步:工具類源碼
/** * Created by staunch on 2016/11/22. */ @Log4j public class ExportExcelUtil{ /** * 這是一個(gè)通用的方法,利用了JAVA的反射機(jī)制,可以將放置在JAVA集合中并且符號(hào)一定條件的數(shù)據(jù)以EXCEL * 的形式輸出到指定IO設(shè)備上 * @param title 表格標(biāo)題名 * @param dataset 需要顯示的數(shù)據(jù)集合,集合中一定要放置符合javabean風(fēng)格的類的對(duì)象。 * @param out 與輸出設(shè)備關(guān)聯(lián)的流對(duì)象,可以將EXCEL文檔導(dǎo)出到本地文件或者網(wǎng)絡(luò)中 * @param pattern 如果有時(shí)間數(shù)據(jù),設(shè)定輸出格式。默認(rèn)為"yyy-MM-dd" */ @SuppressWarnings("deprecation") public void exportExcel(String title, List dataset, OutputStream out, String pattern) throws Exception { // 聲明一個(gè)工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一個(gè)表格 HSSFSheet sheet = workbook.createSheet(title); // 設(shè)置表格默認(rèn)列寬度為20個(gè)字節(jié) sheet.setDefaultColumnWidth((short) 20); // 生成一個(gè)樣式 HSSFCellStyle style = workbook.createCellStyle(); // 設(shè)置這些樣式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成一個(gè)字體 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字體應(yīng)用到當(dāng)前的樣式 style.setFont(font); // 生成并設(shè)置另一個(gè)樣式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一個(gè)字體 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字體應(yīng)用到當(dāng)前的樣式 style2.setFont(font2); if (dataset == null || dataset.size() == 0) return; T tempT = dataset.get(0); Field[] heads = tempT.getClass().getDeclaredFields(); List headList = new ArrayList<>(); //獲取字段注解的表頭 for(int i=0;i it = dataset.iterator(); int index = 0; while (it.hasNext()) { index++; row = sheet.createRow(index); T t = (T) it.next(); Field[] fields = t.getClass().getDeclaredFields(); List fieldsList = new ArrayList<>(); for (Field field : fields) { if(field.getAnnotations().length != 0) fieldsList.add(field); } for (Field field:fieldsList) { HSSFCell cell = row.createCell(fieldsList.indexOf(field)); cell.setCellStyle(style2); String fieldName = field.getName(); String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); Class tCls = t.getClass(); Method getMethod = tCls.getMethod(getMethodName, new Class[] {}); Object value = getMethod.invoke(t, new Object[] {}); // 判斷值的類型后進(jìn)行強(qiáng)制類型轉(zhuǎn)換 String textValue = null; if (value == null) { cell.setCellValue(""); } if (value instanceof Integer) { int intValue = (Integer) value; cell.setCellValue(intValue); } else if (value instanceof Float) { float fValue = (Float) value; cell.setCellValue(fValue); } else if (value instanceof Double) { double dValue = (Double) value; cell.setCellValue(dValue); } else if (value instanceof Long) { long longValue = (Long) value; cell.setCellValue(longValue); } else if (value instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new SimpleDateFormat(pattern); textValue = sdf.format(date); cell.setCellValue(textValue); } else { // 其它數(shù)據(jù)類型都當(dāng)作字符串簡(jiǎn)單處理 textValue = value==null? "":value.toString(); cell.setCellValue(textValue); } } } workbook.write(out); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/66279.html
摘要:一行代碼完成對(duì)象和之間的轉(zhuǎn)換。說(shuō)明屬性列名稱四版本更新日志版本,新特性導(dǎo)出支持對(duì)象裝換為,并且支持字節(jié)數(shù)組等多種導(dǎo)出方式導(dǎo)入支持轉(zhuǎn)換為對(duì)象,并且支持文件路徑等多種導(dǎo)入方式版本,新特性字段支持類型。 《Java對(duì)象和Excel轉(zhuǎn)換工具XXL-EXCEL》 showImg(https://segmentfault.com/img/remote/1460000012470335);showI...
Octopus 如何導(dǎo)入excel 如何導(dǎo)出excel Octopus Octopus 是一個(gè)簡(jiǎn)單的java excel導(dǎo)入導(dǎo)出工具. 如何導(dǎo)入excel 下面是一個(gè)excel文件中sheet的數(shù)據(jù),有四個(gè)學(xué)生信息. studentId name sex inTime score 20134123 John M 2013-9-1 89 20124524 Joyce F 2012...
摘要:消費(fèi)之后,多線程處理文件導(dǎo)出,生成文件后上傳到等文件服務(wù)器。前端直接查詢并且展現(xiàn)對(duì)應(yīng)的任務(wù)執(zhí)行列表,去等文件服務(wù)器下載文件即可。這客戶體驗(yàn)不友好,而且網(wǎng)絡(luò)傳輸,系統(tǒng)占用多種問(wèn)題。拓展閱讀導(dǎo)出最佳實(shí)踐框架 產(chǎn)品需求 產(chǎn)品經(jīng)理需要導(dǎo)出一個(gè)頁(yè)面的所有的信息到 EXCEL 文件。 需求分析 對(duì)于 excel 導(dǎo)出,是一個(gè)很常見(jiàn)的需求。 最常見(jiàn)的解決方案就是使用 poi 直接同步導(dǎo)出一個(gè) exc...
閱讀 3318·2023-04-26 03:06
閱讀 3744·2021-11-22 09:34
閱讀 1202·2021-10-08 10:05
閱讀 3159·2021-09-22 15:53
閱讀 3617·2021-09-14 18:05
閱讀 1530·2021-08-05 09:56
閱讀 2046·2019-08-30 15:56
閱讀 2172·2019-08-29 11:02