Java excelツールクラスを使用したオブジェクトのエクスポート機能の例

8655 ワード

この例では、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 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);
    //      
    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  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プログラム設計に役立つことを願っています.