HglMapExcelView.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package cn.com.lzt.excel.view;
  2. import cn.com.lzt.excel.HGLExcelConstant;
  3. import org.apache.poi.hssf.usermodel.*;
  4. import org.apache.poi.ss.usermodel.*;
  5. import org.apache.poi.ss.util.CellRangeAddressList;
  6. import org.apache.poi.xssf.usermodel.*;
  7. import org.jeecgframework.poi.excel.ExcelExportUtil;
  8. import org.jeecgframework.poi.excel.entity.ExportParams;
  9. import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
  10. import org.jeecgframework.poi.excel.entity.vo.MapExcelConstants;
  11. import org.jeecgframework.poi.excel.view.MiniAbstractExcelView;
  12. import org.springframework.stereotype.Controller;
  13. import javax.servlet.ServletOutputStream;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.util.Collection;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * Map 对象接口
  21. *
  22. */
  23. @SuppressWarnings("unchecked")
  24. @Controller("hglexcelview")
  25. public class HglMapExcelView extends MiniAbstractExcelView {
  26. public HglMapExcelView() {
  27. super();
  28. }
  29. @Override
  30. protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
  31. String codedFileName = "临时文件";
  32. Workbook workbook = ExcelExportUtil.exportExcel((ExportParams) model.get(MapExcelConstants.PARAMS), (List<ExcelExportEntity>) model.get(MapExcelConstants.ENTITY_LIST), (Collection<? extends Map<?, ?>>) model.get(MapExcelConstants.MAP_LIST));
  33. if (model.containsKey(MapExcelConstants.FILE_NAME)) {
  34. codedFileName = (String) model.get(MapExcelConstants.FILE_NAME);
  35. }
  36. if (workbook instanceof HSSFWorkbook) {
  37. codedFileName += HSSF;
  38. } else {
  39. codedFileName += XSSF;
  40. }
  41. if (isIE(request)) {
  42. codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
  43. } else {
  44. codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
  45. }
  46. if (workbook instanceof HSSFWorkbook) {
  47. HSSFSheet sheet = ((HSSFWorkbook)workbook).getSheetAt(0);
  48. sheet.createFreezePane(4, 3);
  49. if(model.get(HGLExcelConstant.VALIDPARAM) != null) {
  50. List<Map<String,Object>> validParamM = (List<Map<String,Object>>)model.get(HGLExcelConstant.VALIDPARAM);
  51. String[] arr = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
  52. int index = 0;
  53. HSSFSheet hiddenSheet = ((HSSFWorkbook)workbook).createSheet("Sheet2");
  54. HSSFRow row = null;
  55. for(Map<String,Object> param :validParamM) {
  56. List<String> paramL = (List<String>)param.get(HGLExcelConstant.VALIDTIONSTRS);
  57. // 准备下拉列表数据
  58. String[] dlData = new String[paramL.size()];
  59. paramL.toArray(dlData);
  60. if(dlData.length<5){ //255以内的下拉
  61. //255以内的下拉,参数分别是:作用的sheet、下拉内容数组、起始行、终止行、起始列、终止列
  62. sheet.addValidationData(setDataValidation(sheet, dlData,
  63. (Integer)param.get(HGLExcelConstant.FIRSTROW),
  64. (Integer)param.get(HGLExcelConstant.ENDROW),
  65. (Integer)param.get(HGLExcelConstant.FIRSTCOL),
  66. (Integer)param.get(HGLExcelConstant.ENDCOL))); //超过255个报错
  67. } else { // 255以上的下拉,即下拉列表元素很多的情况
  68. // 1、设置有效性
  69. String strFormula = "Sheet2!$" + arr[index] + "$1:$" + arr[index] + "$"+dlData.length; // Sheet2第A1到A5000作为下拉列表来源数据
  70. hiddenSheet.setColumnWidth(0, 4000); // 设置每列的列宽
  71. // 设置数据有效性加载在哪个单元格上,参数分别是:从sheet2获取A1到A5000作为一个下拉的数据、起始行、终止行、起始列、终止列
  72. sheet.addValidationData(
  73. SetDataValidation(strFormula, (Integer) param.get(HGLExcelConstant.FIRSTROW),
  74. (Integer) param.get(HGLExcelConstant.ENDROW),
  75. (Integer) param.get(HGLExcelConstant.FIRSTCOL),
  76. (Integer) param.get(HGLExcelConstant.ENDCOL))); // 下拉列表元素很多的情况
  77. // 2、生成sheet2内容
  78. for (int j = 0; j < dlData.length; j++) {
  79. if (index == 0) { // 第1个下拉选项,直接创建行、列
  80. row = hiddenSheet.createRow(j); // 创建数据行
  81. hiddenSheet.setColumnWidth(j, 4000); // 设置每列的列宽
  82. row.createCell(0).setCellValue(dlData[j]); // 设置对应单元格的值
  83. } else { // 非第1个下拉选项
  84. int rowCount = hiddenSheet.getLastRowNum();
  85. // System.out.println("========== LastRowNum =========" + rowCount);
  86. if (j <= rowCount) { // 前面创建过的行,直接获取行,创建列
  87. // 获取行,创建列
  88. hiddenSheet.getRow(j).createCell(index).setCellValue(dlData[j]); // 设置对应单元格的值
  89. } else { // 未创建过的行,直接创建行、创建列
  90. hiddenSheet.setColumnWidth(j, 4000); // 设置每列的列宽
  91. // 创建行、创建列
  92. hiddenSheet.createRow(j).createCell(index).setCellValue(dlData[j]); // 设置对应单元格的值
  93. }
  94. }
  95. }
  96. index++;
  97. }
  98. }
  99. // //将第二个sheet设置为隐藏
  100. workbook.setSheetHidden(1, true);
  101. }
  102. if(model.get(HGLExcelConstant.CHECKPARAM) != null) {
  103. HSSFSheet checkSheet = ((HSSFWorkbook)workbook).createSheet("check");
  104. checkSheet.createRow(0).createCell(0).setCellValue(model.get(HGLExcelConstant.CHECKPARAM).toString());
  105. workbook.setSheetHidden(2, true);
  106. }
  107. response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
  108. ServletOutputStream out = response.getOutputStream();
  109. workbook.write(out);
  110. out.flush();
  111. }else if(workbook instanceof XSSFWorkbook) {
  112. XSSFSheet sheet = ((XSSFWorkbook)workbook).getSheetAt(0);
  113. sheet.createFreezePane(4, 3);
  114. if(model.get(HGLExcelConstant.VALIDPARAM) != null) {
  115. List<Map<String,Object>> validParamM = (List<Map<String,Object>>)model.get(HGLExcelConstant.VALIDPARAM);
  116. String[] arr = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
  117. int index = 0;
  118. XSSFSheet hiddenSheet = ((XSSFWorkbook)workbook).createSheet("Sheet2");
  119. XSSFRow row = null;
  120. for(Map<String,Object> param :validParamM) {
  121. List<String> paramL = (List<String>)param.get(HGLExcelConstant.VALIDTIONSTRS);
  122. // 准备下拉列表数据
  123. String[] dlData = new String[paramL.size()];
  124. paramL.toArray(dlData);
  125. if(dlData.length<5){ //255以内的下拉
  126. //255以内的下拉,参数分别是:作用的sheet、下拉内容数组、起始行、终止行、起始列、终止列
  127. // DataValidation dstDataValidation = helper.createValidation(helper.createExplicitListConstraint(dlData),
  128. // dstAddrList);
  129. sheet.addValidationData(setDataValidation(sheet, dlData,
  130. (Integer)param.get(HGLExcelConstant.FIRSTROW),
  131. (Integer)param.get(HGLExcelConstant.ENDROW),
  132. (Integer)param.get(HGLExcelConstant.FIRSTCOL),
  133. (Integer)param.get(HGLExcelConstant.ENDCOL))); //超过255个报错
  134. } else { // 255以上的下拉,即下拉列表元素很多的情况
  135. // 1、设置有效性
  136. String strFormula = "Sheet2!$" + arr[index] + "$1:$" + arr[index] + "$"+dlData.length; // Sheet2第A1到A5000作为下拉列表来源数据
  137. hiddenSheet.setColumnWidth(0, 4000); // 设置每列的列宽
  138. // 设置数据有效性加载在哪个单元格上,参数分别是:从sheet2获取A1到A5000作为一个下拉的数据、起始行、终止行、起始列、终止列
  139. sheet.addValidationData(
  140. SetDataValidation4XSSF(sheet, strFormula, (Integer) param.get(HGLExcelConstant.FIRSTROW),
  141. (Integer) param.get(HGLExcelConstant.ENDROW),
  142. (Integer) param.get(HGLExcelConstant.FIRSTCOL),
  143. (Integer) param.get(HGLExcelConstant.ENDCOL))); // 下拉列表元素很多的情况
  144. // 2、生成sheet2内容
  145. for (int j = 0; j < dlData.length; j++) {
  146. if (index == 0) { // 第1个下拉选项,直接创建行、列
  147. row = hiddenSheet.createRow(j); // 创建数据行
  148. hiddenSheet.setColumnWidth(j, 4000); // 设置每列的列宽
  149. row.createCell(0).setCellValue(dlData[j]); // 设置对应单元格的值
  150. } else { // 非第1个下拉选项
  151. int rowCount = hiddenSheet.getLastRowNum();
  152. // System.out.println("========== LastRowNum =========" + rowCount);
  153. if (j <= rowCount) { // 前面创建过的行,直接获取行,创建列
  154. // 获取行,创建列
  155. hiddenSheet.getRow(j).createCell(index).setCellValue(dlData[j]); // 设置对应单元格的值
  156. } else { // 未创建过的行,直接创建行、创建列
  157. hiddenSheet.setColumnWidth(j, 4000); // 设置每列的列宽
  158. // 创建行、创建列
  159. hiddenSheet.createRow(j).createCell(index).setCellValue(dlData[j]); // 设置对应单元格的值
  160. }
  161. }
  162. }
  163. index++;
  164. }
  165. }
  166. // //将第二个sheet设置为隐藏
  167. workbook.setSheetHidden(1, true);
  168. }
  169. if(model.get(HGLExcelConstant.CHECKPARAM) != null) {
  170. XSSFSheet checkSheet = ((XSSFWorkbook)workbook).createSheet("check");
  171. checkSheet.createRow(0).createCell(0).setCellValue(model.get(HGLExcelConstant.CHECKPARAM).toString());
  172. workbook.setSheetHidden(2, true);
  173. }
  174. response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
  175. ServletOutputStream out = response.getOutputStream();
  176. workbook.write(out);
  177. out.flush();
  178. }
  179. }
  180. /**
  181. *
  182. * @Title: SetDataValidation
  183. * @Description: 下拉列表元素很多的情况 (255以上的下拉)
  184. * @param @param strFormula
  185. * @param @param firstRow 起始行
  186. * @param @param endRow 终止行
  187. * @param @param firstCol 起始列
  188. * @param @param endCol 终止列
  189. * @param @return
  190. * @return HSSFDataValidation
  191. * @throws
  192. */
  193. private static HSSFDataValidation SetDataValidation(String strFormula,
  194. int firstRow, int endRow, int firstCol, int endCol) {
  195. // 设置数据有效性加载在哪个单元格上。四个参数分别是:起始行、终止行、起始列、终止列
  196. CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
  197. DVConstraint constraint = DVConstraint.createFormulaListConstraint(strFormula);
  198. HSSFDataValidation dataValidation = new HSSFDataValidation(regions,constraint);
  199. dataValidation.createErrorBox("Error", "Error");
  200. dataValidation.createPromptBox("", null);
  201. return dataValidation;
  202. }
  203. private static XSSFDataValidation SetDataValidation4XSSF(Sheet sheet, String strFormula,
  204. int firstRow, int endRow, int firstCol, int endCol) {
  205. DataValidationHelper dvHelper = sheet.getDataValidationHelper();
  206. // 设置数据有效性加载在哪个单元格上。四个参数分别是:起始行、终止行、起始列、终止列
  207. CellRangeAddressList addressList = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
  208. // CTDataValidationImpl ctv = new CTDataValidationImpl(SchemaTypeImpl.);
  209. // dvHelper.createFormulaListConstraint
  210. // DVConstraint constraint = DVConstraint.createFormulaListConstraint(strFormula);
  211. // XSSFDataValidation dataValidation = new XSSFDataValidation(constraint,regions,);
  212. //
  213. // dataValidation.createErrorBox("Error", "Error");
  214. // dataValidation.setSuppressDropDownArrow(true);
  215. // dataValidation.createPromptBox("", null);
  216. //
  217. // return dataValidation;
  218. XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createFormulaListConstraint(strFormula);
  219. XSSFDataValidation validation = (XSSFDataValidation)dvHelper.createValidation(dvConstraint, addressList);
  220. validation.setShowErrorBox(true);
  221. validation.setSuppressDropDownArrow(true);
  222. validation.setShowPromptBox(true);
  223. return validation;
  224. // sheetTracing.addValidationData(validation);
  225. }
  226. /**
  227. *
  228. * @Title: setDataValidation
  229. * @Description: 下拉列表元素不多的情况(255以内的下拉)
  230. * @param @param sheet
  231. * @param @param textList
  232. * @param @param firstRow
  233. * @param @param endRow
  234. * @param @param firstCol
  235. * @param @param endCol
  236. * @param @return
  237. * @return DataValidation
  238. * @throws
  239. */
  240. private static DataValidation setDataValidation(Sheet sheet, String[] textList, int firstRow, int endRow, int firstCol, int endCol) {
  241. DataValidationHelper helper = sheet.getDataValidationHelper();
  242. //加载下拉列表内容
  243. DataValidationConstraint constraint = helper.createExplicitListConstraint(textList);
  244. //DVConstraint constraint = new DVConstraint();
  245. constraint.setExplicitListValues(textList);
  246. //设置数据有效性加载在哪个单元格上。四个参数分别是:起始行、终止行、起始列、终止列
  247. CellRangeAddressList regions = new CellRangeAddressList((short) firstRow, (short) endRow, (short) firstCol, (short) endCol);
  248. //数据有效性对象
  249. DataValidation data_validation = helper.createValidation(constraint, regions);
  250. //DataValidation data_validation = new DataValidation(regions, constraint);
  251. return data_validation;
  252. }
  253. }