您的位置:首页 > 其它

POI实现excel导出功能

2018-03-28 16:37 369 查看
package com.utils;

import com.bean.User;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExcelExportUtil {

// 反射调用的前缀
public static final String prefix = "get";

// 测试姓氏
private static char[] subnames = {'王','杨','刘','陈','潘','张','汪'};

// 测试名
private static String[] name = {"伟","雄","琳","星","柳","智","泉"};

/**
*
* @param title 表格名
* @param header  标题头
* @param column  取值属性
* @param list   取值对象
* @param opStream 输出位置
*/
public static void exportExcel(String title, String[] header, String[] column, List<Object> lists, OutputStream opStream) throws Exception {

// 生成一个工作薄
XSSFWorkbook workbook = new XSSFWorkbook();
// 生成一个表格
XSSFSheet sheet = workbook.createSheet(title);
// 生成一个宽度为15的字节
sheet.setDefaultColumnWidth(15);

// 创建第一种样式
XSSFCellStyle style = workbook.createCellStyle();
// 设置样式
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);
// 生成一个字体
XSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);

// 创建第二种样式
XSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
XSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);

// 设置标题头
XSSFRow row = sheet.createRow(0);
for (int i = 0; i < header.length; i++) {
XSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
XSSFRichTextString textString = new XSSFRichTextString(header[i]);
cell.setCellValue(textString);
}
int copyRow = 1;
for (Object list : lists) {
Class<?> aClass = list.getClass();
row = sheet.createRow(copyRow);
copyRow ++;
for (int i = 0; i < column.length; i++) {
StringBuffer buffer = new StringBuffer();
String col = column[i];
XSSFCell cell = row.createCell(i);
char[] chars = col.toCharArray();
buffer.append(prefix) .append(charToUpperCase(chars[0]));
for (int z = 1; z < chars.length; z++) {
buffer.append(chars[z]);
}
Method method = aClass.getDeclaredMethod(buffer.toString(), new Class[]{});
Object result = method.invoke(list, new Object[]{});
cell.setCellValue(result.toString());
cell.setCellStyle(style2);
}
}
workbook.write(opStream);
}

/**
*  字符转大写
* @param ch
* @return
*/
private static char charToUpperCase(char ch) {
if (ch <= 122 && ch >= 97) {
ch -= 32;
}
return ch;
}

public static void main(String[] args) {

List users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId(i);
user.setUserName(confirmName());
users.add(user);
}
String[] column = {"id","userName"};
String title = "用户信息";
String[] headers = {"序号","姓名"};
try {
FileOutputStream outputStream = new FileOutputStream("E:\\workTest\\111.xlsx");
exportExcel(title,headers,column,users,outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}

private static String confirmName() {
StringBuilder builder = new StringBuilder();
int subnameMath = (int) (Math.random() * 6) + 1;
int nameMath = (int)(Math.random() * 6) + 1;
String userName = builder.append(subnames[subnameMath]).append(name[nameMath]).toString();
return userName;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poi