欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-17 10:10:22
本文实例讲述了java使用excel工具类导出对象功能。分享给大家供大家参考,具体如下: package com.gcloud.common; import o...

本文实例讲述了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<string> fieldnames;
  /**
   * 11、导出文件每列代码,用于反射获取对象属性值
   */
  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;
  }
  /**
   * 创建标题
   *
   * @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 <j ; i++) {
      argsclass[i] = args[i].getclass();
    }
    method method = ownerclass.getmethod(methodname, argsclass);
    return method.invoke(owner, args);
  }
  /**
   * 向导出文件写数据
   *
   * @param datalist 存放字符串数组
   * @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) : "");
    }
  }
  /**
   * 手动刷新方法,如果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<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;
  }
}

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

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

上一篇: git命令行简写配置

下一篇: