PageRenderTime 78ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/JFOA/core/net/loyin/jFinal/render/excel/ExcelKit.java

https://github.com/loyinsoft/JFOA
Java | 179 lines | 136 code | 9 blank | 34 comment | 28 complexity | 3bc68cbfa582eeae8a0cb0fe016ccf74 MD5 | raw file
Possible License(s): MIT
  1. package net.loyin.jFinal.render.excel;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import java.util.Set;
  6. import org.apache.poi.hssf.usermodel.HSSFCell;
  7. import org.apache.poi.hssf.usermodel.HSSFRow;
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. import org.apache.poi.hssf.util.Region;
  11. import com.jfinal.plugin.activerecord.Model;
  12. import com.jfinal.plugin.activerecord.Record;
  13. /**
  14. * description:
  15. *
  16. * @author liyupeng
  17. *
  18. * 2012-8-17
  19. */
  20. public class ExcelKit {
  21. private final static int MAX_ROWS = 65536;
  22. public final static int HEADER_ROW = 1;
  23. public final static int DEFAULT_CELL_WIDTH = 8000;
  24. /**
  25. *
  26. * @param sheetName sheet名称
  27. * @param cellWidth 设置单元格宽度
  28. * @param headerRow 设置头列占的行数
  29. * @param headers 头列值
  30. * @param columns 列key(即 list<Map<String ,Ojbect>> 中 map的key)
  31. * @param list 数据
  32. * @return
  33. */
  34. @SuppressWarnings("deprecation")
  35. private static HSSFWorkbook export(String sheetName, int cellWidth,
  36. int headerRow, String[] headers, String[] columns,List list,int columnNum, boolean hasHeaders) {
  37. if (sheetName == null || sheetName.isEmpty()) {
  38. sheetName = "new sheet";
  39. }
  40. HSSFWorkbook wb = null;
  41. try {
  42. wb = new HSSFWorkbook();
  43. HSSFSheet sheet = wb.createSheet(sheetName);
  44. HSSFRow row = null;
  45. HSSFCell cell = null;
  46. setCellWidth(sheet, cellWidth, columnNum);
  47. if (hasHeaders) {
  48. row = sheet.createRow(0);
  49. if (headerRow <= 0) {
  50. headerRow = HEADER_ROW;
  51. }
  52. headerRow = Math.min(headerRow, MAX_ROWS);
  53. for (int h = 0, lenH = headers.length; h < lenH; h++) {
  54. Region region = new Region(0, (short) h, (short) headerRow - 1,
  55. (short) h);// 合并从第rowFrom行columnFrom列
  56. sheet.addMergedRegion(region);// 到rowTo行columnTo的区域
  57. // 得到所有区域
  58. sheet.getNumMergedRegions();
  59. sheet.setColumnWidth(h, cellWidth);
  60. cell = row.createCell(h);
  61. cell.setCellValue(headers[h]);
  62. }
  63. }
  64. if (list==null||list.size()==0) {
  65. return wb;
  66. }
  67. for (int i = 0, len = list.size(); i < len; i++) {
  68. row = sheet.createRow(i + headerRow);
  69. Object obj = list.get(i);
  70. if(obj==null){
  71. continue;
  72. }
  73. //obj : record map model
  74. if(obj instanceof Map){
  75. @SuppressWarnings("unchecked")
  76. Map<String,Object> map = (Map<String,Object>)obj;
  77. if(columns.length==0){//未设置显示列,默认全部
  78. Set<String> keys = map.keySet();
  79. int columnIndex=0;
  80. for (String key : keys) {
  81. cell = row.createCell(columnIndex);
  82. cell.setCellValue(map.get(key)+"");
  83. columnIndex++;
  84. }
  85. }else{
  86. for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
  87. cell = row.createCell(j);
  88. cell.setCellValue(map.get(columns[j])+"");
  89. }
  90. }
  91. }else if(obj instanceof Model){
  92. @SuppressWarnings("rawtypes")
  93. Model model = (Model)obj;
  94. Set<Entry<String, Object>> entries = model.getAttrsEntrySet();
  95. if(columns.length==0){//未设置显示列,默认全部
  96. int columnIndex=0;
  97. for (Entry<String, Object> entry : entries) {
  98. cell = row.createCell(columnIndex);
  99. cell.setCellValue(entry.getValue()+"");
  100. columnIndex++;
  101. }
  102. }else{
  103. for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
  104. cell = row.createCell(j);
  105. cell.setCellValue(model.get(columns[j])+"");
  106. }
  107. }
  108. }else if(obj instanceof Record){
  109. Record record = (Record)obj;
  110. Map<String,Object> map = record.getColumns();
  111. if(columns.length==0){//未设置显示列,默认全部
  112. record.getColumns();
  113. Set<String> keys = map.keySet();
  114. int columnIndex=0;
  115. for (String key : keys) {
  116. cell = row.createCell(columnIndex);
  117. cell.setCellValue(record.get(key)+"");
  118. columnIndex++;
  119. }
  120. }else{
  121. for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
  122. cell = row.createCell(j);
  123. cell.setCellValue(map.get(columns[j])+"");
  124. }
  125. }
  126. }
  127. }
  128. } catch (Exception ex) {
  129. ex.printStackTrace();
  130. }
  131. return wb;
  132. }
  133. /**
  134. * 设置单元格宽度
  135. * @param sheet
  136. * @param cellWidth
  137. * @param columnNum
  138. */
  139. private static void setCellWidth(HSSFSheet sheet, int cellWidth, int columnNum){
  140. for (int i = 0; i < columnNum; i++) {
  141. sheet.setColumnWidth(i, cellWidth);
  142. }
  143. }
  144. /**
  145. * @param String sheetName sheet名称
  146. * @param int headerRow 设置头列占的行数
  147. * @param String[] headers 头列值
  148. * @param String[] columns 列key(即 list<Map<String ,Ojbect>> 中 map的key)
  149. * @param List<Map<String, Object>> list 数据
  150. * @param int cellWidth 设置单元格宽度
  151. * @return
  152. */
  153. public static HSSFWorkbook export(String sheetName,int headerRow, String[] headers, String[] columns,
  154. List<Object> list, int cellWidth) {
  155. boolean hasHeaders = false;
  156. int columnNum = 0;
  157. if (headers != null && headers.length >0) {
  158. hasHeaders = true;
  159. columnNum = headers.length;
  160. }
  161. if (columns == null ) {
  162. columns = new String[]{};
  163. }
  164. columnNum = Math.max(columnNum, columns.length);
  165. if (cellWidth <= 0) {
  166. cellWidth = DEFAULT_CELL_WIDTH;
  167. }
  168. return export(sheetName, cellWidth, headerRow, headers, columns, list, columnNum , hasHeaders);
  169. }
  170. }