简单的导出Excel Java POI

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.List;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.ResourceUtils;
public class ExcelUtils {

public <T> String exportExcel(List<String> titles, List<T> datas)
		throws IllegalArgumentException, IllegalAccessException, IOException {
	// List<Map> data = JSON.parseArray(JSON.toJSONString(errors), Map.class);
	XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿对象
	XSSFSheet sheet = workbook.createSheet(); // 创建工作表

	// 产生表格标题行
	XSSFCellStyle nameCellStyle = getColumnTopStyle(workbook);
	XSSFCellStyle dataCellStyle = getStyle(workbook);
	// 定义所需列数
	XSSFRow rowName = sheet.createRow(0); // 在索引2的位置创建行(最顶端的行开始的第二行)

	// 插入标题列
	for (int i = 0; i < titles.size(); i++) {
		Cell cell = rowName.createCell(i);
		cell.setCellStyle(nameCellStyle);
		cell.setCellValue(titles.get(i));
	}

	// 插入数据列
	for (int i = 0; i < datas.size(); i++) {
		XSSFRow dataRow = sheet.createRow(i + 1);
		T t = datas.get(i);
		Class<? extends Object> class1 = t.getClass();
		Field[] fields = class1.getDeclaredFields();
		for (int j = 0; j < fields.length; j++) {
			Field field = fields[j];
			field.setAccessible(true);
			Cell cell = dataRow.createCell(j);
			cell.setCellStyle(dataCellStyle);
			cell.setCellValue(field.get(t).toString());
		}
	}

	// 让列宽随着导出的列长自动适应
	for (int colNum = 0; colNum <= datas.size(); colNum++) {
		int columnWidth = sheet.getColumnWidth(colNum) / 256;
		for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
			XSSFRow currentRow;
			// 当前行未被使用过
			if (sheet.getRow(rowNum) == null) {
				currentRow = sheet.createRow(rowNum);
			} else {
				currentRow = sheet.getRow(rowNum);
			}
			if (currentRow.getCell(colNum) != null) {
				XSSFCell currentCell = currentRow.getCell(colNum);
				if (currentCell.getCellType() == CellType.STRING.getCode()) {
					int length = currentCell.getStringCellValue().getBytes().length;
					if (columnWidth < length) {
						columnWidth = length;
					}
				}
			}
		}
		sheet.setColumnWidth(colNum, columnWidth * 256 * 2);
	}

	OutputStream out = null;
	String path = null;
	try {
		path = ResourceUtils.getURL("classpath:").getPath() + File.separator + "test.xlsx";
		out = new FileOutputStream(path);
		workbook.write(out);
		out.flush();
	} catch (IOException e) {
		throw e;
	} finally {
		if (out != null) {
			out.close();
		}
	}
	return path;
}

/*
 * 列头单元格样式
 */
public XSSFCellStyle getColumnTopStyle(XSSFWorkbook workbook) {

	// 设置字体
	XSSFFont font = workbook.createFont();
	// 设置字体大小
	font.setFontHeightInPoints((short) 25);
	// 字体加粗
	font.setBold(true);
	// 设置字体名字
	font.setFontName("Courier New");
	// 设置样式;
	XSSFCellStyle style = workbook.createCellStyle();
	// 设置底边框;
	style.setBorderBottom(BorderStyle.THIN);
	// 设置底边框颜色;
	style.setBottomBorderColor(HSSFColor.BLACK.index);
	// 设置左边框;
	style.setBorderLeft(BorderStyle.THIN);
	// 设置左边框颜色;
	style.setLeftBorderColor(HSSFColor.BLACK.index);
	// 设置右边框;
	style.setBorderRight(BorderStyle.THIN);
	// 设置右边框颜色;
	style.setRightBorderColor(HSSFColor.BLACK.index);
	// 设置顶边框;
	style.setBorderTop(BorderStyle.THIN);
	// 设置顶边框颜色;
	style.setTopBorderColor(HSSFColor.BLACK.index);
	// 在样式用应用设置的字体;
	style.setFont(font);
	// 设置自动换行;
	style.setWrapText(false);
	// 设置水平对齐的样式为居中对齐;
	style.setAlignment(HorizontalAlignment.CENTER);
	// 设置垂直对齐的样式为居中对齐;
	style.setVerticalAlignment(VerticalAlignment.CENTER);

	return style;

}

/*
 * 列数据信息单元格样式
 */
public XSSFCellStyle getStyle(XSSFWorkbook workbook) {
	// 设置字体
	XSSFFont font = workbook.createFont();
	// 设置字体大小
	font.setFontHeightInPoints((short) 20);
	// 字体加粗
	// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	// 设置字体名字
	// font.setFontName("微軟正黑體");
	// 设置样式;
	XSSFCellStyle style = workbook.createCellStyle();
	// 设置底边框;
	style.setBorderBottom(BorderStyle.THIN);
	// 设置底边框颜色;
	style.setBottomBorderColor(HSSFColor.BLACK.index);
	// 设置左边框;
	style.setBorderLeft(BorderStyle.THIN);
	// 设置左边框颜色;
	style.setLeftBorderColor(HSSFColor.BLACK.index);
	// 设置右边框;
	style.setBorderRight(BorderStyle.THIN);
	// 设置右边框颜色;
	style.setRightBorderColor(HSSFColor.BLACK.index);
	// 设置顶边框;
	style.setBorderTop(BorderStyle.THIN);
	// 设置顶边框颜色;
	style.setTopBorderColor(HSSFColor.BLACK.index);
	// 在样式用应用设置的字体;
	style.setFont(font);
	// 设置自动换行;
	style.setWrapText(false);
	// 设置水平对齐的样式为居中对齐;
	style.setAlignment(HorizontalAlignment.CENTER);
	// 设置垂直对齐的样式为居中对齐;
	style.setVerticalAlignment(VerticalAlignment.CENTER);
	return style;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值