java excel导出(基于注解)
小白,做日志只是为了方便自己查看,能帮到别人当然更好,不喜勿喷。
上代码
依赖:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency>
注解,使用了俩个注解,一个是 sheet 公用属性,以及单元格属性,只是简单的几个属性,可自行扩展。
sheet 公用注解:
package com.authorize.utils.excel;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/**
*
@filename ExcelBookAnnotation.java
@pakage com.authorize.utils.excel
@descption TODO(用一句话表述类的作用)
@author Pandong
@date 2019 年 4 月 8 日
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelBookAnnotation {/**
- 标题
- @return
*/
String title();}
单元格注解:
package com.authorize.utils.excel;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import org.apache.poi.hssf.util.HSSFColor;
/**
*
@filename ExcelAnnotation.java
@pakage com.authorize.utils.excel
@descption TODO(用一句话表述类的作用)
@author Pandong
@date 2019 年 4 月 8 日
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelColAnnotation {/**
- 列名
- @return
*/
String text() default "";/**
- 列宽
- @return
*/
int colWidth() default 6000;/**
- 字体颜色,默认黑色
- @return
*/
short color() default HSSFColor.BLACK.index;/**
- 导出是是否忽略该字段, 默认不忽略
- @return
*/
int ignore() default 0;}
实体类,其中使用了 lombok,感兴趣可以百度一下,不用可以不相关注解删掉,自己写 get/set 等方法。
package com.authorize.utils.excel;import java.io.Serializable;
import org.apache.poi.hssf.util.HSSFColor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/**
*
@filename ExcelBean.java
@pakage com.authorize.utils.excel
@descption TODO(用一句话表述类的作用)
@author Pandong
@date 2019 年 4 月 8 日
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelBookAnnotation(title = "日常工作表")
public class ExcelBean implements Serializable{@ExcelColAnnotation( ignore = 1 )
private static final long serialVersionUID = 4248622093488850427L;@ExcelColAnnotation(colWidth = 8000, text = "姓名",color = HSSFColor.RED.index)
private String name;
@ExcelColAnnotation( text = "年龄", colWidth = 2000)
private int age;
@ExcelColAnnotation( text = "地址", colWidth = 12000)
private String addr;}
最后就是 excel 导出的工具类了:
package com.authorize.utils.excel;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import com.authorize.utils.CommonUtils;
import com.authorize.utils.exception.CustomException;public class ExcelUtils<T> {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Log log = LogFactory.getLog(ExcelUtils.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 行高 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> rowHeight = 400<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 列宽 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> colWidth = 8500<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 起始位置 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> rowIndex = 0<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 默认标题 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> String title = "defaultExcel"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Workbook workbook; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Sheet sheet; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 公共列样式 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> CellStyle cellStyle; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 操作的实体类 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> T obj; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 列属性集合 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> List<Map<String,Object>> colList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 列样式集合 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> List<CellStyle> styleList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">private</span> ClassUtils util = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ClassUtils(); </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> ExcelUtils( T obj ) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.obj =<span style="color: rgba(0, 0, 0, 1)"> obj; initWorkbook(); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> ExcelUtils(T obj,<span style="color: rgba(0, 0, 255, 1)">int</span> rowHeight, <span style="color: rgba(0, 0, 255, 1)">int</span> colWidth, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> rowIndex, String title) { </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">(obj); </span><span style="color: rgba(0, 0, 255, 1)">this</span>.rowHeight =<span style="color: rgba(0, 0, 0, 1)"> rowHeight; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.colWidth =<span style="color: rgba(0, 0, 0, 1)"> colWidth; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.rowIndex =<span style="color: rgba(0, 0, 0, 1)"> rowIndex; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.title =<span style="color: rgba(0, 0, 0, 1)"> title; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 默认文档设置文档 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initWorkbook() { </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( CommonUtils.isEmpty(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.obj) ) { </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> CustomException("未指定导出实体类,无法进行导出操作"<span style="color: rgba(0, 0, 0, 1)">); } util.parseBookAnnotation(); workbook </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XSSFWorkbook(); sheet </span>= workbook.createSheet(<span style="color: rgba(0, 0, 255, 1)">this</span>.title); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建工作页</span> sheet.setDefaultColumnWidth(colWidth); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置默认列宽</span> cellStyle =<span style="color: rgba(0, 0, 0, 1)"> createCellStyle(); cellStyle.setFont(createFont(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, (<span style="color: rgba(0, 0, 255, 1)">short</span>)0, (<span style="color: rgba(0, 0, 255, 1)">short</span>)0<span style="color: rgba(0, 0, 0, 1)">)); titleSetting(); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 标题、列名相关设置 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> titleSetting() { Row topRow </span>= createRow((<span style="color: rgba(0, 0, 255, 1)">short</span>)600<span style="color: rgba(0, 0, 0, 1)">); mergedRegion(</span>0, 0, 0, <span style="color: rgba(0, 0, 255, 1)">this</span>.colList.size()-1); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 合并标题行</span> Cell cell = createCell(topRow,0<span style="color: rgba(0, 0, 0, 1)">,cellStyle); cell.setCellValue(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.title); Row textRow </span>= createRow((<span style="color: rgba(0, 0, 255, 1)">short</span>)0<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span> ( <span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < colList.size(); i++<span style="color: rgba(0, 0, 0, 1)"> ) { Map</span><String,Object> fieldMap =<span style="color: rgba(0, 0, 0, 1)"> colList.get(i); sheet.setColumnWidth(i, Integer.parseInt(fieldMap.get(</span>"width"<span style="color: rgba(0, 0, 0, 1)">).toString())); Cell cell1 </span>=<span style="color: rgba(0, 0, 0, 1)"> createCell(textRow,i,cellStyle); cell1.setCellValue(fieldMap.get(</span>"text"<span style="color: rgba(0, 0, 0, 1)">).toString()); addColStlye(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, (<span style="color: rgba(0, 0, 255, 1)">short</span>)0, Short.parseShort(fieldMap.get("color"<span style="color: rgba(0, 0, 0, 1)">).toString())); } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 创建字体对象 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> fontName * 字体库名称 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> fontSize * 字体大小-传0默认14 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> color * 字体颜色参考{</span><span style="color: rgba(128, 128, 128, 1)">@link</span><span style="color: rgba(0, 128, 0, 1)"> HSSFColor.BLACK.index} * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> Font createFont(String fontName, <span style="color: rgba(0, 0, 255, 1)">short</span> fontSize,<span style="color: rgba(0, 0, 255, 1)">short</span><span style="color: rgba(0, 0, 0, 1)"> color) { Font font </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.workbook.createFont(); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> ( CommonUtils.isEmpty(fontName) ) { fontName </span>= "宋体"<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( fontSize == 0<span style="color: rgba(0, 0, 0, 1)"> ) { fontSize </span>= (<span style="color: rgba(0, 0, 255, 1)">short</span>)14<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( color == 0<span style="color: rgba(0, 0, 0, 1)"> ) { color </span>=<span style="color: rgba(0, 0, 0, 1)"> HSSFColor.BLACK.index; } font.setFontName(</span>"宋体"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置为宋体字</span> font.setFontHeightInPoints(fontSize); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体大小</span>
font.setColor(color);
return font;
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 创建列样式 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> alignment * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> vertical * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> CellStyle createCellStyle(<span style="color: rgba(0, 0, 255, 1)">short</span><span style="color: rgba(0, 0, 0, 1)"> ...alignments) { CellStyle style </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.workbook.createCellStyle(); </span><span style="color: rgba(0, 0, 255, 1)">short</span> alignment = HSSFCellStyle.ALIGN_CENTER_SELECTION,vertical =<span style="color: rgba(0, 0, 0, 1)"> HSSFCellStyle.VERTICAL_CENTER; </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( alignments.length > 0<span style="color: rgba(0, 0, 0, 1)"> ) { alignment </span>= alignments[0<span style="color: rgba(0, 0, 0, 1)">]; </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( alignments.length > 1<span style="color: rgba(0, 0, 0, 1)"> ) { vertical </span>= alignments[1<span style="color: rgba(0, 0, 0, 1)">]; } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">水平居中</span>
style.setAlignment(alignment);
//垂直居中
style.setVerticalAlignment(vertical);
return style;
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 合并行、列 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> firstRow * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> lastRow * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> firstCol * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> lastCol </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> mergedRegion(<span style="color: rgba(0, 0, 255, 1)">int</span> firstRow, <span style="color: rgba(0, 0, 255, 1)">int</span> lastRow, <span style="color: rgba(0, 0, 255, 1)">int</span> firstCol, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> lastCol) { CellRangeAddress region </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> CellRangeAddress(firstRow,lastRow,firstCol,lastCol); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 合并行</span>
sheet.addMergedRegion(region);
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 创建单元格 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> row * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> index * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> style * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> Cell createCell ( Row row,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> index,CellStyle style ) { Cell cell </span>=<span style="color: rgba(0, 0, 0, 1)"> row.createCell(index); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> ( CommonUtils.isNotEmpty(style) ) { cell.setCellStyle(style); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cell; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 创建行 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> Row createRow ( <span style="color: rgba(0, 0, 255, 1)">short</span><span style="color: rgba(0, 0, 0, 1)"> rowHeight ) { Row row </span>= sheet.createRow(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.rowIndex); </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( rowHeight == 0<span style="color: rgba(0, 0, 0, 1)"> ) { rowHeight </span>= (<span style="color: rgba(0, 0, 255, 1)">short</span>)<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.rowHeight; } row.setHeight(rowHeight); </span><span style="color: rgba(0, 0, 255, 1)">this</span>.rowIndex ++<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> row; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 创建每一列的样式 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> fontName * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> fontSize * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> color * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> alignments </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> addColStlye(String fontName, <span style="color: rgba(0, 0, 255, 1)">short</span> fontSize,<span style="color: rgba(0, 0, 255, 1)">short</span> color,<span style="color: rgba(0, 0, 255, 1)">short</span><span style="color: rgba(0, 0, 0, 1)"> ...alignments) { Font ft </span>=<span style="color: rgba(0, 0, 0, 1)"> createFont(fontName, fontSize, color); CellStyle style </span>=<span style="color: rgba(0, 0, 0, 1)"> createCellStyle(alignments); style.setFont(ft); styleList.add(style); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 生成Excel表 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> list * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> SecurityException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> NoSuchMethodException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> InvocationTargetException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IllegalArgumentException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IllegalAccessException </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> createExcel(List<T> list,String parentPath) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> ( T temp : list ) { Row row </span>= createRow((<span style="color: rgba(0, 0, 255, 1)">short</span>)0<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < colList.size(); i ++<span style="color: rgba(0, 0, 0, 1)"> ) { Map</span><String,Object> cl =<span style="color: rgba(0, 0, 0, 1)"> colList.get(i); Cell cell </span>=<span style="color: rgba(0, 0, 0, 1)"> createCell(row,i,styleList.get(i)); String methodName </span>= cl.get("methodName"<span style="color: rgba(0, 0, 0, 1)">).toString(); Object obj </span>=<span style="color: rgba(0, 0, 0, 1)"> util.valueToGet(temp, methodName); cell.setCellValue(obj.toString()); } } File file </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> File(parentPath); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">file.exists()){ file.createNewFile(); } FileOutputStream outputStream </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FileOutputStream(file); workbook.write(outputStream); outputStream.close(); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> List<ExcelBean><span style="color: rgba(0, 0, 0, 1)"> getListBean(){ List</span><ExcelBean> list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); ExcelBean bean </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; Random random </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Random(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> ( <span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < 100; i++<span style="color: rgba(0, 0, 0, 1)"> ) { bean </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ExcelBean(); bean.setAddr(UUID.randomUUID().toString().substring(</span>0, 15<span style="color: rgba(0, 0, 0, 1)">)); bean.setAge(random.nextInt(</span>100<span style="color: rgba(0, 0, 0, 1)">)); bean.setName(</span>"张三"+(i + 1) +"号"<span style="color: rgba(0, 0, 0, 1)">); list.add(bean); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> list; } </span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ClassUtils{ </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> ClassUtils() {} </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 反射获取value * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> object * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> SecurityException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> NoSuchMethodException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> InvocationTargetException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IllegalArgumentException * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IllegalAccessException </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> Object valueToGet( Object object, String methodName ) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method method </span>=<span style="color: rgba(0, 0, 0, 1)"> object.getClass().getDeclaredMethod(methodName); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> method.invoke(object); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 首字母大写 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> fieldName * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String convertMethodName( String fieldName ) { String newField </span>= fieldName.substring(1<span style="color: rgba(0, 0, 0, 1)">, fieldName.length()); </span><span style="color: rgba(0, 0, 255, 1)">return</span> fieldName.substring(0,1).toUpperCase()+<span style="color: rgba(0, 0, 0, 1)">newField; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 通过注解获取导出sheet相关注解属性 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> parseBookAnnotation( ) { Annotation[] ans </span>= ExcelUtils.<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.obj.getClass().getAnnotations(); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> ( Annotation temp : ans ) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( temp <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> ExcelBookAnnotation) { String title </span>=<span style="color: rgba(0, 0, 0, 1)"> ((ExcelBookAnnotation) temp).title(); ExcelUtils.</span><span style="color: rgba(0, 0, 255, 1)">this</span>.title =<span style="color: rgba(0, 0, 0, 1)"> title; parseFielAnnotation( ); } } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 通过注解获取列相关注解属性 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> parseFielAnnotation( ) { Field [] fiels </span>= ExcelUtils.<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.obj.getClass().getDeclaredFields(); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> ( Field temp : fiels ) { Annotation[] ans </span>=<span style="color: rgba(0, 0, 0, 1)"> temp.getAnnotations(); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> ( Annotation tempAn : ans ) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( tempAn <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> ExcelColAnnotation ) { ExcelColAnnotation col </span>=<span style="color: rgba(0, 0, 0, 1)"> ((ExcelColAnnotation) tempAn); </span><span style="color: rgba(0, 0, 255, 1)">int</span> ignore =<span style="color: rgba(0, 0, 0, 1)"> col.ignore(); </span><span style="color: rgba(0, 0, 255, 1)">if</span> ( ignore == 0<span style="color: rgba(0, 0, 0, 1)"> ) { Map</span><String,Object> fieldMap = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<><span style="color: rgba(0, 0, 0, 1)">(); fieldMap.put(</span>"width"<span style="color: rgba(0, 0, 0, 1)">, col.colWidth()); fieldMap.put(</span>"color"<span style="color: rgba(0, 0, 0, 1)">, col.color()); fieldMap.put(</span>"text"<span style="color: rgba(0, 0, 0, 1)">, col.text()); fieldMap.put(</span>"methodName", "get"+<span style="color: rgba(0, 0, 0, 1)">convertMethodName(temp.getName())); ExcelUtils.</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.colList.add(fieldMap); } } } } } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException, ParseException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { ExcelUtils</span><ExcelBean> excel = <span style="color: rgba(0, 0, 255, 1)">new</span> ExcelUtils<ExcelBean>( <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ExcelBean()); List</span><ExcelBean> list =<span style="color: rgba(0, 0, 0, 1)"> excel.getListBean(); excel.createExcel(list, </span>"C:\\Users\\Administrator\\Desktop\\test_bak\\test.xlsx"<span style="color: rgba(0, 0, 0, 1)">); }
}
到这里就算完了,有不好的地方可以提出。