PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/jfinal/ext/render/excel/PoiKit.java

https://github.com/OVERSKY2003/jfinal-ext
Java | 185 lines | 151 code | 18 blank | 16 comment | 29 complexity | 230d586baafb6806b582b91b442110ae MD5 | raw file
  1. /**
  2. * Copyright (c) 2011-2013, kidzhou 周磊 (zhouleib1412@gmail.com)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.jfinal.ext.render.excel;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Map.Entry;
  20. import java.util.Set;
  21. import org.apache.poi.hssf.usermodel.HSSFCell;
  22. import org.apache.poi.hssf.usermodel.HSSFRow;
  23. import org.apache.poi.hssf.usermodel.HSSFSheet;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.hssf.util.Region;
  26. import com.google.common.base.Preconditions;
  27. import com.jfinal.plugin.activerecord.Model;
  28. import com.jfinal.plugin.activerecord.Record;
  29. public class PoiKit {
  30. private static final int HEADER_ROW = 1;
  31. private static final int MAX_ROWS = 65536;
  32. private String sheetName = "new sheet";
  33. private int cellWidth = 8000;
  34. private int headerRow;
  35. private String[] headers = new String[] {};
  36. private String[] columns;
  37. private List<?> data;
  38. public PoiKit(List<?> data) {
  39. this.data = data;
  40. }
  41. public static PoiKit with(List<?> data) {
  42. return new PoiKit(data);
  43. }
  44. public HSSFWorkbook export() {
  45. Preconditions.checkNotNull(headers, "headers can not be null");
  46. Preconditions.checkNotNull(columns, "columns can not be null");
  47. Preconditions.checkArgument(cellWidth >= 0, "cellWidth < 0");
  48. HSSFWorkbook wb = new HSSFWorkbook();
  49. HSSFSheet sheet = wb.createSheet(sheetName);
  50. HSSFRow row = null;
  51. HSSFCell cell = null;
  52. if (headers.length > 0) {
  53. row = sheet.createRow(0);
  54. if (headerRow <= 0) {
  55. headerRow = HEADER_ROW;
  56. }
  57. headerRow = Math.min(headerRow, MAX_ROWS);
  58. for (int h = 0, lenH = headers.length; h < lenH; h++) {
  59. @SuppressWarnings("deprecation")
  60. Region region = new Region(0, (short) h, (short) headerRow - 1, (short) h);// 合并从第rowFrom行columnFrom列
  61. sheet.addMergedRegion(region);// 到rowTo行columnTo的区域
  62. // 得到所有区域
  63. sheet.getNumMergedRegions();
  64. if (cellWidth > 0) {
  65. sheet.setColumnWidth(h, cellWidth);
  66. }
  67. cell = row.createCell(h);
  68. cell.setCellValue(headers[h]);
  69. }
  70. }
  71. if (data.size() == 0) {
  72. return wb;
  73. }
  74. for (int i = 0, len = data.size(); i < len; i++) {
  75. row = sheet.createRow(i + headerRow);
  76. Object obj = data.get(i);
  77. if (obj == null) {
  78. continue;
  79. }
  80. if (obj instanceof Map) {
  81. processAsMap(columns, row, obj);
  82. } else if (obj instanceof Model) {
  83. processAsModel(columns, row, obj);
  84. } else if (obj instanceof Record) {
  85. processAsRecord(columns, row, obj);
  86. }
  87. }
  88. return wb;
  89. }
  90. @SuppressWarnings("unchecked")
  91. private static void processAsMap(String[] columns, HSSFRow row, Object obj) {
  92. HSSFCell cell;
  93. Map<String, Object> map = (Map<String, Object>) obj;
  94. if (columns.length == 0) {// 未设置显示列,默认全部
  95. Set<String> keys = map.keySet();
  96. int columnIndex = 0;
  97. for (String key : keys) {
  98. cell = row.createCell(columnIndex);
  99. cell.setCellValue(map.get(key) + "");
  100. columnIndex++;
  101. }
  102. } else {
  103. for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
  104. cell = row.createCell(j);
  105. cell.setCellValue(map.get(columns[j]) + "");
  106. }
  107. }
  108. }
  109. private static void processAsModel(String[] columns, HSSFRow row, Object obj) {
  110. HSSFCell cell;
  111. Model<?> model = (Model<?>) obj;
  112. Set<Entry<String, Object>> entries = model.getAttrsEntrySet();
  113. if (columns.length == 0) {// 未设置显示列,默认全部
  114. int columnIndex = 0;
  115. for (Entry<String, Object> entry : entries) {
  116. cell = row.createCell(columnIndex);
  117. cell.setCellValue(entry.getValue() + "");
  118. columnIndex++;
  119. }
  120. } else {
  121. for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
  122. cell = row.createCell(j);
  123. cell.setCellValue(model.get(columns[j]) + "");
  124. }
  125. }
  126. }
  127. private static void processAsRecord(String[] columns, HSSFRow row, Object obj) {
  128. HSSFCell cell;
  129. Record record = (Record) obj;
  130. Map<String, Object> map = record.getColumns();
  131. if (columns.length == 0) {// 未设置显示列,默认全部
  132. record.getColumns();
  133. Set<String> keys = map.keySet();
  134. int columnIndex = 0;
  135. for (String key : keys) {
  136. cell = row.createCell(columnIndex);
  137. cell.setCellValue(record.get(key) + "");
  138. columnIndex++;
  139. }
  140. } else {
  141. for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
  142. cell = row.createCell(j);
  143. cell.setCellValue(map.get(columns[j]) + "");
  144. }
  145. }
  146. }
  147. public PoiKit sheetName(String sheetName) {
  148. this.sheetName = sheetName;
  149. return this;
  150. }
  151. public PoiKit cellWidth(int cellWidth) {
  152. this.cellWidth = cellWidth;
  153. return this;
  154. }
  155. public PoiKit headerRow(int headerRow) {
  156. this.headerRow = headerRow;
  157. return this;
  158. }
  159. public PoiKit headers(String[] headers) {
  160. this.headers = headers;
  161. return this;
  162. }
  163. public PoiKit columns(String[] columns) {
  164. this.columns = columns;
  165. return this;
  166. }
  167. }