JAVA 到處excel檔案的工具類
阿新 • • 發佈:2019-01-02
import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.Region; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; importjava.util.Collection; import java.util.Iterator; /** * Created by liyiwen1 on 2016/7/5. */ public class ExportExcelUtils<T> { private static final Logger logger = LoggerFactory.getLogger(ExportExcelUtils.class); /** * 這是一個通用的方法,利用了JAVA的反射機制,可以將放置在JAVA集合中並且符合一定條件的資料以EXCEL 的形式輸出到指定IO裝置上* * @param title * 表格標題名 * @param headers * 表格屬性列名陣列 * @param dataset * 需要顯示的資料集合,集合中一定要放置符合javabean風格的類的物件。此方法支援的 * javabean屬性的資料型別為String * @param out * 與輸出裝置關聯的流物件,可以將EXCEL文件匯出到本地檔案或者網路中 */ @SuppressWarnings("unchecked") public static void exportExcel(String title, String[] headers,Collection<T> dataset, OutputStream out){
// 宣告一個工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一個表格 HSSFSheet sheet = workbook.createSheet(title); // 設定表格預設列寬度為15個位元組 sheet.setDefaultColumnWidth((short) 15); // 生成一個樣式 HSSFCellStyle style = workbook.createCellStyle(); // 設定這些樣式 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); // 生成一個字型 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字型應用到當前的樣式 style.setFont(font); // 生成並設定另一個樣式 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); // 生成另一個字型 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字型應用到當前的樣式 style2.setFont(font2); // 宣告一個畫圖的頂級管理器 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // 定義註釋的大小和位置,詳見文件 HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); // 設定註釋內容 comment.setString(new HSSFRichTextString("可以在POI中添加註釋!")); // 設定註釋作者,當滑鼠移動到單元格上是可以在狀態列中看到該內容. comment.setAuthor("liyiwen"); // 產生表格標題行 HSSFRow row = sheet.createRow(0); for (short i = 0; i < headers.length; i++){ HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } // 遍歷集合資料,產生資料行 Iterator<T> it = dataset.iterator(); int index = 0; while (it.hasNext()) { index++; row = sheet.createRow(index); T t = (T) it.next(); // 利用反射,根據javabean屬性的先後順序,動態呼叫getXxx()方法得到屬性值 Field[] fields = t.getClass().getDeclaredFields(); for (short i = 0; i < fields.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style2); Field field = fields[i]; String fieldName = field.getName(); String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String textValue = null; try { Class tCls = t.getClass(); Method getMethod = tCls.getMethod(getMethodName, null); Object value = getMethod.invoke(t, null); textValue = (String)value; } catch (Exception e) { } if (!StringUtils.isBlank(textValue)) { HSSFRichTextString richString = new HSSFRichTextString(textValue); HSSFFont font3 = workbook.createFont(); font3.setColor(HSSFColor.BLUE.index); richString.applyFont(font3); if (index != 1){ HSSFComment shang = sheet.getCellComment(index - 1, i); HSSFComment xia = sheet.getCellComment(index, i); if (shang.getString().getString().equals(xia.getString().getString())){ sheet.addMergedRegion(new Region(index - 1,(short)i, index,(short)i)); }else{ cell.setCellValue(richString); } }else{ cell.setCellValue(richString); } } } } try{ workbook.write(out); }catch (Exception e){ logger.info("workbook.write(out) 發生IO錯誤", e); } } }