Java使用excel工具类导出对象功能示例

网友投稿 325 2023-03-22


Java使用excel工具类导出对象功能示例

本文实例讲述了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;

/**

* Created by charlin on 2017/9/7.

*/

public class ExcelExportUtil {

// 1、定义工作表

private SXSSFWorkbook workbook;

// 2、定义sheet

private Sheet sheet;

// 3、定义保存在内存中的数量,-1表示手动控制

private int flushRows;

/**

* 4、导出文件行数

*/

private int rowNum;

/**

* 5、导出文件列数

*/

private int colNum;

/**

* 6、导出文件的存放路径

*/

private String filePath;

/**

* 7、下载导出文件的路径

*/

private String fileWebPath;

/**

* 8、文件名称前缀

*/

private String filePrefix;

/**

* 9、导出文件全路径

*/

private String fileAllPath;

/**

* 10、导出文件列标题

*/

private List fieldNames;

/**

* 11、导出文件每列代码,用于反射获取对象属性值

*/

private List fieldCodes;

//---构造方法-----------------------------------------

public ExcelExportUtil() {

}

public ExcelExportUtil(SXSSFWorkbook workbook) {

this.workbook = workbook;

}

public static ExcelExportUtil start(String filePath, String fileWebIskYcitPath, String filePrefix, List fieldNames, List fieldCodes, int flushRows) throws ExcIskYciteption {

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;

}

/**

* 创建标题

*

* @throws Exception

*/

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));

}

}

/**

* 写入对象数据

*

* @param datalist

* @throws Exception

*/

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() : "");

}

}

}

/**

* 获得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

argsClass[i] = args[i].getClass();

}

Method method = ownerClass.getMethod(methodName, argsClass);

return method.invoke(owner, args);

}

/**

* 向导出文件写数据

*

* @param datalist 存放字符串数组

* @return

*/

public void writeDatasByStr(List 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则需要使用此方法手动刷新内存

* @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);

//导数据

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 getFieldNames() {

return fieldNames;

}

public void setFieldNames(List fieldNames) {

this.fieldNames = fieldNames;

}

public List getFieldCodes() {

return fieldCodes;

}

public void setFieldCodes(List fieldCodes) {

this.fieldCodes = fieldCodes;

}

}

更多关于java相关内容感兴趣的读者可查看本站专题:《Java操作Excel技巧总结》、《Java+mysql数据库程序设计总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》及《Java操作DOM节点技巧总结》

希望本文所述对大家java程序设计有所帮助。

argsClass[i] = args[i].getClass();

}

Method method = ownerClass.getMethod(methodName, argsClass);

return method.invoke(owner, args);

}

/**

* 向导出文件写数据

*

* @param datalist 存放字符串数组

* @return

*/

public void writeDatasByStr(List 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则需要使用此方法手动刷新内存

* @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);

//导数据

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 getFieldNames() {

return fieldNames;

}

public void setFieldNames(List fieldNames) {

this.fieldNames = fieldNames;

}

public List getFieldCodes() {

return fieldCodes;

}

public void setFieldCodes(List fieldCodes) {

this.fieldCodes = fieldCodes;

}

}

更多关于java相关内容感兴趣的读者可查看本站专题:《Java操作Excel技巧总结》、《Java+mysql数据库程序设计总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》及《Java操作DOM节点技巧总结》

希望本文所述对大家java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序商品详情页规格属性选择示例代码
下一篇:Java文件操作工具类fileUtil实例【文件增删改,复制等】
相关文章

 发表评论

暂时没有评论,来抢沙发吧~