使用poi技术对excel进行解析和下载的一个公共使用方法(使用注解的方式配置即可):
java类:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
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.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/*
* ExcelUtil工具类实现功能:
* 导出时传入list<T>,即可实现导出为一个excel,其中每个对象T为Excel中的一条记录.
* 导入时读取excel,得到的结果是一个list<T>.T是自己定义的对象.
* 需要导出的实体对象只需简单配置注解就能实现灵活导出,通过注解您可以方便实现下面功能:
* 1.实体属性配置了注解就能导出到excel中,每个属性都对应一列.
* 2.列名称可以通过注解配置.
* 3.导出到哪一列可以通过注解配置.
* 4.鼠标移动到该列时提示信息可以通过注解配置.
* 5.用注解设置只能下拉选择不能随意填写功能.
* 6.用注解设置是否只导出标题而不导出内容,这在导出内容作为模板以供用户填写时比较实用.
*
*/
public class ExcelUtil<T> {
Class<T> clazz;
public ExcelUtil(Class<T> clazz) {
this.clazz = clazz;
}
/**
* excel解析成list集合
* @param sheetName
* @param input
* @return
*/
public List<T> importExcel(String sheetName, InputStream input) {
List<T> list = new ArrayList<T>();
try {
Workbook book = createExcel(input);
Sheet sheet = null;
if (sheetName != null && !sheetName.trim().equals("")) {
sheet = book.getSheet(sheetName);// 如果指定sheet名,则取指定sheet中的内容.
}
if (sheet == null) {
sheet = book.getSheetAt(0);// 如果传入的sheet名不存在则默认指向第1个sheet.
}
int rows = sheet.getPhysicalNumberOfRows();// 得到数据的行数
if (rows > 0) {// 有数据时才处理
Field[] allFields = clazz.getDeclaredFields();// 得到类的所有field.
Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();// 定义一个map用于存放列的序号和field.
for (Field field : allFields) {
// 将有注解的field存放到map中.
if (field.isAnnotationPresent(ExcelVOAttribute.class)) {
ExcelVOAttribute attr = field.getAnnotation(ExcelVOAttribute.class);
int col = getExcelCol(attr.column());// 获得列号
field.setAccessible(true);// 设置类的私有字段属性可访问.
fieldsMap.put(col, field);
}
}
for (int i = 1; i < rows; i++) {// 从第2行开始取数据,默认第一行是表头.
Row cells = sheet.getRow(i);// 得到一行中的所有单元格对象.
T entity = null;
for (int j = 0; j < cells.getPhysicalNumberOfCells(); j++) {
String c = getCellValue(cells.getCell(j));// 单元格中的内容.
if (c.equals("")) {
continue;
}
entity = (entity == null ? clazz.newInstance() : entity);// 如果不存在实例则新建.
Field field = fieldsMap.get(j);// 从map中得到对应列的field.
//配置的格式与excel相同的时候才读取
if(field != null){
// 取得类型,并根据对象类型设置值.
Class<?> fieldType = field.getType();
if ((Integer.TYPE == fieldType) || (Integer.class == fieldType)) {
field.set(entity, Integer.parseInt(c));
} else if (String.class == fieldType) {
field.set(entity, String.valueOf(c));
} else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) {
field.set(entity, Long.valueOf(c));
} else if ((Float.TYPE == fieldType) || (Float.class == fieldType)) {
field.set(entity, Float.valueOf(c));
} else if ((Short.TYPE == fieldType) || (Short.class == fieldType)) {
field.set(entity, Short.valueOf(c));
} else if ((Double.TYPE == fieldType) || (Double.class == fieldType)) {
field.set(entity, Double.valueOf(c));
} else if (Character.TYPE == fieldType) {
if ((c != null) && (c.length() > 0)) {
field.set(entity, Character.valueOf(c.charAt(0)));
}
}
}else{
//如果配置文件不对,则抛出异常
throw new NullPointerException();
}
}
if (entity != null) {
list.add(entity);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
return list;
}
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @param sheetName
* 工作表的名称
* @param sheetSize
* 每个sheet中数据的行数,此数值必须小于65536
* @param output
* java输出流
*/
public boolean exportExcel(List<T> list, String sheetName, int sheetSize,
OutputStream output) {
Field[] allFields = clazz.getDeclaredFields();// 得到所有定义字段
List<Field> fields = new ArrayList<Field>();
// 得到所有field并存放到一个list中.
for (Field field : allFields) {
if (field.isAnnotationPresent(ExcelVOAttribute.class)) {
fields.add(field);
}
}
HSSFWorkbook workbook = new HSSFWorkbook();// 产生工作薄对象
HSSFCellStyle style = getStyle(workbook);
HSSFCellStyle rowStyle = getRowStyle(workbook);
// excel2003中每个sheet中最多有65536行,为避免产生错误所以加这个逻辑.
if (sheetSize > 65536 || sheetSize < 1) {
sheetSize = 65536;
}
double sheetNo = Math.ceil(list.size() / sheetSize);// 取出一共有多少个sheet.
for (int index = 0; index <= sheetNo; index++) {
HSSFSheet sheet = workbook.createSheet();// 产生工作表对象
workbook.setSheetName(index, sheetName + index);// 设置工作表的名称.
HSSFRow row;
HSSFCell cell;// 产生单元格
row = sheet.createRow(0);// 产生一行
// 写入各个字段的列头名称
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
ExcelVOAttribute attr = field.getAnnotation(ExcelVOAttribute.class);
int col = getExcelCol(attr.column());// 获得列号
cell = row.createCell(col);// 创建列
cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 设置列中写入内容为String类型
//设置标题的样式
cell.setCellStyle(style);
cell.setCellValue(attr.name());// 写入列名
// 如果设置了提示信息则鼠标放上去提示.
if (!attr.prompt().trim().equals("")) {
setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, col, col);// 这里默认设了2-101列提示.
}
// 如果设置了combo属性则本列只能选择不能输入
if (attr.combo().length > 0) {
setHSSFValidation(sheet, attr.combo(), 1, 100, col, col);// 这里默认设了2-101列只能选择不能输入.
}
}
int startNo = index * sheetSize;
int endNo = Math.min(startNo + sheetSize, list.size());
// 写入各条记录,每条记录对应excel表中的一行
for (int i = startNo; i < endNo; i++) {
row = sheet.createRow(i + 1 - startNo);
T vo = (T) list.get(i); // 得到导出对象.
for (int j = 0; j < fields.size(); j++) {
sheet.setColumnWidth(j,10 * 512);
Field field = fields.get(j);// 获得field.
field.setAccessible(true);// 设置实体类私有属性可访问
ExcelVOAttribute attr = field.getAnnotation(ExcelVOAttribute.class);
try {
// 根据ExcelVOAttribute中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
if (attr.isExport()) {
cell = row.createCell(getExcelCol(attr.column()));// 创建cell
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//设值数据的样式
cell.setCellStyle(rowStyle);
cell.setCellValue(field.get(vo) == null ? ""
: String.valueOf(field.get(vo)));// 如果数据存在就填入,不存在填入空格.
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
try {
output.flush();
workbook.write(output);
output.close();
return true;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Output is closed ");
return false;
}
}
/**
* 将EXCEL中A,B,C,D,E列映射成0,1,2,3
*
* @param col
*/
public static int getExcelCol(String col) {
col = col.toUpperCase();
// 从-1开始计算,字母重1开始运算。这种总数下来算数正好相同。
int count = -1;
char[] cs = col.toCharArray();
for (int i = 0; i < cs.length; i++) {
count += (cs[i] - 64) * Math.pow(26, cs.length - 1 - i);
}
return count;
}
/**
* 设置单元格上提示
*
* @param sheet
* 要设置的sheet.
* @param promptTitle
* 标题
* @param promptContent
* 内容
* @param firstRow
* 开始行
* @param endRow
* 结束行
* @param firstCol
* 开始列
* @param endCol
* 结束列
* @return 设置好的sheet.
*/
public static HSSFSheet setHSSFPrompt(HSSFSheet sheet, String promptTitle,
String promptContent, int firstRow, int endRow, int firstCol,
int endCol) {
// 构造constraint对象
DVConstraint constraint = DVConstraint
.createCustomFormulaConstraint("DD1");
// 四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList(firstRow,
endRow, firstCol, endCol);
// 数据有效性对象
HSSFDataValidation data_validation_view = new HSSFDataValidation(
regions, constraint);
data_validation_view.createPromptBox(promptTitle, promptContent);
sheet.addValidationData(data_validation_view);
return sheet;
}
/**
* 设置某些列的值只能输入预制的数据,显示下拉框.
*
* @param sheet
* 要设置的sheet.
* @param textlist
* 下拉框显示的内容
* @param firstRow
* 开始行
* @param endRow
* 结束行
* @param firstCol
* 开始列
* @param endCol
* 结束列
* @return 设置好的sheet.
*/
public static HSSFSheet setHSSFValidation(HSSFSheet sheet,
String[] textlist, int firstRow, int endRow, int firstCol,
int endCol) {
// 加载下拉列表内容
DVConstraint constraint = DVConstraint.createExplicitListConstraint(textlist);
// 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList(firstRow,endRow, firstCol, endCol);
// 数据有效性对象
HSSFDataValidation data_validation_list = new HSSFDataValidation(regions, constraint);
sheet.addValidationData(data_validation_list);
return sheet;
}
/**
*
* @param inp
* @return
* @throws IOException
* @throws InvalidFormatException
*/
public Workbook createExcel(InputStream inp) throws IOException,InvalidFormatException {
if (!inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
//xls格式
if (POIFSFileSystem.hasPOIFSHeader(inp)) {
return new HSSFWorkbook(inp);
}
//xlsx格式
if (POIXMLDocument.hasOOXMLHeader(inp)) {
return new XSSFWorkbook(OPCPackage.open(inp));
}
throw new IllegalArgumentException("你的excel版本目前poi解析不了");
}
/**
* 根据类型获取单元格内容
* @param cell
* @return
*/
public static String getCellValue(Cell cell){
String cellValue = "";
if(cell == null){
return cellValue;
}
//把数字当成String来读,避免出现1读成1.0的情况
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
cell.setCellType(Cell.CELL_TYPE_STRING);
}
//判断数据的类型
switch (cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC: //数字
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING: //字符串
cellValue = String.valueOf(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN: //Boolean
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK: //空值
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR: //故障
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
return cellValue;
}
//设置单元格标题样式
public HSSFCellStyle getStyle(HSSFWorkbook wb){
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
return style;
}
//设置表单的样式
public HSSFCellStyle getRowStyle(HSSFWorkbook wb){
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
style.setFillForegroundColor(HSSFColor.WHITE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}}
excel配置注解类:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target( { java.lang.annotation.ElementType.FIELD })
public @interface ExcelVOAttribute {
/**
* 导出到Excel中的名字.
*/
public abstract String name();
/**
* 配置列的名称,对应A,B,C,D....
*/
public abstract String column();
/**
* 提示信息(与combo不能同时使用)
*/
public abstract String prompt() default "";
/**
* 设置只能选择不能输入的列内容.(下拉框类型)
*/
public abstract String[] combo() default {};
/**
* 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.(设置为true就是到处数据,为false就是不导出数据)
*/
public abstract boolean isExport() default true;
}
创建好以上两个java类,下面是使用方法:
新建一个实体类,字段与excel中的字段相同,类型全部为String类型:
package com.tgb.lk.test04;
import com.tgb.lk.util.ExcelVOAttribute;
//新建的实体类:根据需求配置注解
public class StudentVO {
@ExcelVOAttribute(name = "姓名", column = "B", isExport = true, prompt = "姓名为必填项哦!")
private String name;
@ExcelVOAttribute(name = "性别", column = "C", combo = { "男", "女" })
private String sex;
@ExcelVOAttribute(name = "班级", column = "D", combo = { "五期提高班", "六期提高班",
"七期提高班" })
private String clazz;
@ExcelVOAttribute(name="生日",column="E")
private String birthday;
@ExcelVOAttribute(name = "公司", column = "F")
private String company;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "StudentVO [birthday=" + birthday + ", clazz=" + clazz
+ ", company=" + company + ", name=" + name + ", sex=" + sex
+ "]";
}}
java中调用方法解析excel:
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("d:\\ftptest.xlsx");
ExcelUtil<StudentVO> util = new ExcelUtil<StudentVO>(
StudentVO.class);
List<StudentVO> list = util.importExcel(null, fis);//拿到list后根据自己的需求对list中的数据进行处理
System.out.println(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
}}
//excel导出时弹出下载框的方法:
//获取输出流
public void getOut(HSSFWorkbook workbook,String fileName){
OutputStream out = null;
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(out != null){
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
弹框下载文件的方法:
/**
* 弹出下载框下载文件
* @param file
*/
@SuppressWarnings("unused")
public void exportXml(File file){
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
if(file.exists()){
is = new FileInputStream(file);
os = response.getOutputStream();
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(os);
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1){
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.flush();
bis.close();
bos.close();
is.close();
os.close();
//下载完成后删除文件
Boolean falg = file.delete();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bos.flush();
bis.close();
bos.close();
is.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
全部代码已经上传至百度云盘java项目中