本文實例講述了Java使用excel工具類導出對象功能。分享給大家供大家參考,具體如下:
烏當網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
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表示手動控制 private int flushRows; /** * 4、導出文件行數(shù) */ private int rowNum; /** * 5、導出文件列數(shù) */ private int colNum; /** * 6、導出文件的存放路徑 */ private String filePath; /** * 7、下載導出文件的路徑 */ private String fileWebPath; /** * 8、文件名稱前綴 */ private String filePrefix; /** * 9、導出文件全路徑 */ private String fileAllPath; /** * 10、導出文件列標題 */ private ListfieldNames; /** * 11、導出文件每列代碼,用于反射獲取對象屬性值 */ private List fieldCodes; //---構(gòu)造方法----------------------------------------- public ExcelExportUtil() { } public ExcelExportUtil(SXSSFWorkbook workbook) { this.workbook = workbook; } public static ExcelExportUtil start(String filePath, String fileWebPath, String filePrefix, List fieldNames, List 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)建標題 * * @throws Exception */ public void writeTitles() throws Exception { rowNum = 0; colNum = fieldNames.size(); //創(chuàng)建行 Row row = sheet.createRow(rowNum); //在每列第一行輸出標題 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 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) : ""); } } /** * 手動刷新方法,如果flushRows為-1則需要使用此方法手動刷新內(nèi)存 * @param flushNum * @throws Exception */ public void flush(int flushNum) throws Exception{ ((SXSSFSheet)sheet).flushRows(flushNum); } /** * 導出文件 * @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; } /** * 導出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{ //導出字段代碼和名稱 String[] fieldArr = field.split(","); //獲取導出字段名稱 List fieldNames = new ArrayList (); //獲取導出字段代碼 List fieldCodes = new ArrayList (); for (int i = 0; i < fieldArr.length; i++) { String names = fieldArr[i]; String[] nameArr = names.split("#"); fieldNames.add(nameArr[1]); fieldCodes.add(nameArr[0]); } //開導出 ExcelExportUtil exportUtil = ExcelExportUtil.start(path, webpath,filePrefix, fieldNames,fieldCodes, flushRows); //導數(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 getFieldNames() { return fieldNames; } public void setFieldNames(List fieldNames) { this.fieldNames = fieldNames; } public List getFieldCodes() { return fieldCodes; } public void setFieldCodes(List fieldCodes) { this.fieldCodes = fieldCodes; } }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java操作Excel技巧總結(jié)》、《Java+MySQL數(shù)據(jù)庫程序設(shè)計總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java文件與目錄操作技巧匯總》及《Java操作DOM節(jié)點技巧總結(jié)》
希望本文所述對大家java程序設(shè)計有所幫助。