poi导出excel之java

pom文件:java

<!-- excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>git

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>github

工具类:apache

package com.vai.aio.frame.utils;app

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFFont;
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;工具

import com.github.pagehelper.util.StringUtil;
/**
* 做者: 豆豆<br>
* 时间: 2018年6月6日 下午3:01:12<br>
* 描述 :<br>
*/
public class ExcelUtil {
/**
* <br/>
* 做者:豆豆<br/>
* 时间:2018年6月6日下午4:07:05<br/>
* 描述:<br/>
* @param response
* @param expList:待导出的list列表
* @param fileName:文件名/表格标题
* @param heads:表头
* @param fields:类的字段名,顺序跟heads与之对应
* @throws Exception void<br/>
* --------------------其余说明--------------------<br/>

*/
public static void doExport(HttpServletResponse response, List<?> expList,
String fileName, String[] heads,String[] fields) throws Exception {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
int rowNo = 0; //行号
int colNo = 0; //列号
Row nRow = null;
Cell nCell = null;
for(int t=0;t<heads.length;t++){
if(t==0){
sheet.setColumnWidth(t, 10*256);
}else{
sheet.setColumnWidth(t, 20*256);
}

}
if(!StringUtil.isEmpty(fileName)){
nRow = sheet.createRow(rowNo);
nRow.setHeightInPoints(40);
sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, heads.length-1)); //合并单元格,新对象,不会覆盖合并的那些单元格,只是遮住
rowNo++;
nCell = nRow.createCell(0);
nCell.setCellValue(fileName);
nCell.setCellStyle(bigTilteStyle(wb));
}
//写表头
nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(28); //设置行高
for(int i=0;i<heads.length;i++){
nCell = nRow.createCell(i);
nCell.setCellValue(heads[i]);
nCell.setCellStyle(headStyle(wb)); //绑定样式
}

for(int j=0;j<expList.size();j++){
colNo = 0; //初始化
Object obj = expList.get(j); //获取到每条记录

nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(21);

nCell = nRow.createCell(colNo++);
nCell.setCellValue(j+1);
nCell.setCellStyle(textStyle(wb));

for(int k=0;k<fields.length;k++){
Field field=obj.getClass().getDeclaredField(fields[k]);
if(field==null){
new RuntimeException("字段名映射错误");
}
field.setAccessible(true);
nCell = nRow.createCell(colNo++);
nCell.setCellValue(field.get(obj)==null?"":field.get(obj).toString());
nCell.setCellStyle(textStyle(wb));
}
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //生成流对象
wb.write(byteArrayOutputStream);
download(byteArrayOutputStream, response, StringUtil.isEmpty(fileName)?"noName.xls":fileName+".xls"); //弹出下载框,用户就能够直接下载
}字体

/**
* @param byteArrayOutputStream 将文件内容写入ByteArrayOutputStream
* @param response HttpServletResponse 写入response
* @param returnName 返回的文件名
*/
public static void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName)
throws IOException {
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(), "iso8859-1")); // 保存的文件名,必须和页面编码一致,不然乱码
response.addHeader("Content-Disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size());编码

ServletOutputStream outputstream = response.getOutputStream(); // 取得输出流
byteArrayOutputStream.writeTo(outputstream); // 写到输出流
byteArrayOutputStream.close(); // 关闭
outputstream.flush(); // 刷数据
}excel

// 大标题样式
public static CellStyle bigTilteStyle(Workbook wb) {
// 建立一个单元格样式对象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); // 横向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 纵向居中code

Font curFont = wb.createFont(); // 建立字体对象
curFont.setFontName("华文隶书"); // 设置字体
curFont.setFontHeightInPoints((short) 20); // 设置字体大小

curStyle.setFont(curFont); // 将字体对象绑定到样式对象上

return curStyle;
}

// 表头样式
public static CellStyle headStyle(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) 10); // 设置字体大小
curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
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;
}

// 文本样式
public static 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;
}
}

调用工具类:

List<ProductTypeVo> ls = mapper.queryProductType(productTypeVo); String fileName="设备类型设置表"; String[]title=new String[]{"编号","类型名称","计量单位","类型描述","建立时间","备注"}; String [] field=new String []{"typeName","unitname","typeDesc","createTime","remark"}; ExcelUtil.doExport(response, ls, fileName, title, field);

相关文章
相关标签/搜索