grid导出
作者:互联网
//接口service
void
exportExcel(ApdTaskBill taskBill, IRequest request, HttpServletResponse response);
//接口实现类serviceImpI
@Override
public
void
exportExcel(ApdTaskBill taskBill,IRequest requestContext, HttpServletResponse response) {
//表头信息
FormTitleDto titleDto0 =
new
FormTitleDto(
"序号"
,
"sequence"
,
"0,0,0,0"
);
FormTitleDto titleDto1 =
new
FormTitleDto(
"仿真任务单号"
,
"simulationTaskCode"
,
"0,0,1,1"
);
FormTitleDto titleDto2 =
new
FormTitleDto(
"优先级"
,
"priority"
,
"0,0,2,2"
);
FormTitleDto titleDto3 =
new
FormTitleDto(
"项目代码"
,
"projectCode"
,
"0,0,3,3"
);
FormTitleDto titleDto4 =
new
FormTitleDto(
"项目名称"
,
"projectName"
,
"0,0,4,4"
);
FormTitleDto titleDto5 =
new
FormTitleDto(
"任务单状态"
,
"simulationTaskStatus"
,
"0,0,5,5"
);
FormTitleDto titleDto6 =
new
FormTitleDto(
"变更状态"
,
"changeStatus"
,
"0,0,6,6"
);
FormTitleDto titleDto7 =
new
FormTitleDto(
"仿真工程师"
,
"distributeName"
,
"0,0,7,7"
);
FormTitleDto titleDto8 =
new
FormTitleDto(
"产品类型"
,
"productType"
,
"0,0,8,8"
);
FormTitleDto titleDto9 =
new
FormTitleDto(
"工况类型"
,
"miningTypeName"
,
"0,0,9,9"
);
FormTitleDto titleDto10 =
new
FormTitleDto(
"报告提交日期"
,
"submissionReportTime"
,
"0,0,10,10"
);
FormTitleDto titleDto11 =
new
FormTitleDto(
"报告首次审批日期"
,
"firstApprovalDate"
,
"0,0,11,11"
);
FormTitleDto titleDto12 =
new
FormTitleDto(
"报告审批完成日期"
,
"simulationTaskEndTime"
,
"0,0,12,12"
);
FormTitleDto titleDto13 =
new
FormTitleDto(
"报告来源"
,
"reportSources"
,
"0,0,13,13"
);
FormTitleDto titleDto14 =
new
FormTitleDto(
"仿真类别"
,
"simulationType"
,
"0,0,14,14"
);
FormTitleDto titleDto15 =
new
FormTitleDto(
"标准总价"
,
"unitPrcieSum"
,
"0,0,15,15"
);
FormTitleDto titleDto16 =
new
FormTitleDto(
"实际总价"
,
"actualPrcieSum"
,
"0,0,16,16"
);
FormTitleDto titleDto17 =
new
FormTitleDto(
"满意度评分"
,
"satisfaction"
,
"0,0,17,17"
);
FormTitleDto titleDto18 =
new
FormTitleDto(
"建议"
,
"suggestion"
,
"0,0,18,18"
);
FormTitleDto titleDto19 =
new
FormTitleDto(
"项目阶段"
,
"projectPhase"
,
"0,0,19,19"
);
FormTitleDto titleDto20 =
new
FormTitleDto(
"产品线"
,
"productLine"
,
"0,0,20,20"
);
List<FormTitleDto> formTitleDtoList =
new
ArrayList<>();
formTitleDtoList.add(titleDto0);
formTitleDtoList.add(titleDto1);
formTitleDtoList.add(titleDto2);
formTitleDtoList.add(titleDto3);
formTitleDtoList.add(titleDto4);
formTitleDtoList.add(titleDto5);
formTitleDtoList.add(titleDto6);
formTitleDtoList.add(titleDto7);
formTitleDtoList.add(titleDto8);
formTitleDtoList.add(titleDto9);
formTitleDtoList.add(titleDto10);
formTitleDtoList.add(titleDto11);
formTitleDtoList.add(titleDto12);
formTitleDtoList.add(titleDto13);
formTitleDtoList.add(titleDto14);
formTitleDtoList.add(titleDto15);
formTitleDtoList.add(titleDto16);
formTitleDtoList.add(titleDto17);
formTitleDtoList.add(titleDto18);
formTitleDtoList.add(titleDto19);
formTitleDtoList.add(titleDto20);
//导出数据(与表头配置字段对应才能把对应数据导出)
List<ApdTaskBill> resultList = apdTaskBillMapper.selectTaskBillInfoForReport(taskBill,
null
);
//处理key->value(快码)
resultList.forEach(e -> {
e.setPriority(codeService.getCodeMeaningByValue(requestContext,
"TRP_APD_TST_TEST_BILL_PRIORITY"
, e.getPriority()));
e.setProductType(codeService.getCodeMeaningByValue(requestContext,
"TRP_APD_MST_PRODUCT_TYPE"
, e.getProductType()));
e.setSimulationTaskStatus(codeService.getCodeMeaningByValue(requestContext,
"TRP_APD_TST_TEST_BILL_STATUS"
, e.getSimulationTaskStatus()));
e.setChangeStatus(codeService.getCodeMeaningByValue(requestContext,
"TRP_APD_TST_TEST_CHANGE_STATUS"
, e.getChangeStatus()));
});
String sheetName =
"任务单报告查询(内部)导出"
;
Map<Integer, FormHeadDto> formHeadMap =
new
HashMap(
16
);
FormTextDto formTextDto =
new
FormTextDto();
ExportExcelUtil.startDownLoad(response, sheetName, formHeadMap, formTitleDtoList, formTextDto, resultList);
}
//导出工具类ExportExcelUtil
package
com.catl.trp.utils.exportexcel.controllers;
import
com.catl.trp.utils.StringUtils;
import
com.catl.trp.utils.exportexcel.dto.FormHeadDto;
import
com.catl.trp.utils.exportexcel.dto.FormTextDto;
import
com.catl.trp.utils.exportexcel.dto.FormTitleDto;
import
org.apache.poi.hssf.usermodel.*;
import
org.apache.poi.ss.util.CellRangeAddress;
import
org.joda.time.DateTime;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
java.beans.PropertyDescriptor;
import
java.io.ByteArrayInputStream;
import
java.io.ByteArrayOutputStream;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.lang.reflect.Field;
import
java.lang.reflect.Method;
import
java.math.BigDecimal;
import
java.math.RoundingMode;
import
java.text.SimpleDateFormat;
import
java.util.*;
/**
* Created by zhuheng on 2017/8/16.
*/
public
class
ExportExcelUtil {
public
static
final
String TITLE_MAP =
"titleMap"
;
public
static
<E>
void
startDownLoad(HttpServletResponse response, String sheetName, Map<Integer, FormHeadDto> formHeadMap, List<FormTitleDto> formTitleDtoList, FormTextDto formTextDto, List<E> dataList) {
HSSFWorkbook workbook =
new
HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(sheetName);
// 创建一个表
try
{
Map map = writeDataTitle(workbook, sheet, formTitleDtoList);
Map temp = (Map) map.get(TITLE_MAP);
writeDataHead(workbook, sheet, formHeadMap, temp.size());
writeText(workbook, sheet, formTextDto, dataList, map);
IOWrite(workbook, sheet, response, sheetName);
}
catch
(Exception e) {
e.printStackTrace();
}
}
/**
* 此方法支持单sheet和多sheet导出
* @param request
* @param response
* @param sheetNames
* @param formTitleDtoList
* @param formHeadMap
* @param formTextDto
* @param dataList
* @param excelName
*/
public
static
<E>
void
startDownLoadForSheets(HttpServletRequest request, HttpServletResponse response,List<String> sheetNames,List<List<FormTitleDto>> formTitleDtoList,Map<Integer, FormHeadDto> formHeadMap, FormTextDto formTextDto,List<List> dataList, String excelName) {
HSSFWorkbook workbook =
new
HSSFWorkbook();
for
(
int
i =
0
; i < sheetNames.size(); i++) {
if
(!StringUtils.isEmpty(sheetNames.get(i)) && (formTitleDtoList.get(i)!=
null
&&formTitleDtoList.get(i).size()>
0
)
&& (dataList.get(i)!=
null
&&dataList.get(i).size()>
0
)){
String sheetName = sheetNames.get(i);
try
{
HSSFSheet hssfSheet = workbook.createSheet(sheetName);
Map map = writeDataTitle(workbook, hssfSheet, formTitleDtoList.get(i));
Map temp = (Map) map.get(TITLE_MAP);
writeDataHead(workbook, hssfSheet, formHeadMap, temp.size());
writeText(workbook, hssfSheet, formTextDto, dataList.get(i), map);
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
IOWriteForSheets(request, workbook, response, excelName);
}
/**
* 此方法支持单sheet和多sheet导出
*
* @param response
* @param sheetsList
* @param excelName
* @param <E>
*/
public
static
<E>
void
startDownLoadForSheets(HttpServletRequest request, HttpServletResponse response, List sheetsList, String excelName) {
HSSFWorkbook workbook =
new
HSSFWorkbook();
for
(
int
i =
0
; i < sheetsList.size(); i++) {
String sheetName =
null
;
Map<Integer, FormHeadDto> sheetFormHeadMap =
null
;
List<FormTitleDto> sheetFormTitleDtolist =
null
;
FormTextDto sheetFormTextDto =
null
;
List DataList =
null
;
Map sheetMap = (Map) sheetsList.get(i);
sheetName = (String) sheetMap.get(
"sheetName"
);
sheetFormHeadMap = (Map) sheetMap.get(
"formHead"
);
sheetFormTitleDtolist = (List<FormTitleDto>) sheetMap.get(
"formTitle"
);
sheetFormTextDto = (FormTextDto) sheetMap.get(
"textStyle"
);
DataList = (List) sheetMap.get(
"data"
);
try
{
HSSFSheet hssfSheet = workbook.createSheet(sheetName);
Map map = writeDataTitle(workbook, hssfSheet, sheetFormTitleDtolist);
Map temp = (Map) map.get(TITLE_MAP);
writeDataHead(workbook, hssfSheet, sheetFormHeadMap, temp.size());
writeText(workbook, hssfSheet, sheetFormTextDto, DataList, map);
}
catch
(Exception e) {
e.printStackTrace();
}
}
IOWriteForSheets(request, workbook, response, excelName);
}
public
static
void
writeDataHead(HSSFWorkbook workbook, HSSFSheet sheet, Map<Integer, FormHeadDto> headMap,
int
length) {
//初始化表头
if
(headMap !=
null
) {
for
(
int
i =
0
; i < headMap.size(); i++) {
HSSFRow row2 = sheet.createRow(i);
for
(
int
j =
0
; j < length; j++) {
FormHeadDto tempDto = headMap.get(i);
// 表头标题样式,时间样式
HSSFFont headfont = workbook.createFont();
headfont.setFontName(tempDto.getFontName());
headfont.setFontHeightInPoints(tempDto.getFontHeightInPoints());
// 字体大小
headfont.setBoldweight(tempDto.getBoldweight());
headfont.setColor(tempDto.getFontColor());
HSSFCellStyle headstyle = workbook.createCellStyle();
//加粗
headstyle.setFillForegroundColor(tempDto.getFillForegroundColor());
//背景
headstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headstyle.setFont(headfont);
headstyle.setAlignment(tempDto.getAlignment());
// 左右居中
headstyle.setVerticalAlignment(tempDto.getVerticalAlignment());
// 上下居中
HSSFCell cell3 = row2.createCell(j);
cell3.setCellValue(
""
);
cell3.setCellStyle(headstyle);
}
}
for
(
int
i =
0
; i < headMap.size(); i++) {
FormHeadDto tempDto = headMap.get(i);
// 表头标题样式,时间样式
HSSFFont headfont = workbook.createFont();
headfont.setFontName(tempDto.getFontName());
headfont.setFontHeightInPoints(tempDto.getFontHeightInPoints());
// 字体大小
headfont.setBoldweight(tempDto.getBoldweight());
headfont.setColor(tempDto.getFontColor());
HSSFCellStyle headstyle = workbook.createCellStyle();
//加粗
headstyle.setFillForegroundColor(tempDto.getFillForegroundColor());
//背景
headstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headstyle.setFont(headfont);
headstyle.setAlignment(tempDto.getAlignment());
// 左右居中
headstyle.setVerticalAlignment(tempDto.getVerticalAlignment());
// 上下居中
// 表头标题
if
(length >
1
) {
sheet.addMergedRegion(
new
CellRangeAddress(i, i,
0
, length -
1
));
}
HSSFRow row = sheet.getRow(i);
row.setHeight(tempDto.getRowHeight());
HSSFCell cell = row.getCell(
0
);
cell.setCellStyle(headstyle);
cell.setCellValue(tempDto.getHeadName());
}
}
}
public
static
Map writeDataTitle(HSSFWorkbook workbook, HSSFSheet sheet, List<FormTitleDto> list) {
//用来存标题属性(父标题除外)
Map<String, Integer> titleMap =
new
HashMap<String, Integer>();
Set<Integer> set =
new
HashSet<Integer>();
Set<Integer> col =
new
HashSet<Integer>();
//判断是否存在row
for
(
int
i =
0
; i < list.size(); i++) {
FormTitleDto tempDto = list.get(i);
String[] temp = tempDto.getXyLocation().split(
","
);
int
startrow = Integer.parseInt(temp[
0
]);
int
overrow = Integer.parseInt(temp[
1
]);
int
overcol = Integer.parseInt(temp[
3
]);
set.add(startrow);
set.add(overrow);
col.add(overcol);
}
Arrays.sort(col.toArray());
int
maxRow = (
int
) set.toArray()[set.toArray().length -
1
];
int
maxcol = (
int
) col.toArray()[col.toArray().length -
1
];
//初始化标题
for
(Integer row : set) {
HSSFRow row2 = sheet.createRow(row);
for
(
int
i =
0
; i <= maxcol; i++) {
FormTitleDto tempDto =
new
FormTitleDto();
//开始渲染列
HSSFFont font = workbook.createFont();
font.setFontName(tempDto.getFontName());
font.setFontHeightInPoints(tempDto.getFontHeightInPoints());
// 字体大小
font.setBoldweight(tempDto.getBoldweight());
font.setColor(tempDto.getFontColor());
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(tempDto.getFillForegroundColor());
//背景
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//右边框
style.setFont(font);
style.setAlignment(tempDto.getAlignment());
// 左右居中
style.setVerticalAlignment(tempDto.getVerticalAlignment());
// 上下居中
HSSFCell cell3 = row2.createCell(i);
cell3.setCellValue(
""
);
cell3.setCellStyle(style);
}
}
for
(
int
i =
0
; i < list.size(); i++) {
FormTitleDto tempDto = list.get(i);
if
(!(
""
.equals(tempDto.getColName())) && tempDto.getColName() !=
null
) {
titleMap.put(tempDto.getColName(), Integer.parseInt(tempDto.getXyLocation().split(
","
)[
3
]));
}
//开始渲染列
HSSFFont font = workbook.createFont();
font.setFontName(tempDto.getFontName());
font.setFontHeightInPoints(tempDto.getFontHeightInPoints());
// 字体大小
font.setBoldweight(tempDto.getBoldweight());
font.setColor(tempDto.getFontColor());
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(tempDto.getFillForegroundColor());
//背景
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//右边框
style.setFont(font);
style.setAlignment(tempDto.getAlignment());
// 左右居中
style.setVerticalAlignment(tempDto.getVerticalAlignment());
// 上下居中
//合并动态列
String[] temp = tempDto.getXyLocation().split(
","
);
int
startrow = Integer.parseInt(temp[
0
]);
int
overrow = Integer.parseInt(temp[
1
]);
int
startcol = Integer.parseInt(temp[
2
]);
int
overcol = Integer.parseInt(temp[
3
]);
if
(startrow != overrow || startcol != overcol) {
sheet.addMergedRegion(
new
CellRangeAddress(startrow, overrow,
startcol, overcol));
}
sheet.setColumnWidth(startcol, tempDto.getColumnWidth());
//设置列名值
if
(sheet.getRow(startrow) ==
null
) {
HSSFRow row = sheet.createRow(startrow);
HSSFCell cell3 = row.createCell(startcol);
cell3.setCellValue(tempDto.getTextName());
cell3.setCellStyle(style);
}
else
{
HSSFRow row = sheet.getRow(startrow);
HSSFCell cell3 = row.getCell(startcol);
cell3.setCellValue(tempDto.getTextName());
cell3.setCellStyle(style);
}
}
Map map =
new
HashMap();
map.put(
"maxRow"
, maxRow);
map.put(TITLE_MAP, titleMap);
map.put(
"maxcol"
, maxcol);
return
map;
}
public
boolean
isContains(String colName, Map titleMap) {
return
titleMap.containsKey(colName);
}
public
static
<E>
void
writeText(HSSFWorkbook workbook, HSSFSheet sheet, FormTextDto formTextDto, List<E> list, Map map)
throws
Exception {
// 普通单元格样式(中文)
HSSFFont font2 = workbook.createFont();
font2.setFontName(formTextDto.getFontName());
font2.setFontHeightInPoints(formTextDto.getFontHeightInPoints());
font2.setColor(formTextDto.getFontColor());
font2.setBoldweight(formTextDto.getBoldweight());
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(formTextDto.getFillForegroundColor());
//背景
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//下边框
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//左边框
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
//上边框
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
//右边框
style2.setFont(font2);
style2.setAlignment(formTextDto.getAlignment());
// 左右居中
style2.setWrapText(formTextDto.getWrapText());
// 换行
style2.setVerticalAlignment(formTextDto.getVerticalAlignment());
// 上下居中
//开始赋值
int
maxRow = (
int
) map.get(
"maxRow"
);
Map titleMap = (Map) map.get(TITLE_MAP);
HSSFRow row;
HSSFCell cell;
//真正的赋值开始了,写数据
for
(
int
i =
0
; i < list.size(); i++) {
row = sheet.createRow(i + maxRow +
1
);
Class tClass = list.get(
0
).getClass();
//获得该类的所有属性
Field[] fields = tClass.getDeclaredFields();
for
(Field field : fields) {
int
modifiers = field.getModifiers();
//获取属性的修饰
if
(!titleMap.containsKey(field.getName())) {
//如果该getter方法不是想要的属性方法,则跳过
continue
;
}
if
(modifiers ==
26
|| modifiers ==
25
) {
continue
;
}
if
(
"serialVersionUID"
.equals(field.getName())) {
continue
;
}
PropertyDescriptor pd =
new
PropertyDescriptor(field.getName(), tClass);
if
(pd ==
null
) {
continue
;
}
//获得get方法
Method get = pd.getReadMethod();
Object getValue = get.invoke(list.get(i),
new
Object[]{});
if
(titleMap.get(field.getName()) !=
null
) {
int
cellNum = (
int
) titleMap.get(field.getName());
cell = row.createCell(cellNum);
cell.setCellStyle(style2);
if
(getValue ==
null
) {
cell.setCellValue(
""
);
}
else
{
if
(getValue
instanceof
BigDecimal) {
//保留两位小数
getValue = ((BigDecimal) getValue).setScale(
2
, RoundingMode.HALF_UP);
}
//日期类型进行格式化
if
(getValue
instanceof
Date) {
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyy-MM-dd"
);
getValue = sdf.format(getValue);
}
//时间类型进行格式化
// if (getValue instanceof DateTime) {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
// getValue = sdf.format(getValue);
// }
cell.setCellValue((getValue.toString()));
}
}
}
Class superClazz1=tClass.getSuperclass();
//获取继承类 ,15个备用字段
Field[] superFields = superClazz1.getDeclaredFields();
for
(Field superfield : superFields) {
int
modifiers = superfield.getModifiers();
//获取属性的修饰
if
(!titleMap.containsKey(superfield.getName())) {
//如果该getter方法不是想要的属性方法,则跳过
continue
;
}
if
(modifiers ==
26
|| modifiers ==
25
) {
continue
;
}
if
(
"serialVersionUID"
.equals(superfield.getName())) {
continue
;
}
PropertyDescriptor superpd =
new
PropertyDescriptor(superfield.getName(), superClazz1);
if
(superpd ==
null
) {
continue
;
}
//获得get方法
Method superget = superpd.getReadMethod();
Object getValue = superget.invoke(list.get(i),
new
Object[]{});
if
(titleMap.get(superfield.getName()) !=
null
) {
int
cellNum = (
int
) titleMap.get(superfield.getName());
cell = row.createCell(cellNum);
cell.setCellStyle(style2);
if
(getValue ==
null
) {
cell.setCellValue(
""
);
}
else
{
if
(getValue
instanceof
BigDecimal) {
//保留两位小数
getValue = ((BigDecimal) getValue).setScale(
2
, RoundingMode.HALF_UP);
}
//日期类型进行格式化
if
(getValue
instanceof
Date) {
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyy-MM-dd"
);
getValue = sdf.format(getValue);
}
cell.setCellValue((getValue.toString()));
}
}
}
}
}
public
static
void
IOWrite(HSSFWorkbook workbook, HSSFSheet sheet, HttpServletResponse response, String sheetName) {
try
{
String fileName =
new
String(sheetName.getBytes(
"utf-8"
),
"ISO8859-1"
);
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
workbook.write(baos);
response.setContentType(
"application/x-download;charset=utf-8"
);
response.addHeader(
"Content-Disposition"
,
"attachment;filename="
+ fileName +
".xls"
);
OutputStream os = response.getOutputStream();
ByteArrayInputStream bais =
new
ByteArrayInputStream(baos.toByteArray());
byte
[] b =
new
byte
[
1024
];
while
((bais.read(b)) >
0
) {
os.write(b);
}
bais.close();
os.flush();
os.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
/**
* 用于多sheet导出
*
* @param workbook
* @param response
* @param excelName
*/
public
static
void
IOWriteForSheets(HttpServletRequest request, HSSFWorkbook workbook, HttpServletResponse response, String excelName) {
try
{
String agent = request.getHeader(
"USER-AGENT"
);
if
(
null
!= agent && -
1
!= agent.indexOf(
"MSIE"
) ||
null
!= agent
&& -
1
!= agent.indexOf(
"Trident"
) ||
null
!= agent && -
1
!= agent.indexOf(
"Edge"
)) {
// ie
String fileName = java.net.URLEncoder.encode(excelName,
"UTF8"
);
response.addHeader(
"Content-Disposition"
,
"attachment;filename="
+ fileName +
".xls"
);
}
else
if
(
null
!= agent && -
1
!= agent.indexOf(
"Mozilla"
)) {
// 火狐,chrome等
String fileName =
new
String(excelName.getBytes(
"utf-8"
),
"ISO8859-1"
);
response.addHeader(
"Content-Disposition"
,
"attachment;filename="
+ fileName +
".xls"
);
}
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
workbook.write(baos);
response.setContentType(
"application/x-download;charset=utf-8"
);
OutputStream os = response.getOutputStream();
ByteArrayInputStream bais =
new
ByteArrayInputStream(baos.toByteArray());
byte
[] b =
new
byte
[
1024
];
while
((bais.read(b)) >
0
) {
os.write(b);
}
bais.close();
os.flush();
os.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
//表头dto FormHeadDto
package
com.catl.trp.utils.exportexcel.dto;
import
com.catl.trp.utils.exportexcel.controllers.FillForegroundColorUtil;
import
org.apache.poi.hssf.usermodel.HSSFCellStyle;
import
org.apache.poi.hssf.usermodel.HSSFFont;
/**
* Created by zhuheng on 2017/8/16.
*/
public
class
FormHeadDto {
private
String headName;
//表头名
private
String fontName =
"宋体"
;
//字体
private
Short fontHeightInPoints =
10
;
//字体大小
private
Short isBoldweight = HSSFFont.BOLDWEIGHT_BOLD;
//是否加粗
private
Short fontColor = FillForegroundColorUtil.getBackGround(
"X2"
);
//字体颜色
private
Short fillForegroundColor = FillForegroundColorUtil.getBackGround(
"X50"
);
//背景颜色
private
Short alignment = HSSFCellStyle.ALIGN_CENTER;
//左右居中格式
private
Short verticalAlignment = HSSFCellStyle.VERTICAL_BOTTOM;
//上下居中格式
private
Short rowHeight =
255
;
//行高
public
static
final
String CENTER =
"center"
;
public
FormHeadDto(String headName) {
super
();
this
.headName = headName;
}
/**
* 无参构造器
*/
public
FormHeadDto() {
}
public
FormHeadDto(String headName, String fontName, Short fontHeightInPoints,
boolean
isBoldweight,
String fontColor, String fillForegroundColor, String alignment, String verticalAlignment, Short rowHeight) {
super
();
this
.headName = headName;
this
.fontName = fontName;
this
.rowHeight = rowHeight;
this
.fontHeightInPoints = fontHeightInPoints;
if
(isBoldweight) {
this
.isBoldweight = HSSFFont.BOLDWEIGHT_BOLD;
}
else
{
this
.isBoldweight = HSSFFont.BOLDWEIGHT_NORMAL;
}
//通过工具类查看背景颜色
this
.fillForegroundColor = FillForegroundColorUtil.getBackGround(fillForegroundColor);
//通过工具类查看字体颜色
this
.fontColor = FillForegroundColorUtil.getBackGround(fontColor);
if
(
"left"
.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_LEFT;
}
else
if
(
"right"
.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_RIGHT;
}
else
if
(CENTER.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_CENTER;
}
if
(
"bottom"
.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_BOTTOM;
}
else
if
(
"top"
.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_TOP;
}
else
if
(CENTER.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
}
}
@Deprecated
public
Short getFillForegroundColor() {
return
fillForegroundColor;
}
@Deprecated
public
void
setFillForegroundColor(String key) {
//通过工具类查看背景颜色
this
.fillForegroundColor = FillForegroundColorUtil.getBackGround(key);
}
public
String getHeadName() {
return
headName;
}
public
void
setHeadName(String headName) {
this
.headName = headName;
}
public
String getFontName() {
return
fontName;
}
public
void
setFontName(String fontName) {
this
.fontName = fontName;
}
public
Short getFontHeightInPoints() {
return
fontHeightInPoints;
}
public
void
setFontHeightInPoints(Short fontHeightInPoints) {
this
.fontHeightInPoints = fontHeightInPoints;
}
public
Short getBoldweight() {
return
isBoldweight;
}
public
void
setBoldweight(
boolean
isBoldweight) {
if
(isBoldweight) {
this
.isBoldweight = HSSFFont.BOLDWEIGHT_BOLD;
}
else
{
this
.isBoldweight = HSSFFont.BOLDWEIGHT_NORMAL;
}
}
public
Short getFontColor() {
return
fontColor;
}
public
void
setFontColor(String key) {
//通过工具类查看字体颜色
this
.fontColor = FillForegroundColorUtil.getBackGround(key);
}
public
Short getAlignment() {
return
alignment;
}
//alignment 左右居中格式
public
void
setAlignment(String alignment) {
if
(
"left"
.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_LEFT;
}
else
if
(
"right"
.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_RIGHT;
}
else
if
(CENTER.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_CENTER;
}
}
public
Short getVerticalAlignment() {
return
verticalAlignment;
}
//上下居中格式
public
void
setVerticalAlignment(String verticalAlignment) {
if
(
"bottom"
.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_BOTTOM;
}
else
if
(
"top"
.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_TOP;
}
else
if
(CENTER.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
}
}
public
Short getRowHeight() {
return
rowHeight;
}
public
void
setRowHeight(Short rowHeight) {
this
.rowHeight = rowHeight;
}
}
//内容样式dto FormTextDto
package
com.catl.trp.utils.exportexcel.dto;
import
com.catl.trp.utils.exportexcel.controllers.FillForegroundColorUtil;
import
org.apache.poi.hssf.usermodel.HSSFCellStyle;
import
org.apache.poi.hssf.usermodel.HSSFFont;
/**
* Created by zhuheng on 2017/8/16.
*/
public
class
FormTextDto {
private
String fontName =
"宋体"
;
//字体
private
Short fontHeightInPoints =
10
;
//字体大小
private
Short isBoldweight = HSSFFont.BOLDWEIGHT_NORMAL;
//是否加粗
private
Short fontColor = FillForegroundColorUtil.getBackGround(
"X2"
);
//字体颜色
private
Short fillForegroundColor = FillForegroundColorUtil.getBackGround(
"X50"
);
//背景颜色
private
Short alignment = HSSFCellStyle.ALIGN_CENTER;
//左右居中格式
private
Short verticalAlignment = HSSFCellStyle.VERTICAL_BOTTOM;
//上下居中格式
private
Short rowHeight =
255
;
//行高
private
boolean
wrapText =
false
;
//自动换行
public
static
final
String CENTER =
"center"
;
/**
* 无参构造器
*/
public
FormTextDto() {
}
public
FormTextDto(String fontName, Short fontHeightInPoints,
boolean
isBoldweight,
String fontColor, String fillForegroundColor, String alignment, String verticalAlignment, Short rowHeight) {
super
();
this
.fontName = fontName;
this
.rowHeight = rowHeight;
this
.fontHeightInPoints = fontHeightInPoints;
if
(isBoldweight) {
this
.isBoldweight = HSSFFont.BOLDWEIGHT_BOLD;
}
else
{
this
.isBoldweight = HSSFFont.BOLDWEIGHT_NORMAL;
}
//通过工具类查看背景颜色
this
.fillForegroundColor = FillForegroundColorUtil.getBackGround(fillForegroundColor);
//通过工具类查看字体颜色
this
.fontColor = FillForegroundColorUtil.getBackGround(fontColor);
if
(
"left"
.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_LEFT;
}
else
if
(
"right"
.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_RIGHT;
}
else
if
(CENTER.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_CENTER;
}
if
(
"bottom"
.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_BOTTOM;
}
else
if
(
"top"
.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_TOP;
}
else
if
(CENTER.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
}
}
public
Short getFillForegroundColor() {
return
fillForegroundColor;
}
public
void
setFillForegroundColor(String key) {
//通过工具类查看背景颜色
this
.fillForegroundColor = FillForegroundColorUtil.getBackGround(key);
}
public
String getFontName() {
return
fontName;
}
public
void
setFontName(String fontName) {
this
.fontName = fontName;
}
public
Short getFontHeightInPoints() {
return
fontHeightInPoints;
}
public
void
setFontHeightInPoints(Short fontHeightInPoints) {
this
.fontHeightInPoints = fontHeightInPoints;
}
public
Short getBoldweight() {
return
isBoldweight;
}
public
void
setBoldweight(
boolean
isBoldweight) {
if
(isBoldweight) {
this
.isBoldweight = HSSFFont.BOLDWEIGHT_BOLD;
}
else
{
this
.isBoldweight = HSSFFont.BOLDWEIGHT_NORMAL;
}
}
public
Short getFontColor() {
return
fontColor;
}
public
void
setFontColor(String key) {
//通过工具类查看字体颜色
this
.fontColor = FillForegroundColorUtil.getBackGround(key);
}
public
Short getAlignment() {
return
alignment;
}
//alignment 左右居中格式
public
void
setAlignment(String alignment) {
if
(
"left"
.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_LEFT;
}
else
if
(
"right"
.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_RIGHT;
}
else
if
(CENTER.equals(alignment)) {
this
.alignment = HSSFCellStyle.ALIGN_CENTER;
}
}
public
Short getVerticalAlignment() {
return
verticalAlignment;
}
//上下居中格式
public
void
setVerticalAlignment(String verticalAlignment) {
if
(
"bottom"
.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_BOTTOM;
}
else
if
(
"top"
.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_TOP;
}
else
if
(CENTER.equals(verticalAlignment)) {
this
.verticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
}
}
public
Short getRowHeight() {
return
rowHeight;
}
public
void
setRowHeight(Short rowHeight) {
this
.rowHeight = rowHeight;
}
public
boolean
getWrapText() {
return
wrapText;
}
public
void
setWrapText(
boolean
wrapText) {
this
.wrapText = wrapText;
}
}
标签:String,FormTitleDto,导出,grid,HSSFCellStyle,new,public,tempDto 来源: https://www.cnblogs.com/yt-yt/p/11431915.html