本文实例讲述了Java使用excel工具类导出对象功能。分享给大家供大家参考,具体如下:package com.GCloud.common;import org.apache.poi.ss.usermodel.Cell;import org
本文实例讲述了Java使用excel工具类导出对象功能。分享给大家供大家参考,具体如下:
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;public class ExcelExportUtil { // 1、定义工作表 private SXSSFWorkbook workbook; // 2、定义sheet private Sheet sheet; // 3、定义保存在内存中的数量,-1表示手动控制 private int flushRows; private int rowNum; private int colNum; private String filePath; private String fileWEBPath; private String filePrefix; private String fileAllPath; private List<String> fieldNames; private List<String> fieldCodes; //---构造方法----------------------------------------- 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); //设置输出行数 excelExportUtil.setWorkbook(new SXSSFWorkbook(flushRows)); //设置sheet excelExportUtil.setSheet(excelExportUtil.getWorkbook().createSheet()); excelExportUtil.writeTitles(); return excelExportUtil; } public void writeTitles() throws Exception { rowNum = 0; colNum = fieldNames.size(); //创建行 Row row = sheet.createRow(rowNum); //在每列第一行输出标题 for (int i = 0; i < colNum; i++) { Cell cell = row.createCell(i); cell.setCellValue(fieldNames.get(i)); } } public void writeDatas(List datalist) throws Exception { for (int i = 0; i < datalist.size(); i++) { rowNum++; //不断创建行 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() : ""); } } } 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); } 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) : ""); } } public void flush(int flushNum) throws Exception{ ((SXSSFSheet)sheet).flushRows(flushNum); } 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; } public ExcelExportUtil excelExport(String field,String path,String webpath,String filePrefix,List datas,int flushRows) throws Exception{ //导出字段代码和名称 String[] fieldArr = field.split(","); //获取导出字段名称 List<String> fieldNames = new ArrayList<String>(); //获取导出字段代码 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]); } //开导出 ExcelExportUtil exportUtil = ExcelExportUtil.start(path, webpath,filePrefix, fieldNames,fieldCodes, flushRows); //导数据 exportUtil.writeDatas(datas); exportUtil.exportFile(); return exportUtil; } public static void main(String[] args) { //使用方法,调用 //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; }}
--结束END--
本文标题: Java使用excel工具类导出对象功能示例
本文链接: https://www.lsjlt.com/news/222027.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0