| @@ -1,13 +1,13 @@ | |||
| /** | |||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | |||
| * | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * <p> | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * <p> | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * <p> | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| @@ -21,6 +21,8 @@ import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import java.io.*; | |||
| import java.net.URL; | |||
| import java.net.URLConnection; | |||
| /** | |||
| * 文件加载类,根据路径加载指定文件 | |||
| @@ -37,18 +39,30 @@ public class FileLoaderImpl implements IFileLoader { | |||
| InputStream fileis = null; | |||
| ByteArrayOutputStream baos = null; | |||
| try { | |||
| //先用绝对路径查询,再查询相对路径 | |||
| try { | |||
| fileis = new FileInputStream(url); | |||
| } catch (FileNotFoundException e) { | |||
| //获取项目文件 | |||
| fileis = FileLoaderImpl.class.getClassLoader().getResourceAsStream(url); | |||
| if (fileis == null) { | |||
| //最后再拿相对文件路径 | |||
| String path = PoiPublicUtil.getWebRootPath(url); | |||
| fileis = new FileInputStream(path); | |||
| //判断是否是网络地址 | |||
| if (url.startsWith("http")) { | |||
| URL urlObj = new URL(url); | |||
| URLConnection urlConnection = urlObj.openConnection(); | |||
| urlConnection.setConnectTimeout(30); | |||
| urlConnection.setReadTimeout(60); | |||
| urlConnection.setDoInput(true); | |||
| fileis = urlConnection.getInputStream(); | |||
| } else { | |||
| //先用绝对路径查询,再查询相对路径 | |||
| try { | |||
| fileis = new FileInputStream(url); | |||
| } catch (FileNotFoundException e) { | |||
| //获取项目文件 | |||
| fileis = FileLoaderImpl.class.getClassLoader().getResourceAsStream(url); | |||
| if (fileis == null) { | |||
| //最后再拿相对文件路径 | |||
| String path = PoiPublicUtil.getWebRootPath(url); | |||
| fileis = new FileInputStream(path); | |||
| } | |||
| } | |||
| } | |||
| baos = new ByteArrayOutputStream(); | |||
| byte[] buffer = new byte[1024]; | |||
| int len; | |||
| @@ -83,6 +83,11 @@ public class ExportParams extends ExcelBaseParams { | |||
| * Excel 导出style | |||
| */ | |||
| private Class<?> style = ExcelExportStylerDefaultImpl.class; | |||
| /** | |||
| * 表头高度 | |||
| */ | |||
| private double headerHeight = 9D; | |||
| /** | |||
| * 是否创建表头 | |||
| */ | |||
| @@ -272,4 +277,11 @@ public class ExportParams extends ExcelBaseParams { | |||
| this.height = height; | |||
| } | |||
| public short getHeaderHeight() { | |||
| return (short) (titleHeight * 50); | |||
| } | |||
| public void setHeaderHeight(double headerHeight) { | |||
| this.headerHeight = headerHeight; | |||
| } | |||
| } | |||
| @@ -55,9 +55,9 @@ public class ExcelExportService extends BaseExportService { | |||
| List<ExcelExportEntity> excelParams) { | |||
| int rows = 0, fieldLength = getFieldLength(excelParams); | |||
| if (entity.getTitle() != null) { | |||
| rows += createHeaderRow(entity, sheet, workbook, fieldLength); | |||
| rows += createTitle2Row(entity, sheet, workbook, fieldLength); | |||
| } | |||
| rows += createTitleRow(entity, sheet, workbook, rows, excelParams); | |||
| rows += createHeaderRow(entity, sheet, workbook, rows, excelParams); | |||
| sheet.createFreezePane(0, rows, 0, rows); | |||
| return rows; | |||
| } | |||
| @@ -65,7 +65,7 @@ public class ExcelExportService extends BaseExportService { | |||
| /** | |||
| * 创建 表头改变 | |||
| */ | |||
| public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, | |||
| public int createTitle2Row(ExportParams entity, Sheet sheet, Workbook workbook, | |||
| int fieldWidth) { | |||
| Row row = sheet.createRow(0); | |||
| @@ -212,15 +212,15 @@ public class ExcelExportService extends BaseExportService { | |||
| /** | |||
| * 创建表头 | |||
| */ | |||
| private int createTitleRow(ExportParams title, Sheet sheet, Workbook workbook, int index, | |||
| List<ExcelExportEntity> excelParams) { | |||
| private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index, | |||
| List<ExcelExportEntity> excelParams) { | |||
| Row row = sheet.createRow(index); | |||
| int rows = getRowNums(excelParams); | |||
| row.setHeight((short) 450); | |||
| row.setHeight(title.getHeaderHeight()); | |||
| Row listRow = null; | |||
| if (rows == 2) { | |||
| listRow = sheet.createRow(index + 1); | |||
| listRow.setHeight((short) 450); | |||
| listRow.setHeight(title.getHeaderHeight()); | |||
| } | |||
| int cellIndex = 0; | |||
| int groupCellLength = 0; | |||
| @@ -302,7 +302,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| cell = row.getCell(i); | |||
| if (row.getCell(i) != null && (cell.getCellType() == Cell.CELL_TYPE_STRING | |||
| || cell.getCellType() == Cell.CELL_TYPE_NUMERIC)) { | |||
| String text = PoiCellUtil.getCellValue(cell); // plq modify at 2017-11-11 | |||
| String text = PoiCellUtil.getCellValue(cell); | |||
| if (text.contains(FOREACH_COL) || text.contains(FOREACH_COL_VALUE)) { | |||
| foreachCol(cell, map, text); | |||
| } | |||