Java使用excel工具類導(dǎo)出對象功能示例
本文實(shí)例講述了Java使用excel工具類導(dǎo)出對象功能。分享給大家供大家參考,具體如下:
package com.gcloud.common; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import java.io.FileOutputStream; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; /** * Created by charlin on 2017/9/7. */ public class ExcelExportUtil { // 1、定義工作表 private SXSSFWorkbook workbook; // 2、定義sheet private Sheet sheet; // 3、定義保存在內(nèi)存中的數(shù)量,-1表示手動(dòng)控制 private int flushRows; /** * 4、導(dǎo)出文件行數(shù) */ private int rowNum; /** * 5、導(dǎo)出文件列數(shù) */ private int colNum; /** * 6、導(dǎo)出文件的存放路徑 */ private String filePath; /** * 7、下載導(dǎo)出文件的路徑 */ private String fileWebPath; /** * 8、文件名稱前綴 */ private String filePrefix; /** * 9、導(dǎo)出文件全路徑 */ private String fileAllPath; /** * 10、導(dǎo)出文件列標(biāo)題 */ private List<String> fieldNames; /** * 11、導(dǎo)出文件每列代碼,用于反射獲取對象屬性值 */ private List<String> fieldCodes; //---構(gòu)造方法----------------------------------------- public ExcelExportUtil() { } public ExcelExportUtil(SXSSFWorkbook workbook) { this.workbook = workbook; } public static ExcelExportUtil start(String filePath, String fileWebPath, String filePrefix, List<String> fieldNames, List<String> fieldCodes, int flushRows) throws Exception { ExcelExportUtil excelExportUtil = new ExcelExportUtil(); excelExportUtil.setFilePath(filePath); excelExportUtil.setFileWebPath(fileWebPath); excelExportUtil.setFilePrefix(filePrefix); excelExportUtil.setFieldNames(fieldNames); excelExportUtil.setFieldCodes(fieldCodes); //設(shè)置輸出行數(shù) excelExportUtil.setWorkbook(new SXSSFWorkbook(flushRows)); //設(shè)置sheet excelExportUtil.setSheet(excelExportUtil.getWorkbook().createSheet()); excelExportUtil.writeTitles(); return excelExportUtil; } /** * 創(chuàng)建標(biāo)題 * * @throws Exception */ public void writeTitles() throws Exception { rowNum = 0; colNum = fieldNames.size(); //創(chuàng)建行 Row row = sheet.createRow(rowNum); //在每列第一行輸出標(biāo)題 for (int i = 0; i < colNum; i++) { Cell cell = row.createCell(i); cell.setCellValue(fieldNames.get(i)); } } /** * 寫入對象數(shù)據(jù) * * @param datalist * @throws Exception */ public void writeDatas(List datalist) throws Exception { for (int i = 0; i < datalist.size(); i++) { rowNum++; //不斷創(chuàng)建行 Row row = sheet.createRow(rowNum); for (int j = 0; j < fieldCodes.size(); j++) { Object obj = datalist.get(j); //獲得get方法返回的值 Object value = invokeMethod(obj, fieldCodes.get(j), new Object[]{}); Cell cell = row.createCell(j); cell.setCellValue(value != null ? value.toString() : ""); } } } /** * 獲得get方法返回的值 * @param owner * @param fieldname * @param args * @return * @throws Exception */ private Object invokeMethod(Object owner, String fieldname, Object[] args) throws Exception { String methodName = "get" + fieldname.substring(0,1).toUpperCase() + fieldname.substring(1); Class ownerClass = owner.getClass(); Class[] argsClass = new Class[args.length]; for (int i = 0, j = argsClass.length ; i <j ; i++) { argsClass[i] = args[i].getClass(); } Method method = ownerClass.getMethod(methodName, argsClass); return method.invoke(owner, args); } /** * 向?qū)С鑫募憯?shù)據(jù) * * @param datalist 存放字符串?dāng)?shù)組 * @return */ public void writeDatasByStr(List<String> datalist) throws Exception { rowNum++; Row row = sheet.createRow(rowNum); int dataSize = datalist.size(); for (int i = 0; i < colNum; i++) { Cell cell = row.createCell(i); cell.setCellValue(dataSize > i ? datalist.get(i) : ""); } } /** * 手動(dòng)刷新方法,如果flushRows為-1則需要使用此方法手動(dòng)刷新內(nèi)存 * @param flushNum * @throws Exception */ public void flush(int flushNum) throws Exception{ ((SXSSFSheet)sheet).flushRows(flushNum); } /** * 導(dǎo)出文件 * @return * @throws Exception */ public String exportFile() throws Exception{ String fileName = filePrefix + "_" + DateUtil.getCurrentTimeFileName() + ".xlsx"; FileOutputStream fos = new FileOutputStream(filePath + fileName); workbook.write(fos); fos.close(); setFileAllPath(fileWebPath + fileName); return fileWebPath + fileName; } /** * 導(dǎo)出excel通用方法 * @param field * @param path * @param webpath * @param filePrefix * @param datas * @param flushRows * @return * @throws Exception */ public ExcelExportUtil excelExport(String field,String path,String webpath,String filePrefix,List datas,int flushRows) throws Exception{ //導(dǎo)出字段代碼和名稱 String[] fieldArr = field.split(","); //獲取導(dǎo)出字段名稱 List<String> fieldNames = new ArrayList<String>(); //獲取導(dǎo)出字段代碼 List<String> fieldCodes = new ArrayList<String>(); for (int i = 0; i < fieldArr.length; i++) { String names = fieldArr[i]; String[] nameArr = names.split("#"); fieldNames.add(nameArr[1]); fieldCodes.add(nameArr[0]); } //開導(dǎo)出 ExcelExportUtil exportUtil = ExcelExportUtil.start(path, webpath,filePrefix, fieldNames,fieldCodes, flushRows); //導(dǎo)數(shù)據(jù) exportUtil.writeDatas(datas); exportUtil.exportFile(); return exportUtil; } public static void main(String[] args) { //使用方法,調(diào)用 //excelExport } //----get set------------------------------------------------- public SXSSFWorkbook getWorkbook() { return workbook; } public void setWorkbook(SXSSFWorkbook workbook) { this.workbook = workbook; } public Sheet getSheet() { return sheet; } public void setSheet(Sheet sheet) { this.sheet = sheet; } public int getFlushRows() { return flushRows; } public void setFlushRows(int flushRows) { this.flushRows = flushRows; } public int getRowNum() { return rowNum; } public void setRowNum(int rowNum) { this.rowNum = rowNum; } public int getColNum() { return colNum; } public void setColNum(int colNum) { this.colNum = colNum; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getFileWebPath() { return fileWebPath; } public void setFileWebPath(String fileWebPath) { this.fileWebPath = fileWebPath; } public String getFilePrefix() { return filePrefix; } public void setFilePrefix(String filePrefix) { this.filePrefix = filePrefix; } public String getFileAllPath() { return fileAllPath; } public void setFileAllPath(String fileAllPath) { this.fileAllPath = fileAllPath; } public List<String> getFieldNames() { return fieldNames; } public void setFieldNames(List<String> fieldNames) { this.fieldNames = fieldNames; } public List<String> getFieldCodes() { return fieldCodes; } public void setFieldCodes(List<String> fieldCodes) { this.fieldCodes = fieldCodes; } }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java操作Excel技巧總結(jié)》、《Java+MySQL數(shù)據(jù)庫程序設(shè)計(jì)總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java文件與目錄操作技巧匯總》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java并發(fā)Lock接口實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Java并發(fā)Lock接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Java 內(nèi)置接口 Serializable示例詳解
這篇文章主要為大家介紹了Java 內(nèi)置接口 Serializable示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11詳解JVM 運(yùn)行時(shí)內(nèi)存使用情況監(jiān)控
這篇文章主要介紹了詳解JVM 運(yùn)行時(shí)內(nèi)存使用情況監(jiān)控,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09java通過DelayQueue實(shí)現(xiàn)延時(shí)任務(wù)
本文主要介紹了java通過DelayQueue實(shí)現(xiàn)延時(shí)任務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Java核心編程之文件隨機(jī)讀寫類RandomAccessFile詳解
這篇文章主要為大家詳細(xì)介紹了Java核心編程之文件隨機(jī)讀寫類RandomAccessFile,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08java并發(fā)編程工具類JUC之ArrayBlockingQueue
類ArrayBlockingQueue是BlockingQueue接口的實(shí)現(xiàn)類,它是有界的阻塞隊(duì)列,內(nèi)部使用數(shù)組存儲(chǔ)隊(duì)列元素,通過代碼給大家說明如何初始化一個(gè)ArrayBlockingQueue,并向其中添加一個(gè)對象,對java并發(fā)編程工具類ArrayBlockingQueue相關(guān)知識(shí)感興趣的朋友一起看看吧2021-05-05