小白,做日志只是为了方便自己查看,能帮到别人当然更好,不喜勿喷。

  上代码

  依赖:

        <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> { private static final Log log = LogFactory.getLog(ExcelUtils.class); /**
* 行高
*/
private int rowHeight = 400;
/**
* 列宽
*/
private int colWidth = 8500;
/**
* 起始位置
*/
private int rowIndex = 0;
/**
* 默认标题
*/
private String title = "defaultExcel"; private Workbook workbook;
private Sheet sheet;
/**
* 公共列样式
*/
private CellStyle cellStyle;
/**
* 操作的实体类
*/
private T obj;
/**
* 列属性集合
*/
private List<Map<String,Object>> colList = new ArrayList<>();
/**
* 列样式集合
*/
private List<CellStyle> styleList = new ArrayList<>(); private ClassUtils util = new ClassUtils(); public ExcelUtils( T obj ) {
this.obj = obj;
initWorkbook();
} public ExcelUtils(T obj,int rowHeight, int colWidth, int rowIndex, String title) {
this(obj);
this.rowHeight = rowHeight;
this.colWidth = colWidth;
this.rowIndex = rowIndex;
this.title = title;
} /**
* 默认文档设置文档
*/
private void initWorkbook() {
if ( CommonUtils.isEmpty(this.obj) ) {
throw new CustomException("未指定导出实体类,无法进行导出操作");
}
util.parseBookAnnotation();
workbook = new XSSFWorkbook();
sheet = workbook.createSheet(this.title); // 创建工作页
sheet.setDefaultColumnWidth(colWidth); // 设置默认列宽
cellStyle = createCellStyle();
cellStyle.setFont(createFont(null, (short)0, (short)0));
titleSetting();
} /**
* 标题、列名相关设置
*/
private void titleSetting() {
Row topRow = createRow((short)600);
mergedRegion(0, 0, 0, this.colList.size()-1); // 合并标题行
Cell cell = createCell(topRow,0,cellStyle);
cell.setCellValue(this.title);
Row textRow = createRow((short)0);
for ( int i = 0; i < colList.size(); i++ ) {
Map<String,Object> fieldMap = colList.get(i);
sheet.setColumnWidth(i, Integer.parseInt(fieldMap.get("width").toString()));
Cell cell1 = createCell(textRow,i,cellStyle);
cell1.setCellValue(fieldMap.get("text").toString());
addColStlye(null, (short)0, Short.parseShort(fieldMap.get("color").toString()));
}
} /**
* 创建字体对象
* @param fontName
* 字体库名称
* @param fontSize
* 字体大小-传0默认14
* @param color
* 字体颜色参考{@link HSSFColor.BLACK.index}
* @return
*/
private Font createFont(String fontName, short fontSize,short color) {
Font font = this.workbook.createFont();
if ( CommonUtils.isEmpty(fontName) ) {
fontName = "宋体";
}
if ( fontSize == 0 ) {
fontSize = (short)14;
}
if ( color == 0 ) {
color = HSSFColor.BLACK.index;
}
font.setFontName("宋体"); //设置为宋体字
font.setFontHeightInPoints(fontSize); //设置字体大小
font.setColor(color);
return font;
} /**
* 创建列样式
* @param alignment
* @param vertical
* @return
*/
private CellStyle createCellStyle(short ...alignments) {
CellStyle style = this.workbook.createCellStyle();
short alignment = HSSFCellStyle.ALIGN_CENTER_SELECTION,vertical = HSSFCellStyle.VERTICAL_CENTER;
if ( alignments.length > 0 ) {
alignment = alignments[0];
if ( alignments.length > 1 ) {
vertical = alignments[1];
}
}
//水平居中
style.setAlignment(alignment);
//垂直居中
style.setVerticalAlignment(vertical);
return style;
} /**
* 合并行、列
* @param firstRow
* @param lastRow
* @param firstCol
* @param lastCol
*/
private void mergedRegion(int firstRow, int lastRow, int firstCol, int lastCol) {
CellRangeAddress region = new CellRangeAddress(firstRow,lastRow,firstCol,lastCol); // 合并行
sheet.addMergedRegion(region);
} /**
* 创建单元格
* @param row
* @param index
* @param style
* @return
*/
private Cell createCell ( Row row,int index,CellStyle style ) {
Cell cell = row.createCell(index);
if ( CommonUtils.isNotEmpty(style) ) {
cell.setCellStyle(style);
}
return cell;
} /**
* 创建行
* @return
*/
private Row createRow ( short rowHeight ) {
Row row = sheet.createRow(this.rowIndex); if ( rowHeight == 0 ) {
rowHeight = (short)this.rowHeight;
}
row.setHeight(rowHeight);
this.rowIndex ++;
return row;
} /**
* 创建每一列的样式
* @param fontName
* @param fontSize
* @param color
* @param alignments
*/
private void addColStlye(String fontName, short fontSize,short color,short ...alignments) {
Font ft = createFont(fontName, fontSize, color);
CellStyle style = createCellStyle(alignments);
style.setFont(ft);
styleList.add(style);
} /**
* 生成Excel表
* @param list
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public void createExcel(List<T> list,String parentPath) throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
for ( T temp : list ) {
Row row = createRow((short)0);
for (int i = 0; i < colList.size(); i ++ ) {
Map<String,Object> cl = colList.get(i);
Cell cell = createCell(row,i,styleList.get(i));
String methodName = cl.get("methodName").toString();
Object obj = util.valueToGet(temp, methodName);
cell.setCellValue(obj.toString());
}
}
File file = new File(parentPath);
if (!file.exists()){
file.createNewFile();
}
FileOutputStream outputStream = new FileOutputStream(file);
workbook.write(outputStream);
outputStream.close();
} public List<ExcelBean> getListBean(){
List<ExcelBean> list = new ArrayList<>();
ExcelBean bean = null;
Random random = new Random();
for ( int i = 0; i < 100; i++ ) {
bean = new ExcelBean();
bean.setAddr(UUID.randomUUID().toString().substring(0, 15));
bean.setAge(random.nextInt(100));
bean.setName("张三"+(i + 1) +"号");
list.add(bean);
}
return list;
} class ClassUtils{ private ClassUtils() {} /**
* 反射获取value
* @param object
* @return
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
private Object valueToGet( Object object, String methodName ) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method method = object.getClass().getDeclaredMethod(methodName);
return method.invoke(object);
} /**
* 首字母大写
* @param fieldName
* @return
*/
public String convertMethodName( String fieldName ) {
String newField = fieldName.substring(1, fieldName.length());
return fieldName.substring(0,1).toUpperCase()+newField;
} /**
* 通过注解获取导出sheet相关注解属性
*/
public void parseBookAnnotation( ) {
Annotation[] ans = ExcelUtils.this.obj.getClass().getAnnotations();
for ( Annotation temp : ans ) {
if ( temp instanceof ExcelBookAnnotation) {
String title = ((ExcelBookAnnotation) temp).title();
ExcelUtils.this.title = title;
parseFielAnnotation( );
}
}
} /**
* 通过注解获取列相关注解属性
*/
public void parseFielAnnotation( ) {
Field [] fiels = ExcelUtils.this.obj.getClass().getDeclaredFields();
for ( Field temp : fiels ) {
Annotation[] ans = temp.getAnnotations();
for ( Annotation tempAn : ans ) {
if ( tempAn instanceof ExcelColAnnotation ) {
ExcelColAnnotation col = ((ExcelColAnnotation) tempAn);
int ignore = col.ignore();
if ( ignore == 0 ) {
Map<String,Object> fieldMap = new HashMap<>();
fieldMap.put("width", col.colWidth());
fieldMap.put("color", col.color());
fieldMap.put("text", col.text());
fieldMap.put("methodName", "get"+convertMethodName(temp.getName()));
ExcelUtils.this.colList.add(fieldMap);
}
}
}
}
} } public static void main(String[] args) throws IOException, ParseException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
ExcelUtils<ExcelBean> excel = new ExcelUtils<ExcelBean>( new ExcelBean());
List<ExcelBean> list = excel.getListBean();
excel.createExcel(list, "C:\\Users\\Administrator\\Desktop\\test_bak\\test.xlsx");
} }

到这里就算完了,有不好的地方可以提出。

java excel导出(基于注解)的更多相关文章

  1. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  2. abp框架Excel导出——基于vue

    abp框架Excel导出--基于vue 目录 abp框架Excel导出--基于vue 1.技术栈 1.1 前端采用vue,官方提供 1.2 后台是abp--aspnetboilerplate 2. E ...

  3. 自己写的java excel导出工具类

    最近项目要用到excel导出功能,之前也写过类似的代码.因为这次项目中多次用到excel导出.这次长了记性整理了一下 分享给大伙 欢迎一起讨论 生成excel的主工具类: public class E ...

  4. java excel导出

    下面是jsp代码: <li class="btns"><input id="btnExport" class="btn btn-pr ...

  5. java excel导出(表头合并,多行表头)

    @RequestMapping(value="orderExcelList2") public void orderExcelList2forJava(Order order,Ht ...

  6. Java编程思想-基于注解的单元测试

    Junit的测试方法命名不一定以test开头 上面介绍的atunit已经很老了,现在junit测试框架已经基本注解了

  7. java-excel导出

    java excel导出分为两种2003年的格式和2007年的格式. 2003年的xls一个sheet限制65536. 2007年的xlsx限制为1048576. jxl导入2003 gradle j ...

  8. 基于ABP和Magicodes实现Excel导出操作

      前端使用的vue-element-admin框架,后端使用ABP框架,Excel导出使用的Magicodes.IE.Excel.Abp库.Excel导入和导出操作几乎一样,不再介绍.文本主要介绍E ...

  9. Java基于注解和反射导入导出Excel

    代码地址如下:http://www.demodashi.com/demo/11995.html 1. 构建项目 使用Spring Boot快速构建一个Web工程,并导入与操作Excel相关的POI包以 ...

随机推荐

  1. centos新增或删除用户

    新增用户snzigod:adduser snzigod 修改snzigod密码:passwd snzigod 删除用户snzigod:userdel snzigod 删除用户以及用户目录: userd ...

  2. BZOJ 5313: 新Fib数列

    打表找规律 #include<cstdio> using namespace std; int F[20]={0,1,1,2,3,0,3,3,1,4,0,4,4,3,2,0,2,2,4,1 ...

  3. dedecms 搬家流程

    进入后台 系统 点击数据备份/还原根据新空间数据库版本格式备份 进入网站根目录备份文件夹data\backupdataimagestempletsuploadsplus 进入新空间 重新安装dede程 ...

  4. poj2449 Remmarguts' Date K短路 A*

    K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...

  5. FCKeditor自定义编辑区CSS样式

    在网站后台使用FCKeditor编辑器的时候,见到的效果可能并不完全是”所见即所得”的,因为如果在FCKeditor编辑区中使用了前台样式表中的样式,在编辑区中并不能把这些样式显示出来.解决这个问题的 ...

  6. 记一次WMS的系统改造(2)-敲定方案

    既定改造方案 基于上一篇分析出的种种问题,我们将库房人员的系统操作划分为两大类. 第一类为货物驱动的操作,这类操作主要随着货物而前进,人员不看或者看软件的次数比较少,更多是对货物的状态进行系统上的确认 ...

  7. 元类相关(type & metaclass)

    metaclass作用: 1) 拦截类的创建 2) 修改类 3) 返回修改之后的类 """为什么要用metaclass类而不是函数? 由于__metaclass__可以接 ...

  8. Django--------问题:在terminal命令行创建超级用户时入到password时输入为什么没有反应?

    首先如果遇到这样的问题不用担心,一般一会儿就可以解决: 其实,输入的时候并不是没有反应,只是你输入的时候命令行没有将你的输入显示出来,关键是输入行对Password:********也不是采用这种方式 ...

  9. 【JavaScript】关于 eval()执行JavaScript语句的一次实验测试

    实验主题: eval() 函数可以计算某个字符串,并执行其中的 JavaScript 代码.该函数只接受原始字符串作为参数,如果 string 不是原始字符串,那么该方法将不作任何的改变的返回.因此请 ...

  10. 聊聊、Nginx 初始化错误信息

    这篇文章我们继续学习 main 方法,我们先来看看 ngx_debug_init() 这个方法. 从方法名我们也知道,debug初始化.我们先看看方法位置在哪.我们来断点在这个方法上面. Functi ...