以后应该会经常用到的一个utils
package cn.itcast.tmhkcUtils;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
/**
*
* @author tmhkc
* @version 1.0
* @date 2018年5月13日
*/
public class PoiUtils {
private Workbook workBook;
private String address;
// 创建对象的时候设置路径和文件名称 屏蔽无参构造
public PoiUtils(String address) {
this.address = address;
workBook = new HSSFWorkbook();
}
/**
*
* @param mergeCell 合并的单元格数量
* @param titleColumnData 列名数据
* @param ShowData 真实数据
* @param columnWidth 列宽
* @param title 题头
* @throws Exception
*/
public void mainMethod(Integer mergeCell, String[] titleColumnData, List ShowData, Integer columnWidth,
String title) throws Exception {
int rowNo = 0;
int colNo = 0;
int defaultColumnWidth = 30;
Row row = null;
Cell cell = null;
if (columnWidth != null) {
defaultColumnWidth = columnWidth;
}
if (mergeCell == null || titleColumnData == null || ShowData == null || title == null) {
throw new Exception("必要参数不能为空");
}
if (mergeCell <= 0) {
throw new Exception("第一个参数不能为负数");
}
// 创建表
Sheet sheet = workBook.createSheet();
sheet.setColumnWidth(0, defaultColumnWidth * 256); // 设置列宽
row = sheet.createRow(rowNo);
row.setHeightInPoints(40);
sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 6)); // 合并单元格,新对象,不会覆盖合并的那些单元格,只是遮住
rowNo++;
// 设定合并单元格的内容与样式
cell = row.createCell(0);
cell.setCellValue(title);
cell.setCellStyle(this.bigTilteStyle(workBook));
// 写标题
row = sheet.createRow(rowNo++);
row.setHeightInPoints(28); // 设置行高
// 加样式
for (int i = 0; i < titleColumnData.length; i++) {
cell = row.createCell(i);
cell.setCellValue(titleColumnData[i]);
cell.setCellStyle(this.titleStyle(workBook)); // 绑定样式
}
// 写数据
for (Object obj : ShowData) {
colNo = 0; // 初始化
row = sheet.createRow(rowNo++);
row.setHeightInPoints(21);
String[] filedNames = this.getFiledName(obj);
for (String string : filedNames) {
cell = row.createCell(colNo++);
if (this.getFieldValueByName(string, obj) ==null) {
continue;
}
cell.setCellValue(this.getFieldValueByName(string, obj).toString());
cell.setCellStyle(this.textStyle(workBook));
}
}
FileOutputStream fos = new FileOutputStream(this.address);
workBook.write(fos);
fos.flush();
fos.close();
}
// 大标题样式
private CellStyle bigTilteStyle(Workbook wb) {
// 创建一个单元格样式对象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); // 横向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 纵向居中
Font curFont = wb.createFont(); // 创建字体对象
curFont.setFontName("华文隶书"); // 设置字体
curFont.setFontHeightInPoints((short) 30); // 设置字体大小
curStyle.setFont(curFont); // 将字体对象绑定到样式对象上
return curStyle;
}
// 标题样式
private CellStyle titleStyle(Workbook wb) {
// 创建一个单元格样式对象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); // 横向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 纵向居中
Font curFont = wb.createFont(); // 创建字体对象
curFont.setFontName("微软雅黑"); // 设置字体
curFont.setFontHeightInPoints((short) 12); // 设置字体大小
curStyle.setFont(curFont); // 将字体对象绑定到样式对象上
// 画线
curStyle.setBorderTop(CellStyle.BORDER_THIN); // 细实线
curStyle.setBorderBottom(CellStyle.BORDER_THIN);
curStyle.setBorderLeft(CellStyle.BORDER_THIN);
curStyle.setBorderRight(CellStyle.BORDER_THIN);
return curStyle;
}
// 文本样式
private CellStyle textStyle(Workbook wb) {
CellStyle xStyle = wb.createCellStyle();
Font xFont = wb.createFont();
xStyle.setFont(xFont);
xStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 纵向居中
// 画线
xStyle.setBorderTop(CellStyle.BORDER_THIN); // 细实线
xStyle.setBorderBottom(CellStyle.BORDER_THIN);
xStyle.setBorderLeft(CellStyle.BORDER_THIN);
xStyle.setBorderRight(CellStyle.BORDER_THIN);
return xStyle;
}
/**
* 获取属性名数组
*/
private String[] getFiledName(Object o) {
Field[] fields = o.getClass().getDeclaredFields();
String[] fieldNames = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
System.out.println(fields[i].getType());
fieldNames[i] = fields[i].getName();
}
return fieldNames;
}
/*
* 根据属性名获取属性值
*/
private Object getFieldValueByName(String fieldName, Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(o, new Object[] {});
return value;
} catch (Exception e) {
return null;
}
}
}
只完成了最基本的功能