poi插件生成excel表格utils

以后应该会经常用到的一个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;
		}
	}
}

只完成了最基本的功能

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值