您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java怎么對Excel進行操作”,在日常操作中,相信很多人在Java怎么對Excel進行操作問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java怎么對Excel進行操作”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
想必大家在平常的項目開發過程中經常會涉及到Excel文件的導出功能,一般都會選擇Apache poi組件來完成,但是通常需要寫大量代碼,無疑增加了復雜度,且存在一個嚴重的問題就是非常的耗內存,容易導致OOM,幸運的是阿里開源了easyexcel,它是基于Apache poi而開發的一個組件,easyexcel重寫了poi對07版Excel的解析,能夠原本一個3M的excel用POI sax依然需要100M左右內存降低到KB級別,并且再大的excel不會出現內存溢出,但在實際使用過程中發現easyexcel操作具體每個cell還不是很便捷,所以我重寫了一個組件easyexcel-util,本項目基于阿里easyexcel,在此基礎上做了更進一步的封裝,使其寫入數據更加便捷,通過抽離出的ExcelDataHandler接口更容易處理每個cell的字體與樣式,下面將介紹如何使用easyexcel-util。
<dependency> <groupId>com.github.aifeinik</groupId> <artifactId>easyexcel-util</artifactId> <version>1.0</version> </dependency>
public class ExcelTest { CampaignModel m1 = new CampaignModel("2019-01-01", "10000000", "campaign1", "12.21", "100", "0.11"); CampaignModel m2 = new CampaignModel("2019-01-02", "12000010", "campaign2", "13", "99", "0.91"); CampaignModel m3 = new CampaignModel("2019-01-03", "12001010", "campaign3", "10", "210", "1.13"); CampaignModel m4 = new CampaignModel("2019-01-04", "15005010", "campaign4", "21.9", "150", "0.15"); ArrayList<CampaignModel> data1 = Lists.newArrayList(m1, m2); ArrayList<CampaignModel> data2 = Lists.newArrayList(m3, m4); @Test public void writeExcelWithOneSheet() throws Exception { ExcelUtil.writeExcelWithOneSheet(new File("G:/tmp/campaign.xlsx"), "campaign", data1); } }
寫入效果如下:
@Test public void writeExcelWithOneSheet2() throws Exception { ExcelUtil.writeExcelWithOneSheet(new File("G:/tmp/campaign.xlsx"), "campaign", data1, new CampaignDataHandler()); }
寫入效果如下:
@Test public void writeExcelWithMultiSheet() throws Exception { Map<String, List<? extends BaseRowModel>> map = new HashMap<>(); map.put("sheet1", data1); map.put("sheet2", data2); ExcelUtil.writeExcelWithMultiSheet(new File("G:/tmp/campaign.xlsx"), map); }
寫入效果如下:
@Test public void writeExcelWithMultiSheet2() throws Exception { Map<String, List<? extends BaseRowModel>> map = new HashMap<>(); map.put("sheet1", data1); map.put("sheet2", data2); ExcelUtil.writeExcelWithMultiSheet(new File("G:/tmp/campaign.xlsx"), map, new CampaignDataHandler()); }
寫入效果如下:
@Test public void writeOneSheetWithWrapWriter() { ExcelWrapWriter wrapWriter = null; try { OutputStream os = new FileOutputStream("G:/tmp/campaign.xlsx"); //默認樣式 //wrapWriter = new ExcelWrapWriter(os, ExcelTypeEnum.XLSX); //自定義excel樣式 wrapWriter = new ExcelWrapWriter(os, ExcelTypeEnum.XLSX, new CampaignDataHandler()); List<CampaignModel> models1 = Lists.newArrayList(m1, m2); List<CampaignModel> models2 = Lists.newArrayList(m3, m4); //第一批次寫入設置包含head頭 ExcelUtil.writeExcelWithOneSheet(wrapWriter, "sheet1", true, models1); //第二批次開始不需要在寫入head頭 ExcelUtil.writeExcelWithOneSheet(wrapWriter, "sheet1", false, models2); } catch (Exception e) { e.printStackTrace(); } finally { //close IO if (wrapWriter != null) { wrapWriter.finish(); } } }
數據分批寫入excel文件,可通過該方式寫入超大數據,而不至于一次寫入大數據量導致OOM問題
2.2.6 大數據量分批寫入多個sheet
@Test public void writeMultiSheetWithWrapWriter() { ExcelWrapWriter wrapWriter = null; try { //os流不需要單獨close,可通過wrapWriter.finish()來關閉 OutputStream os = new FileOutputStream("G:/tmp/campaign.xlsx"); //默認樣式 //wrapWriter = new ExcelWrapWriter(os, ExcelTypeEnum.XLSX); //自定義excel樣式 wrapWriter = new ExcelWrapWriter(os, ExcelTypeEnum.XLSX, new CampaignDataHandler()); Map<String, List<? extends BaseRowModel>> batch2 = new HashMap<>(); List<CampaignModel> models1 = Lists.newArrayList(m1, m2); List<CampaignModel> models2 = Lists.newArrayList(m3, m4); batch2.put("sheet1", models1); batch2.put("sheet2", models2); Map<String, List<? extends BaseRowModel>> batch3 = new HashMap<>(); List<CampaignModel> models3 = Lists.newArrayList(m4, m2); List<CampaignModel> models4 = Lists.newArrayList(m3, m1); batch3.put("sheet1", models3); batch3.put("sheet2", models4); //第一批次寫入設置包含head頭 ExcelUtil.writeExcelWithMultiSheet(wrapWriter, true, batch2); //第二批次開始不需要在寫入head頭 ExcelUtil.writeExcelWithMultiSheet(wrapWriter, false, batch3); } catch (Exception e) { e.printStackTrace(); } finally { //close IO if (wrapWriter != null) { wrapWriter.finish(); } } }
通過該注解更加方便的處理每個數據的具體格式, 內部采用MessageFormat.format進行數據格式化,如下代碼,其中cost花費字段注解了@ExcelValueFormat(format = "{0}$") 那么如果cost = 100 則寫入Excel后內容為100$
@Data public class CampaignModel extends BaseRowModel implements Serializable { @ExcelProperty(value = "日期", index = 0) private String day; @ExcelProperty(value = "廣告系列 ID", index = 1) private String campaignId; @ExcelProperty(value = "廣告系列", index = 2) private String campaignName; @ExcelProperty(value = "費用", index = 3) @ExcelValueFormat(format = "{0}$") private String cost; @ExcelProperty(value = "點擊次數", index = 4) private String clicks; @ExcelProperty(value = "點擊率", index = 5) @ExcelValueFormat(format = "{0}%") private String ctr; }
public interface ExcelDataHandler { /** * Excel head頭部字體設置 * @param font * @param cellIndex 列索引 */ void headFont(Font font, int cellIndex); /** * Excel head頭部樣式設置 * @param style * @param cellIndex 列索引 */ void headCellStyle(CellStyle style, int cellIndex); /** * Excel 除head外的內容字體設置 * @param font * @param cellIndex 列索引 */ void contentFont(Font font, int cellIndex, Object data); /** * Excel 除head外的內容樣式設置 * @param style * @param cellIndex 列索引 */ void contentCellStyle(CellStyle style, int cellIndex); /** * Excel sheet * @param sheetIndex sheet索引 * @param sheet */ void sheet(int sheetIndex, Sheet sheet); }
列如實現類如下:
public class CampaignDataHandler implements ExcelDataHandler { @Override public void headCellStyle(CellStyle style, int cellIndex) { style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); } @Override public void headFont(Font font, int cellIndex) { font.setColor(IndexedColors.WHITE.getIndex()); } @Override public void contentCellStyle(CellStyle style, int cellIndex) { style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); } @Override public void contentFont(Font font, int cellIndex, Object data) { CampaignModel campaign = (CampaignModel) data; switch (cellIndex) { case 4: //這里的值為Model對象中ExcelProperty注解里的index值 if (Long.valueOf(campaign.getClicks()) > 100) { //表示將點擊次數大于100的第4列也就是點擊次數列的cell字體標記為紅色 font.setColor(IndexedColors.RED.getIndex()); font.setFontName("宋體"); font.setItalic(true); font.setBold(true); } break; } } @Override public void sheet(int sheetIndex, Sheet sheet) { System.out.println("sheetIndex = [" + sheetIndex + "]"); } }
本文主要介紹了easyexcel-util組件不同場景的使用方式、通過ExcelValueFormat注解可以方便的處理數據的具體格式,以及通過ExcelDataHandler 接口來靈活設置具體每個cell的樣式與字體。
到此,關于“Java怎么對Excel進行操作”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。