正在显示
1 个修改的文件
包含
126 行增加
和
22 行删除
| @@ -47,6 +47,7 @@ import org.apache.poi.ss.usermodel.Sheet; | @@ -47,6 +47,7 @@ import org.apache.poi.ss.usermodel.Sheet; | ||
| 47 | import org.apache.poi.ss.usermodel.VerticalAlignment; | 47 | import org.apache.poi.ss.usermodel.VerticalAlignment; |
| 48 | import org.apache.poi.ss.usermodel.Workbook; | 48 | import org.apache.poi.ss.usermodel.Workbook; |
| 49 | import org.apache.poi.ss.usermodel.WorkbookFactory; | 49 | import org.apache.poi.ss.usermodel.WorkbookFactory; |
| 50 | +import org.apache.poi.ss.util.CellRangeAddress; | ||
| 50 | import org.apache.poi.ss.util.CellRangeAddressList; | 51 | import org.apache.poi.ss.util.CellRangeAddressList; |
| 51 | import org.apache.poi.util.IOUtils; | 52 | import org.apache.poi.util.IOUtils; |
| 52 | import org.apache.poi.xssf.streaming.SXSSFWorkbook; | 53 | import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
| @@ -126,6 +127,16 @@ public class ExcelUtil<T> | @@ -126,6 +127,16 @@ public class ExcelUtil<T> | ||
| 126 | private List<Object[]> fields; | 127 | private List<Object[]> fields; |
| 127 | 128 | ||
| 128 | /** | 129 | /** |
| 130 | + * 当前行号 | ||
| 131 | + */ | ||
| 132 | + private int rownum; | ||
| 133 | + | ||
| 134 | + /** | ||
| 135 | + * 标题 | ||
| 136 | + */ | ||
| 137 | + private String title; | ||
| 138 | + | ||
| 139 | + /** | ||
| 129 | * 最大高度 | 140 | * 最大高度 |
| 130 | */ | 141 | */ |
| 131 | private short maxHeight; | 142 | private short maxHeight; |
| @@ -150,7 +161,7 @@ public class ExcelUtil<T> | @@ -150,7 +161,7 @@ public class ExcelUtil<T> | ||
| 150 | this.clazz = clazz; | 161 | this.clazz = clazz; |
| 151 | } | 162 | } |
| 152 | 163 | ||
| 153 | - public void init(List<T> list, String sheetName, Type type) | 164 | + public void init(List<T> list, String sheetName, String title, Type type) |
| 154 | { | 165 | { |
| 155 | if (list == null) | 166 | if (list == null) |
| 156 | { | 167 | { |
| @@ -159,8 +170,27 @@ public class ExcelUtil<T> | @@ -159,8 +170,27 @@ public class ExcelUtil<T> | ||
| 159 | this.list = list; | 170 | this.list = list; |
| 160 | this.sheetName = sheetName; | 171 | this.sheetName = sheetName; |
| 161 | this.type = type; | 172 | this.type = type; |
| 173 | + this.title = title; | ||
| 162 | createExcelField(); | 174 | createExcelField(); |
| 163 | createWorkbook(); | 175 | createWorkbook(); |
| 176 | + createTitle(); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + /** | ||
| 180 | + * 创建excel第一行标题 | ||
| 181 | + */ | ||
| 182 | + public void createTitle() | ||
| 183 | + { | ||
| 184 | + if (StringUtils.isNotEmpty(title)) | ||
| 185 | + { | ||
| 186 | + Row titleRow = sheet.createRow(rownum == 0 ? rownum++ : 0); | ||
| 187 | + titleRow.setHeightInPoints(30); | ||
| 188 | + Cell titleCell = titleRow.createCell(0); | ||
| 189 | + titleCell.setCellStyle(styles.get("title")); | ||
| 190 | + titleCell.setCellValue(title); | ||
| 191 | + sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), | ||
| 192 | + this.fields.size() - 1)); | ||
| 193 | + } | ||
| 164 | } | 194 | } |
| 165 | 195 | ||
| 166 | /** | 196 | /** |
| @@ -171,17 +201,30 @@ public class ExcelUtil<T> | @@ -171,17 +201,30 @@ public class ExcelUtil<T> | ||
| 171 | */ | 201 | */ |
| 172 | public List<T> importExcel(InputStream is) throws Exception | 202 | public List<T> importExcel(InputStream is) throws Exception |
| 173 | { | 203 | { |
| 174 | - return importExcel(StringUtils.EMPTY, is); | 204 | + return importExcel(is, 0); |
| 205 | + } | ||
| 206 | + | ||
| 207 | + /** | ||
| 208 | + * 对excel表单默认第一个索引名转换成list | ||
| 209 | + * | ||
| 210 | + * @param is 输入流 | ||
| 211 | + * @param titleNum 标题占用行数 | ||
| 212 | + * @return 转换后集合 | ||
| 213 | + */ | ||
| 214 | + public List<T> importExcel(InputStream is, int titleNum) throws Exception | ||
| 215 | + { | ||
| 216 | + return importExcel(StringUtils.EMPTY, is, titleNum); | ||
| 175 | } | 217 | } |
| 176 | 218 | ||
| 177 | /** | 219 | /** |
| 178 | * 对excel表单指定表格索引名转换成list | 220 | * 对excel表单指定表格索引名转换成list |
| 179 | * | 221 | * |
| 180 | * @param sheetName 表格索引名 | 222 | * @param sheetName 表格索引名 |
| 223 | + * @param titleNum 标题占用行数 | ||
| 181 | * @param is 输入流 | 224 | * @param is 输入流 |
| 182 | * @return 转换后集合 | 225 | * @return 转换后集合 |
| 183 | */ | 226 | */ |
| 184 | - public List<T> importExcel(String sheetName, InputStream is) throws Exception | 227 | + public List<T> importExcel(String sheetName, InputStream is, int titleNum) throws Exception |
| 185 | { | 228 | { |
| 186 | this.type = Type.IMPORT; | 229 | this.type = Type.IMPORT; |
| 187 | this.wb = WorkbookFactory.create(is); | 230 | this.wb = WorkbookFactory.create(is); |
| @@ -210,7 +253,7 @@ public class ExcelUtil<T> | @@ -210,7 +253,7 @@ public class ExcelUtil<T> | ||
| 210 | // 定义一个map用于存放excel列的序号和field. | 253 | // 定义一个map用于存放excel列的序号和field. |
| 211 | Map<String, Integer> cellMap = new HashMap<String, Integer>(); | 254 | Map<String, Integer> cellMap = new HashMap<String, Integer>(); |
| 212 | // 获取表头 | 255 | // 获取表头 |
| 213 | - Row heard = sheet.getRow(0); | 256 | + Row heard = sheet.getRow(titleNum); |
| 214 | for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) | 257 | for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) |
| 215 | { | 258 | { |
| 216 | Cell cell = heard.getCell(i); | 259 | Cell cell = heard.getCell(i); |
| @@ -243,7 +286,7 @@ public class ExcelUtil<T> | @@ -243,7 +286,7 @@ public class ExcelUtil<T> | ||
| 243 | } | 286 | } |
| 244 | } | 287 | } |
| 245 | } | 288 | } |
| 246 | - for (int i = 1; i <= rows; i++) | 289 | + for (int i = titleNum + 1; i <= rows; i++) |
| 247 | { | 290 | { |
| 248 | // 从第2行开始取数据,默认第一行是表头. | 291 | // 从第2行开始取数据,默认第一行是表头. |
| 249 | Row row = sheet.getRow(i); | 292 | Row row = sheet.getRow(i); |
| @@ -369,7 +412,20 @@ public class ExcelUtil<T> | @@ -369,7 +412,20 @@ public class ExcelUtil<T> | ||
| 369 | */ | 412 | */ |
| 370 | public AjaxResult exportExcel(List<T> list, String sheetName) | 413 | public AjaxResult exportExcel(List<T> list, String sheetName) |
| 371 | { | 414 | { |
| 372 | - this.init(list, sheetName, Type.EXPORT); | 415 | + return exportExcel(list, sheetName, StringUtils.EMPTY); |
| 416 | + } | ||
| 417 | + | ||
| 418 | + /** | ||
| 419 | + * 对list数据源将其里面的数据导入到excel表单 | ||
| 420 | + * | ||
| 421 | + * @param list 导出数据集合 | ||
| 422 | + * @param sheetName 工作表的名称 | ||
| 423 | + * @param title 标题 | ||
| 424 | + * @return 结果 | ||
| 425 | + */ | ||
| 426 | + public AjaxResult exportExcel(List<T> list, String sheetName, String title) | ||
| 427 | + { | ||
| 428 | + this.init(list, sheetName, title, Type.EXPORT); | ||
| 373 | return exportExcel(); | 429 | return exportExcel(); |
| 374 | } | 430 | } |
| 375 | 431 | ||
| @@ -382,11 +438,26 @@ public class ExcelUtil<T> | @@ -382,11 +438,26 @@ public class ExcelUtil<T> | ||
| 382 | * @return 结果 | 438 | * @return 结果 |
| 383 | * @throws IOException | 439 | * @throws IOException |
| 384 | */ | 440 | */ |
| 385 | - public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) throws IOException | 441 | + public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)throws IOException |
| 442 | + { | ||
| 443 | + exportExcel(response, list, sheetName, StringUtils.EMPTY); | ||
| 444 | + } | ||
| 445 | + | ||
| 446 | + /** | ||
| 447 | + * 对list数据源将其里面的数据导入到excel表单 | ||
| 448 | + * | ||
| 449 | + * @param response 返回数据 | ||
| 450 | + * @param list 导出数据集合 | ||
| 451 | + * @param sheetName 工作表的名称 | ||
| 452 | + * @param title 标题 | ||
| 453 | + * @return 结果 | ||
| 454 | + * @throws IOException | ||
| 455 | + */ | ||
| 456 | + public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title) throws IOException | ||
| 386 | { | 457 | { |
| 387 | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); | 458 | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| 388 | response.setCharacterEncoding("utf-8"); | 459 | response.setCharacterEncoding("utf-8"); |
| 389 | - this.init(list, sheetName, Type.EXPORT); | 460 | + this.init(list, sheetName, title, Type.EXPORT); |
| 390 | exportExcel(response.getOutputStream()); | 461 | exportExcel(response.getOutputStream()); |
| 391 | } | 462 | } |
| 392 | 463 | ||
| @@ -398,7 +469,19 @@ public class ExcelUtil<T> | @@ -398,7 +469,19 @@ public class ExcelUtil<T> | ||
| 398 | */ | 469 | */ |
| 399 | public AjaxResult importTemplateExcel(String sheetName) | 470 | public AjaxResult importTemplateExcel(String sheetName) |
| 400 | { | 471 | { |
| 401 | - this.init(null, sheetName, Type.IMPORT); | 472 | + return importTemplateExcel(sheetName, StringUtils.EMPTY); |
| 473 | + } | ||
| 474 | + | ||
| 475 | + /** | ||
| 476 | + * 对list数据源将其里面的数据导入到excel表单 | ||
| 477 | + * | ||
| 478 | + * @param sheetName 工作表的名称 | ||
| 479 | + * @param title 标题 | ||
| 480 | + * @return 结果 | ||
| 481 | + */ | ||
| 482 | + public AjaxResult importTemplateExcel(String sheetName, String title) | ||
| 483 | + { | ||
| 484 | + this.init(null, sheetName, title, Type.IMPORT); | ||
| 402 | return exportExcel(); | 485 | return exportExcel(); |
| 403 | } | 486 | } |
| 404 | 487 | ||
| @@ -410,9 +493,21 @@ public class ExcelUtil<T> | @@ -410,9 +493,21 @@ public class ExcelUtil<T> | ||
| 410 | */ | 493 | */ |
| 411 | public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException | 494 | public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException |
| 412 | { | 495 | { |
| 496 | + importTemplateExcel(response, sheetName); | ||
| 497 | + } | ||
| 498 | + | ||
| 499 | + /** | ||
| 500 | + * 对list数据源将其里面的数据导入到excel表单 | ||
| 501 | + * | ||
| 502 | + * @param sheetName 工作表的名称 | ||
| 503 | + * @param title 标题 | ||
| 504 | + * @return 结果 | ||
| 505 | + */ | ||
| 506 | + public void importTemplateExcel(HttpServletResponse response, String sheetName, String title) throws IOException | ||
| 507 | + { | ||
| 413 | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); | 508 | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| 414 | response.setCharacterEncoding("utf-8"); | 509 | response.setCharacterEncoding("utf-8"); |
| 415 | - this.init(null, sheetName, Type.IMPORT); | 510 | + this.init(null, sheetName, title, Type.IMPORT); |
| 416 | exportExcel(response.getOutputStream()); | 511 | exportExcel(response.getOutputStream()); |
| 417 | } | 512 | } |
| 418 | 513 | ||
| @@ -473,13 +568,13 @@ public class ExcelUtil<T> | @@ -473,13 +568,13 @@ public class ExcelUtil<T> | ||
| 473 | public void writeSheet() | 568 | public void writeSheet() |
| 474 | { | 569 | { |
| 475 | // 取出一共有多少个sheet. | 570 | // 取出一共有多少个sheet. |
| 476 | - double sheetNo = Math.ceil(list.size() / sheetSize); | ||
| 477 | - for (int index = 0; index <= sheetNo; index++) | 571 | + int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / sheetSize)); |
| 572 | + for (int index = 0; index < sheetNo; index++) | ||
| 478 | { | 573 | { |
| 479 | createSheet(sheetNo, index); | 574 | createSheet(sheetNo, index); |
| 480 | 575 | ||
| 481 | // 产生一行 | 576 | // 产生一行 |
| 482 | - Row row = sheet.createRow(0); | 577 | + Row row = sheet.createRow(rownum); |
| 483 | int column = 0; | 578 | int column = 0; |
| 484 | // 写入各个字段的列头名称 | 579 | // 写入各个字段的列头名称 |
| 485 | for (Object[] os : fields) | 580 | for (Object[] os : fields) |
| @@ -507,7 +602,7 @@ public class ExcelUtil<T> | @@ -507,7 +602,7 @@ public class ExcelUtil<T> | ||
| 507 | int endNo = Math.min(startNo + sheetSize, list.size()); | 602 | int endNo = Math.min(startNo + sheetSize, list.size()); |
| 508 | for (int i = startNo; i < endNo; i++) | 603 | for (int i = startNo; i < endNo; i++) |
| 509 | { | 604 | { |
| 510 | - row = sheet.createRow(i + 1 - startNo); | 605 | + row = sheet.createRow(i + 1 + rownum - startNo); |
| 511 | // 得到导出对象. | 606 | // 得到导出对象. |
| 512 | T vo = (T) list.get(i); | 607 | T vo = (T) list.get(i); |
| 513 | int column = 0; | 608 | int column = 0; |
| @@ -535,6 +630,16 @@ public class ExcelUtil<T> | @@ -535,6 +630,16 @@ public class ExcelUtil<T> | ||
| 535 | CellStyle style = wb.createCellStyle(); | 630 | CellStyle style = wb.createCellStyle(); |
| 536 | style.setAlignment(HorizontalAlignment.CENTER); | 631 | style.setAlignment(HorizontalAlignment.CENTER); |
| 537 | style.setVerticalAlignment(VerticalAlignment.CENTER); | 632 | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| 633 | + Font titleFont = wb.createFont(); | ||
| 634 | + titleFont.setFontName("Arial"); | ||
| 635 | + titleFont.setFontHeightInPoints((short) 16); | ||
| 636 | + titleFont.setBold(true); | ||
| 637 | + style.setFont(titleFont); | ||
| 638 | + styles.put("title", style); | ||
| 639 | + | ||
| 640 | + style = wb.createCellStyle(); | ||
| 641 | + style.setAlignment(HorizontalAlignment.CENTER); | ||
| 642 | + style.setVerticalAlignment(VerticalAlignment.CENTER); | ||
| 538 | style.setBorderRight(BorderStyle.THIN); | 643 | style.setBorderRight(BorderStyle.THIN); |
| 539 | style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); | 644 | style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| 540 | style.setBorderLeft(BorderStyle.THIN); | 645 | style.setBorderLeft(BorderStyle.THIN); |
| @@ -1117,6 +1222,9 @@ public class ExcelUtil<T> | @@ -1117,6 +1222,9 @@ public class ExcelUtil<T> | ||
| 1117 | public void createWorkbook() | 1222 | public void createWorkbook() |
| 1118 | { | 1223 | { |
| 1119 | this.wb = new SXSSFWorkbook(500); | 1224 | this.wb = new SXSSFWorkbook(500); |
| 1225 | + this.sheet = wb.createSheet(); | ||
| 1226 | + wb.setSheetName(0, sheetName); | ||
| 1227 | + this.styles = createStyles(wb); | ||
| 1120 | } | 1228 | } |
| 1121 | 1229 | ||
| 1122 | /** | 1230 | /** |
| @@ -1125,17 +1233,13 @@ public class ExcelUtil<T> | @@ -1125,17 +1233,13 @@ public class ExcelUtil<T> | ||
| 1125 | * @param sheetNo sheet数量 | 1233 | * @param sheetNo sheet数量 |
| 1126 | * @param index 序号 | 1234 | * @param index 序号 |
| 1127 | */ | 1235 | */ |
| 1128 | - public void createSheet(double sheetNo, int index) | 1236 | + public void createSheet(int sheetNo, int index) |
| 1129 | { | 1237 | { |
| 1130 | - this.sheet = wb.createSheet(); | ||
| 1131 | - this.styles = createStyles(wb); | ||
| 1132 | // 设置工作表的名称. | 1238 | // 设置工作表的名称. |
| 1133 | - if (sheetNo == 0) | ||
| 1134 | - { | ||
| 1135 | - wb.setSheetName(index, sheetName); | ||
| 1136 | - } | ||
| 1137 | - else | 1239 | + if (sheetNo > 1 && index > 0) |
| 1138 | { | 1240 | { |
| 1241 | + this.sheet = wb.createSheet(); | ||
| 1242 | + this.createTitle(); | ||
| 1139 | wb.setSheetName(index, sheetName + index); | 1243 | wb.setSheetName(index, sheetName + index); |
| 1140 | } | 1244 | } |
| 1141 | } | 1245 | } |
-
请 注册 或 登录 后发表评论