/**
* 方法描述 : apache poi导出excel,不需要模板,导出的为数据库中的数据
* @param headers 需要导出的excel的头
* @param dataList 需要导出的数据集合
* @param response ServletResponse响应
* @return void
* @throws Exception String
*/
public static void exportExcel(String[] headers,List<?> dataList, HttpServletResponse response){
//声明一个工作薄
HSSFWorkbook hssfWorkBook = new HSSFWorkbook();
//生成一个表格 参数为表格的标题
HSSFSheet hssfSheet = hssfWorkBook.createSheet("测试导出apache POI导出Excel");
//设置表格默认列宽为15个字节
hssfSheet.setDefaultColumnWidth((int)15);
//生成一个样式
HSSFCellStyle cellStyle = hssfWorkBook.createCellStyle();
//设置这些样式
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //设置单元格填充样式,SOLID_FOREGROUND纯色使用前景颜色填充
//接着设置前景颜色(setFillForegroundColor)就可以给单元格着色了。setFillForegroundColor()方法的参数是一个short类型,POI使用索引来代表颜色,默认已经有一些颜色了
cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THICK); //下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THICK); //左边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THICK); //右边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THICK); //上边框
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //对齐方式
//生成一个字体
HSSFFont font = hssfWorkBook.createFont();
//设置这个字体
font.setColor(HSSFColor.BLUE_GREY.index); //字体颜色
font.setFontHeightInPoints((short)13); //字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
//将字体用于当前的样式
cellStyle.setFont(font);
//声明一个画图的顶级管理器
HSSFPatriarch patriarch = hssfSheet.createDrawingPatriarch();
//定义注释的大小和位置
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short)6, 5));
//设置注释内容
comment.setString(new HSSFRichTextString("可以加注释"));
//设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到这个内容的
comment.setAuthor("haiyan");
//产生表格标题行
HSSFRow row = hssfSheet.createRow(0);
for(int i=0;i<headers.length;i++){
HSSFCell cell = row.createCell(i);
//此处为列设置样式
cell.setCellStyle(cellStyle);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//遍历数据集合,产生数据行
@SuppressWarnings("rawtypes")
Iterator it = dataList.iterator();
int index = 0;
while(it.hasNext()){
index++;
//创建数据行
row = hssfSheet.createRow(index);
Object obj = it.next();
//利用反射机制,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
@SuppressWarnings("rawtypes")
Class cls = obj.getClass();
Field[] fields = cls.getDeclaredFields();
for(int k=0;k<fields.length;k++){
/*生成列本应放在for循环中的首行,但是自己写的测试并不包括类中的所有属性,所以放到了textValue != null语句中,
目的是生成的excel字段和属性对应上
*/
HSSFCell cell = row.createCell(k);
//此处为列设置样式
cell.setCellStyle(cellStyle);
Field field = fields[k];
String fieldName = field.getName();
//将属性的首字母变为大写
String toUpperFirst = fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
String getMethodName = "get"+toUpperFirst;
try {
//通过methodName和参数的argsClass(方法中的参数类型集合)数组得到要执行的Method。
Method getMethod = obj.getClass().getMethod(getMethodName, new Class[] {});
//Method[] methods =obj.getClass().getMethods();
/*执行该Method.invoke方法的参数是执行这个方法的对象obj,和参数数组args,
可以这么理解:obj对象中带有参数args的method方法。返回值是Object,也既是该方法的返回值。*/
Object value = getMethod.invoke(obj, new Object[] {});
//判断值的类型后进行强制类型转换
String textValue = null;
//判断value是否为空是因为测试时只用实体类的部分字段,没用到的字段会报空指针异常
if(value != null){
if(value instanceof Date){
Date d = (Date)value;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
textValue = sdf.format(d);
}else{
//其他的都当字符串处理
textValue = value.toString();
}
}
//利用正则表达式判断textValue是否全部由数字组成
if(textValue != null){
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher = p.matcher(textValue);
if(matcher.matches()){
//是数字当double处理
cell.setCellValue(Double.parseDouble(textValue));
}else{
HSSFRichTextString textStr = new HSSFRichTextString(textValue);
cell.setCellValue(textStr);
}
}else{
row.removeCell(cell);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
//下载时默认的名字
String name = "test.xls";
ServletOutputStream os = response.getOutputStream();
//让浏览器知道要保存为什么文件而已,这是输出为EXCEL格式的
response.setContentType("application/vnd.ms-excel");
//要实现文件下载,消息头的设置
response.setHeader("Content-Disposition","attachment;filename=\"" + name + "\"");
hssfWorkBook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
}
* 方法描述 : apache poi导出excel,不需要模板,导出的为数据库中的数据
* @param headers 需要导出的excel的头
* @param dataList 需要导出的数据集合
* @param response ServletResponse响应
* @return void
* @throws Exception String
*/
public static void exportExcel(String[] headers,List<?> dataList, HttpServletResponse response){
//声明一个工作薄
HSSFWorkbook hssfWorkBook = new HSSFWorkbook();
//生成一个表格 参数为表格的标题
HSSFSheet hssfSheet = hssfWorkBook.createSheet("测试导出apache POI导出Excel");
//设置表格默认列宽为15个字节
hssfSheet.setDefaultColumnWidth((int)15);
//生成一个样式
HSSFCellStyle cellStyle = hssfWorkBook.createCellStyle();
//设置这些样式
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //设置单元格填充样式,SOLID_FOREGROUND纯色使用前景颜色填充
//接着设置前景颜色(setFillForegroundColor)就可以给单元格着色了。setFillForegroundColor()方法的参数是一个short类型,POI使用索引来代表颜色,默认已经有一些颜色了
cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THICK); //下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THICK); //左边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THICK); //右边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THICK); //上边框
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //对齐方式
//生成一个字体
HSSFFont font = hssfWorkBook.createFont();
//设置这个字体
font.setColor(HSSFColor.BLUE_GREY.index); //字体颜色
font.setFontHeightInPoints((short)13); //字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
//将字体用于当前的样式
cellStyle.setFont(font);
//声明一个画图的顶级管理器
HSSFPatriarch patriarch = hssfSheet.createDrawingPatriarch();
//定义注释的大小和位置
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short)6, 5));
//设置注释内容
comment.setString(new HSSFRichTextString("可以加注释"));
//设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到这个内容的
comment.setAuthor("haiyan");
//产生表格标题行
HSSFRow row = hssfSheet.createRow(0);
for(int i=0;i<headers.length;i++){
HSSFCell cell = row.createCell(i);
//此处为列设置样式
cell.setCellStyle(cellStyle);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//遍历数据集合,产生数据行
@SuppressWarnings("rawtypes")
Iterator it = dataList.iterator();
int index = 0;
while(it.hasNext()){
index++;
//创建数据行
row = hssfSheet.createRow(index);
Object obj = it.next();
//利用反射机制,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
@SuppressWarnings("rawtypes")
Class cls = obj.getClass();
Field[] fields = cls.getDeclaredFields();
for(int k=0;k<fields.length;k++){
/*生成列本应放在for循环中的首行,但是自己写的测试并不包括类中的所有属性,所以放到了textValue != null语句中,
目的是生成的excel字段和属性对应上
*/
HSSFCell cell = row.createCell(k);
//此处为列设置样式
cell.setCellStyle(cellStyle);
Field field = fields[k];
String fieldName = field.getName();
//将属性的首字母变为大写
String toUpperFirst = fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
String getMethodName = "get"+toUpperFirst;
try {
//通过methodName和参数的argsClass(方法中的参数类型集合)数组得到要执行的Method。
Method getMethod = obj.getClass().getMethod(getMethodName, new Class[] {});
//Method[] methods =obj.getClass().getMethods();
/*执行该Method.invoke方法的参数是执行这个方法的对象obj,和参数数组args,
可以这么理解:obj对象中带有参数args的method方法。返回值是Object,也既是该方法的返回值。*/
Object value = getMethod.invoke(obj, new Object[] {});
//判断值的类型后进行强制类型转换
String textValue = null;
//判断value是否为空是因为测试时只用实体类的部分字段,没用到的字段会报空指针异常
if(value != null){
if(value instanceof Date){
Date d = (Date)value;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
textValue = sdf.format(d);
}else{
//其他的都当字符串处理
textValue = value.toString();
}
}
//利用正则表达式判断textValue是否全部由数字组成
if(textValue != null){
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher = p.matcher(textValue);
if(matcher.matches()){
//是数字当double处理
cell.setCellValue(Double.parseDouble(textValue));
}else{
HSSFRichTextString textStr = new HSSFRichTextString(textValue);
cell.setCellValue(textStr);
}
}else{
row.removeCell(cell);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
//下载时默认的名字
String name = "test.xls";
ServletOutputStream os = response.getOutputStream();
//让浏览器知道要保存为什么文件而已,这是输出为EXCEL格式的
response.setContentType("application/vnd.ms-excel");
//要实现文件下载,消息头的设置
response.setHeader("Content-Disposition","attachment;filename=\"" + name + "\"");
hssfWorkBook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
}