后期还会优化,任务急.先这样做.
页面效果
excel导出效果
public void exportStatDeliPrice(String header, String body, HttpServletResponse response) throws Exception {
RequestModel<QuoteStatDeliPriceQueryParam> requestModel = RequestUtil.getRequestModel(header, body,
QuoteStatDeliPriceQueryParam.class);
int totalCount = statdeliPriceServiceImpl.queryStatDeliPriceCount(requestModel);
if (totalCount > 100000) {
throw new Exception("导出数据不能超过100000笔!");
}
// 设置导出数据数量
requestModel.getBody().setPageCount(totalCount);
requestModel.getBody().setPage(1);
PageResult<QuoteStatDeliPriceQueryResult> pageInfo = statdeliPriceServiceImpl.queryStatDeliPrice(requestModel);
List<QuoteStatDeliPriceQueryResult> dataList = pageInfo.getList();
// 获取表头的名称
List<String> titleList = new QuoteStatDeliPriceQueryResult().excelList();
XSSFWorkbook wb = new XSSFWorkbook(); // 创建工作簿
XSSFSheet sheet = wb.createSheet(); // 创建sheet
XSSFRow row = null;
XSSFCell cell = null;
sheet.protectSheet("edit");
XSSFCellStyle cellStyle5 = initColumnBorderStyle(wb); // 设置边框
XSSFCellStyle title1CellStyle = inittitle1CellStyle(wb); // 第一行样式
XSSFCellStyle cellStyle = inittitle2_3cellStyle(wb); // 第二行和第三行样式
XSSFCellStyle cellStyle2 = initMergeStyle(wb); // 合并行的 样式
XSSFCellStyle cellStyle3 = contentStyle(wb);
for (int i = 0; i < totalCount + 4; i++) {
row = sheet.createRow(i);
for (int j = 0; j < titleList.size(); j++) {
cell = row.createCell(j);
cell.setCellStyle(cellStyle5);
}
}
// 创建表格第一行
String fileName = requestModel.getBody().getFilename();
System.out.println(fileName);
cell = sheet.getRow(0).getCell(0);
cell.setCellValue(fileName);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 19));
cell.setCellStyle(title1CellStyle);
// 设置行高
row = sheet.getRow(0);
row.setHeight((short) (30 * 25));
// 设置列宽
sheet.setColumnWidth(3, 256 * 30 + 184);
sheet.setColumnWidth(4, 256 * 12 + 184);
sheet.setColumnWidth(7, 256 * 12 + 184);
sheet.setColumnWidth(13, 256 * 12 + 184);
sheet.setColumnWidth(14, 256 * 12 + 184);
sheet.setColumnWidth(17, 256 * 12 + 184);
sheet.setColumnWidth(18, 256 * 12 + 184);
// 创建表格标题行 第二行
cell = sheet.getRow(1).getCell(0);
cell.setCellValue("到达");
sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 3));
cell = sheet.getRow(1).getCell(4);
cell.setCellValue("操作费");
sheet.addMergedRegion(new CellRangeAddress(1, 2, 4, 4));
cell = sheet.getRow(1).getCell(5);
cell.setCellValue("干线");
sheet.addMergedRegion(new CellRangeAddress(1, 2, 5, 11));
cell = sheet.getRow(1).getCell(12);
cell.setCellValue("派送");
sheet.addMergedRegion(new CellRangeAddress(1, 1, 12, 19));
// 创建表格第三行
row = sheet.getRow(2);
row.createCell(12).setCellValue("送货");
sheet.addMergedRegion(new CellRangeAddress(2, 2, 12, 15));
row.createCell(16).setCellValue("自提");
sheet.addMergedRegion(new CellRangeAddress(2, 2, 16, 19));
// 设置表头居中
XSSFCell nowCell1 = sheet.getRow(1).getCell(0);
XSSFCell nowCell2 = sheet.getRow(1).getCell(4);
XSSFCell nowCell3 = sheet.getRow(1).getCell(5);
XSSFCell nowCell4 = sheet.getRow(1).getCell(12);
XSSFCell nowCell5 = sheet.getRow(2).getCell(12);
XSSFCell nowCell6 = sheet.getRow(2).getCell(16);
nowCell1.setCellStyle(cellStyle);
nowCell2.setCellStyle(cellStyle);
nowCell3.setCellStyle(cellStyle);
nowCell4.setCellStyle(cellStyle);
nowCell5.setCellStyle(cellStyle);
nowCell6.setCellStyle(cellStyle);
// 创建表头 第四行
row = sheet.getRow(3);
for (int i = 0; i < titleList.size(); i++) {
row.getCell(i).setCellValue(titleList.get(i));
cell = sheet.getRow(3).getCell(i);
cell.setCellStyle(cellStyle);
}
if (CollectionUtils.isEmpty(pageInfo.getList())) {
pageInfo.setList(new ArrayList<>());
}
if (!CollectionUtils.isEmpty(pageInfo.getList())) {
insertDataSheet(dataList, sheet, cellStyle3);
}
for (int z = 0; z < 3; z++) {
row = sheet.createRow(totalCount + 4);
row.createCell(z).setCellValue("");
}
mergeCell(dataList.size(), cellStyle2, sheet, 0);
mergeCell(dataList.size(), cellStyle2, sheet, 1);
mergeCell(dataList.size(), cellStyle2, sheet, 2);
excelUtil.exporExcel(wb, response, "导出excel");
}
private void insertDataSheet(List<QuoteStatDeliPriceQueryResult> dataList, XSSFSheet sheet,
XSSFCellStyle cellStyle3) {
XSSFRow row;
XSSFCell cell;
//开始插入行
for (int i = 0; i < dataList.size(); i++) {
row = sheet.getRow(i + 4);
row.getCell(0).setCellValue(dataList.get(i).getState());
row.getCell(1).setCellValue(dataList.get(i).getArriveStation());
row.getCell(2).setCellValue(dataList.get(i).getAreaName());
row.getCell(3).setCellValue(dataList.get(i).getDeliStationStr());
if(dataList.get(i).getOperFee() != null){
row.getCell(4).setCellValue(String.valueOf(dataList.get(i).getOperFee()));
}else{
row.getCell(4).setCellValue("");
}
if(dataList.get(i).getTrunkMileage() != null){
row.getCell(5).setCellValue(String.valueOf(dataList.get(i).getTrunkMileage()));
}else{
row.getCell(5).setCellValue("");
}
if(dataList.get(i).getTrunkValue1() != null){
row.getCell(6).setCellValue(String.valueOf(dataList.get(i).getTrunkValue1()));
}else{
row.getCell(6).setCellValue("");
}
if(dataList.get(i).getTrunkValue2() != null){
row.getCell(7).setCellValue(String.valueOf(dataList.get(i).getTrunkValue2()));
}else{
row.getCell(7).setCellValue("");
}
if(dataList.get(i).getTrunkValue3() != null){
row.getCell(8).setCellValue(String.valueOf(dataList.get(i).getTrunkValue3()));
}else{
row.getCell(8).setCellValue("");
}
if(dataList.get(i).getTrunkValue4() != null){
row.getCell(9).setCellValue(String.valueOf(dataList.get(i).getTrunkValue4()));
}else{
row.getCell(9).setCellValue("");
}
if(dataList.get(i).getTrunkValue5() != null){
row.getCell(10).setCellValue(String.valueOf(dataList.get(i).getTrunkValue5()));
}else{
row.getCell(10).setCellValue("");
}
if(dataList.get(i).getTrunkValue6() != null){
row.getCell(11).setCellValue(String.valueOf(dataList.get(i).getTrunkValue6()));
}else{
row.getCell(11).setCellValue("");
}
if(dataList.get(i).getDelipValue1() != null){
row.getCell(12).setCellValue(String.valueOf(dataList.get(i).getDelipValue1()));
}else{
row.getCell(12).setCellValue("");
}
if(dataList.get(i).getDelipValue2() != null){
row.getCell(13).setCellValue(String.valueOf(dataList.get(i).getDelipValue2()));
}else{
row.getCell(13).setCellValue("");
}
if(dataList.get(i).getDelipValue3() != null){
row.getCell(14).setCellValue(String.valueOf(dataList.get(i).getDelipValue3()));
}else{
row.getCell(14).setCellValue("");
}
if(dataList.get(i).getDelipValue4() != null){
row.getCell(15).setCellValue(String.valueOf(dataList.get(i).getDelipValue4()));
}else{
row.getCell(15).setCellValue("");
}
if(dataList.get(i).getDelizValue1() != null){
row.getCell(16).setCellValue(String.valueOf(dataList.get(i).getDelizValue1()));
}else{
row.getCell(16).setCellValue("");
}
if(dataList.get(i).getDelizValue2() != null){
row.getCell(17).setCellValue(String.valueOf(dataList.get(i).getDelizValue2()));
}else{
row.getCell(17).setCellValue("");
}
if(dataList.get(i).getDelizValue3() != null){
row.getCell(18).setCellValue(String.valueOf(dataList.get(i).getDelizValue3()));
}else{
row.getCell(18).setCellValue("");
}
if(dataList.get(i).getDelizValue4() != null){
row.getCell(19).setCellValue(String.valueOf(dataList.get(i).getDelizValue4()));
}else{
row.getCell(19).setCellValue("");
}
/*row.getCell(4).setCellValue(String.valueOf(dataList.get(i).getOperFee()));
row.getCell(5).setCellValue(String.valueOf(dataList.get(i).getTrunkMileage()));
row.getCell(6).setCellValue(String.valueOf(dataList.get(i).getTrunkValue1()));
row.getCell(7).setCellValue(String.valueOf(dataList.get(i).getTrunkValue2()));
row.getCell(8).setCellValue(String.valueOf(dataList.get(i).getTrunkValue3()));
row.getCell(9).setCellValue(String.valueOf(dataList.get(i).getTrunkValue4()));
row.getCell(10).setCellValue(String.valueOf(dataList.get(i).getTrunkValue5()));
row.getCell(11).setCellValue(String.valueOf(dataList.get(i).getTrunkValue6()));
row.getCell(12).setCellValue(String.valueOf(dataList.get(i).getDelipValue1()));
row.getCell(13).setCellValue(String.valueOf(dataList.get(i).getDelipValue2()));
row.getCell(14).setCellValue(String.valueOf(dataList.get(i).getDelipValue3()));
row.getCell(15).setCellValue(String.valueOf(dataList.get(i).getDelipValue4()));
row.getCell(16).setCellValue(String.valueOf(dataList.get(i).getDelizValue1()));
row.getCell(17).setCellValue(String.valueOf(dataList.get(i).getDelizValue2()));
row.getCell(18).setCellValue(String.valueOf(dataList.get(i).getDelizValue3()));
row.getCell(19).setCellValue(String.valueOf(dataList.get(i).getDelizValue4()));*/
for (int j = 0; j < 16; j++) {
cell = sheet.getRow(4 + i).getCell(j + 4);
cell.setCellStyle(cellStyle3);
}
}
}
private XSSFCellStyle contentStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle(); // 内容体样式
cellStyle.setAlignment(HorizontalAlignment.RIGHT);
cellStyle.setLeftBorderColor(new XSSFColor(new Color(0,0,0)));// 左边框的颜色
cellStyle.setBorderLeft(BorderStyle.THIN);// 边框的大小
cellStyle.setRightBorderColor(new XSSFColor(new Color(0,0,0)));// 右边框的颜色
cellStyle.setBorderRight(BorderStyle.THIN);// 边框的大小
cellStyle.setBorderBottom(BorderStyle.THIN); // 设置单元格的边框为粗体
cellStyle.setBottomBorderColor(new XSSFColor(new Color(0,0,0))); // 设置单元格的边框颜色
cellStyle.setLocked(true);
return cellStyle;
}
private XSSFCellStyle initMergeStyle(XSSFWorkbook wb) {
// 设置合并行样式
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.LEFT); // 靠左
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
cellStyle.setLeftBorderColor(new XSSFColor(new Color(0,0,0)));// 左边框的颜色
cellStyle.setBorderLeft(BorderStyle.THIN);// 边框的大小
cellStyle.setRightBorderColor(new XSSFColor(new Color(0,0,0)));// 右边框的颜色
cellStyle.setBorderRight(BorderStyle.THIN);// 边框的大小
cellStyle.setBorderBottom(BorderStyle.THIN); // 设置单元格的边框为粗体
cellStyle.setBottomBorderColor(new XSSFColor(new Color(0,0,0))); // 设置单元格的边框颜色
cellStyle.setLocked(true);
cellStyle.setLocked(true);
return cellStyle;
}
private XSSFCellStyle inittitle2_3cellStyle(XSSFWorkbook wb) {
// 设置第二行到第四行样式
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
cellStyle.setFillForegroundColor(new XSSFColor( new Color(198,226,164)));
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setLeftBorderColor(new XSSFColor(new Color(0,0,0)));// 左边框的颜色
cellStyle.setBorderLeft(BorderStyle.THIN);// 边框的大小
cellStyle.setRightBorderColor(new XSSFColor(new Color(0,0,0)));// 右边框的颜色
cellStyle.setBorderRight(BorderStyle.THIN);// 边框的大小
cellStyle.setBorderBottom(BorderStyle.THIN); // 设置单元格的边框为粗体
cellStyle.setBottomBorderColor(new XSSFColor(new Color(0,0,0))); // 设置单元格的边框颜色
XSSFFont font1 = wb.createFont();
font1.setBold(true); // 字体加粗
cellStyle.setFont(font1);
cellStyle.setLocked(true);
return cellStyle;
}
private XSSFCellStyle inittitle1CellStyle(XSSFWorkbook wb) {
// 设置第一行样式
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
cellStyle.setFillForegroundColor(new XSSFColor( new Color(198,226,164)));
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setLeftBorderColor(new XSSFColor(new Color(0,0,0)));// 左边框的颜色
cellStyle.setBorderLeft(BorderStyle.THIN);// 边框的大小
cellStyle.setRightBorderColor(new XSSFColor(new Color(0,0,0)));// 右边框的颜色
cellStyle.setBorderRight(BorderStyle.THIN);// 边框的大小
cellStyle.setBorderBottom(BorderStyle.THIN); // 设置单元格的边框为粗体
cellStyle.setBottomBorderColor(new XSSFColor(new Color(0,0,0))); // 设置单元格的边框颜色
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 20);// 设置字体大小
font.setBold(true); // 字体加粗
cellStyle.setFont(font);
cellStyle.setLocked(true);
return cellStyle;
}
private XSSFCellStyle initColumnBorderStyle(XSSFWorkbook wb) {
// 设置边框
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setLeftBorderColor(new XSSFColor(new Color(0,0,0)));// 左边框的颜色
cellStyle.setBorderLeft(BorderStyle.THIN);// 边框的大小
cellStyle.setRightBorderColor(new XSSFColor(new Color(0,0,0)));// 右边框的颜色
cellStyle.setBorderRight(BorderStyle.THIN); // 边框的大小
cellStyle.setBorderBottom(BorderStyle.THIN);// 设置单元格的边框为粗体
cellStyle.setBottomBorderColor(new XSSFColor(new Color(0,0,0))); // 设置单元格的边框颜色
return cellStyle;
}
private void mergeCell(int size, XSSFCellStyle cellStyle, XSSFSheet sheet, int a) {
int currnetRow = 4;// 开始查找的行
int totalRow = size + 4;
for (int p = 4; p < totalRow; p++) {// totalRow 总行数
XSSFCell currentCell = sheet.getRow(p).getCell(a);
String current = getStringCellValue(currentCell);
XSSFCell nextCell = null;
String next = "";
if (p < totalRow + 1) {
XSSFRow nowRow = sheet.getRow(p + 1);
if (nowRow != null) {
nextCell = nowRow.getCell(a);
next = getStringCellValue(nextCell);
} else {
next = "";
}
} else {
next = "";
}
if (current.equals(next)) {// 比对是否相同
currentCell.setCellValue("");
continue;
} else if (!current.equals(next)) {
if ((p) - currnetRow > 0 && current != "") {
sheet.addMergedRegion(new CellRangeAddress(currnetRow, p, a, a));// 合并单元格
XSSFCell nowCell = sheet.getRow(currnetRow).getCell(a);
nowCell.setCellValue(current);
nowCell.setCellStyle(cellStyle);
}
currnetRow = p + 1;
}
}
}
@SuppressWarnings({ "deprecation", "unused" })
private String getStringCellValue(XSSFCell cell) {
String strCell = "";
if (cell != null) {
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case XSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
}
return strCell;
}