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

时间:2022-09-23 07:47:22

本文实例讲述了Java使用excel工具类导出对象功能。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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程序设计有所帮助。

原文链接:http://blog.csdn.net/lovoo/article/details/77899306