6a06280421418a3fb77aae9a441c78c2d834a6c6.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package cn.com.goldenwater.dcproj.utils.expExcel;
  2. import cn.com.goldenwater.dcproj.utils.StringUtils;
  3. import org.apache.poi.hssf.usermodel.*;
  4. import org.apache.poi.hssf.util.HSSFColor;
  5. import org.apache.poi.ss.usermodel.*;
  6. import org.apache.poi.ss.util.CellRangeAddress;
  7. import org.apache.tools.zip.ZipEntry;
  8. import org.apache.tools.zip.ZipOutputStream;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.OutputStream;
  11. import java.net.URLEncoder;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.zip.Deflater;
  15. /**
  16. * Created by lhc on 2019-3-18.
  17. */
  18. public class ExcelExport extends ExportAbstract {
  19. public OutputStream out;
  20. private HSSFWorkbook wb = null;
  21. private HSSFCellStyle style = null;
  22. HSSFSheet downloadSheet;
  23. HSSFCellStyle headStyle;
  24. HSSFCellStyle dataStyle;
  25. HSSFCellStyle rowStyle;
  26. HSSFDataFormat dataFormat;
  27. HSSFFont headFont;
  28. HSSFFont dataFont;
  29. HSSFFont rowFont;
  30. HSSFRow row = null;
  31. HSSFCell cell = null;
  32. int startsheet = -1;
  33. int maxline = 50000; //一个Sheet最大数据行数 0000
  34. public int rowcount = 0;
  35. private ZipOutputStream zipOut = null;
  36. private String headType = "meta";
  37. @Override
  38. public void Export(HttpServletResponse response) throws Exception {
  39. response.reset();
  40. String _fileName = fileName + (export_bzip ? ".zip" : ".xls");
  41. response.setContentType("application/octet-stream;charset=utf-8");
  42. response.setHeader("Content-Disposition", "attachment; filename="
  43. + URLEncoder.encode(_fileName, "UTF-8"));
  44. }
  45. @Override
  46. public HttpServletResponse Export_new(HttpServletResponse response) throws Exception {
  47. response.reset();
  48. String _fileName = fileName + (export_bzip ? ".zip" : ".xls");
  49. response.setContentType("application/octet-stream;charset=utf-8");
  50. response.setHeader("Content-Disposition", "attachment;filename="
  51. + URLEncoder.encode(_fileName, "UTF-8"));
  52. return response;
  53. }
  54. @Override
  55. public void ExportHeadForCustom(HttpServletResponse response) throws Exception {
  56. headType = "meta";
  57. out = response.getOutputStream();
  58. if (this.export_bzip) {
  59. zipOut = new ZipOutputStream(out);
  60. zipOut.setEncoding("GBK");
  61. zipOut.setMethod(ZipOutputStream.DEFLATED);
  62. zipOut.setLevel(Deflater.BEST_COMPRESSION);
  63. ZipEntry entry = new ZipEntry(this.fileName + "_" + (startsheet + 2) + ".xls");
  64. zipOut.putNextEntry(entry);
  65. }
  66. cE();
  67. if (StringUtils.isNotBlank(title)){
  68. toHeadRowForCustom();
  69. }
  70. }
  71. @Override
  72. public void EndExport() throws Exception {
  73. if (this.export_bzip) {
  74. wb.write(zipOut);
  75. zipOut.flush();
  76. zipOut.finish();
  77. zipOut = null;
  78. } else {
  79. wb.write(out);
  80. }
  81. out.flush();
  82. }
  83. @Override
  84. public HSSFWorkbook getWb() {
  85. return wb;
  86. }
  87. @Override
  88. public void ContinueExport(List list) throws Exception {
  89. java.util.Iterator it = list.iterator();
  90. while (it.hasNext()) {
  91. if (rowcount % maxline == 0) {
  92. if (startsheet != -1) {
  93. if (this.export_bzip) {
  94. wb.write(zipOut);
  95. zipOut.flush();
  96. out.flush();
  97. ZipEntry entry = new ZipEntry(this.fileName + "_" + (startsheet + 2) + ".xls");
  98. zipOut.putNextEntry(entry);
  99. cE();
  100. wb.setSheetName(0, this.fileName + "_" + (startsheet + 2));
  101. } else {
  102. downloadSheet = wb.createSheet();
  103. wb.setSheetName(startsheet + 1, this.fileName + "_" + (startsheet + 2));
  104. }
  105. toHeadRow(list);
  106. }
  107. startsheet++;
  108. }
  109. row = downloadSheet.createRow((rowcount % maxline + this.level + 1));
  110. toTextRow((Map) it.next());
  111. rowcount++;
  112. }
  113. }
  114. public void toTextRow(Map hm) {
  115. cell = row.createCell(0);
  116. cell.setCellValue(rowcount + 1);
  117. int _count = 0;
  118. for (int i = 0; i < this.cols.size(); i++) {
  119. /**
  120. * 这里应该有问题
  121. */
  122. String key = "";
  123. String v = null;
  124. key = this.cols.get(i).toString();
  125. key = key.substring(0, key.indexOf("["));
  126. if (hm.get(key) != null) {
  127. v = hm.get(key).toString();
  128. } else {
  129. v = "";
  130. }
  131. cell = row.createCell((i + 1));
  132. Boolean isNum = false;//data是否为数值型
  133. Boolean isInteger = false;//data是否为整数
  134. Boolean isPercent = false;//data是否为百分数
  135. if (v != null || "".equals(v)) {
  136. //判断data是否为数值型
  137. isNum = v.toString().matches("^(-?\\d+)(\\.\\d+)?$");
  138. //判断data是否为整数(小数部分是否为0)
  139. isInteger = v.toString().matches("^[-\\+]?[\\d]*$");
  140. //判断data是否为百分数(是否包含“%”)
  141. isPercent = v.toString().contains("%");
  142. }//如果单元格内容是数值类型,涉及到金钱(金额、本、利),则设置cell的类型为数值型,设置data的类型为数值类型
  143. if (isNum && !isPercent) {
  144. HSSFDataFormat df = wb.createDataFormat(); // 此处设置数据格式
  145. if (isInteger) {
  146. //style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,#0"));//数据格式只显示整数(这个可能不好使)
  147. style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));//数据格式只显示整数
  148. cell.setCellType(CellType.STRING);
  149. } else {
  150. style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));//保留两位小数点
  151. }
  152. // 设置单元格格式
  153. cell.setCellStyle(style);
  154. // 设置单元格内容为double类型
  155. cell.setCellValue(Double.parseDouble(v.toString()));
  156. } else {
  157. cell.setCellStyle(style);
  158. // 设置单元格内容为字符型
  159. cell.setCellValue(v.toString());
  160. }
  161. /**
  162. * style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  163. * cell.setCellType(cell.CELL_TYPE_STRING);
  164. */
  165. }
  166. }
  167. public void toHeadRow(List data) {
  168. row = downloadSheet.createRow(0);
  169. row.setHeightInPoints((short) 30);
  170. //合并单元格式 合并区域是分别指定起始和终了的列和行,都是从0开始
  171. int colssize = this.cols.size() + 1;
  172. cell = row.createCell((0));
  173. //// 合并单元格(startRow,endRow,startColumn,endColumn)
  174. downloadSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, colssize - 1));
  175. HSSFCellStyle style = wb.createCellStyle();
  176. HSSFFont titleFont = wb.createFont();
  177. titleFont.setFontHeightInPoints((short) 24);
  178. // titleFont.setBoldweight((short) 500);
  179. titleFont.setBold(true);
  180. style.setFont(titleFont);
  181. style.setAlignment(HorizontalAlignment.CENTER);
  182. style.setVerticalAlignment(VerticalAlignment.CENTER);
  183. style.setFillForegroundColor((short) 11);
  184. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  185. cell.setCellValue(this.title);
  186. cell.setCellType(CellType.STRING);
  187. cell.setCellStyle(style);
  188. /**
  189. * 新行
  190. */
  191. row = downloadSheet.createRow(this.level);
  192. HSSFRow _row = downloadSheet.getRow(1);
  193. cell = _row.createCell(0);
  194. //合并单元格(startRow,endRow,startColumn,endColumn)
  195. downloadSheet.addMergedRegion(new CellRangeAddress(1, this.level, 0, 0));
  196. cell.setCellValue("序号");
  197. cell.setCellType(CellType.STRING);
  198. cell.setCellStyle(headStyle);
  199. downloadSheet.setColumnWidth(0, 2400);
  200. for (int i = 0; i < this.cols.size(); i++) {
  201. String m = cols.get(i) == null ? "" : cols.get(i).toString();
  202. if (rowspos[i] != 0) {
  203. HSSFRow _prerow = downloadSheet.getRow(rowspos[i]);
  204. cell = _prerow.getCell((i + 1));
  205. if (cell == null) {
  206. cell = _prerow.createCell((i + 1));
  207. }
  208. } else {
  209. cell = row.createCell((i + 1));
  210. }
  211. cell.setCellValue(m);
  212. cell.setCellType(CellType.STRING);
  213. //set width for columns
  214. //The maximum column width for an individual cell is 255 characters
  215. // //Throws: 判断设置的宽度是否超过最大值 65280
  216. // //java.lang.IllegalArgumentException - if width > 65280 (the maximum column width in Excel)
  217. int columnWidth = (int) ((35.7 + 3) * (MetaUtil.getBestwidth(0, m)));
  218. downloadSheet.setColumnWidth((i + 1), columnWidth >= 65280 ? 65280 : columnWidth);
  219. cell.setCellStyle(headStyle);
  220. }
  221. //冻结第一行 第一列
  222. /**
  223. * 冻结参数......
  224. */
  225. downloadSheet.createFreezePane(this.locksize + 1, this.level + 1);
  226. }
  227. public void toHeadRowForCustom() {
  228. row = downloadSheet.createRow(0);
  229. row.setHeightInPoints((short) 30);
  230. //合并单元格式 合并区域是分别指定起始和终了的列和行,都是从0开始
  231. int colssize = this.cols.size() + 1;
  232. cell = row.createCell((0));
  233. //// 合并单元格(startRow,endRow,startColumn,endColumn)
  234. downloadSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, colssize - 1));
  235. HSSFCellStyle style = wb.createCellStyle();
  236. HSSFFont titleFont = wb.createFont();
  237. titleFont.setFontHeightInPoints((short) 24);
  238. // titleFont.setBoldweight((short) 500);
  239. titleFont.setBold(true);
  240. style.setFont(titleFont);
  241. style.setAlignment(HorizontalAlignment.CENTER);
  242. style.setVerticalAlignment(VerticalAlignment.CENTER);
  243. style.setFillForegroundColor((short) 11);
  244. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  245. cell.setCellValue(this.title);
  246. cell.setCellType(CellType.STRING);
  247. cell.setCellStyle(style);
  248. /**
  249. * 新行
  250. */
  251. row = downloadSheet.createRow(this.level);
  252. HSSFRow _row = downloadSheet.getRow(1);
  253. cell = _row.createCell(0);
  254. //合并单元格(startRow,endRow,startColumn,endColumn)
  255. cell.setCellValue("序号");
  256. cell.setCellType(CellType.STRING);
  257. cell.setCellStyle(headStyle);
  258. downloadSheet.setColumnWidth(0, 2400);
  259. String key = "";
  260. int _count = 0;//用于设置excel的展示列,字段属性为隐藏的列不展示
  261. if(this.cols.size()>0){
  262. for (int i = 0; i < this.cols.size(); i++) {
  263. if (this.cols.get(i) instanceof CellModel) {
  264. key = ((CellModel) this.cols.get(i)).getName();
  265. } else {
  266. key = this.cols.get(i).toString();
  267. if (key!=""||key!=null){
  268. key = key.substring(key.indexOf("[") + 1, key.indexOf("]"));
  269. }
  270. }
  271. _count += 1;
  272. if (rowspos[i] != 0) {
  273. HSSFRow _prerow = downloadSheet.getRow(rowspos[i]);
  274. cell = _prerow.getCell((_count));
  275. if (cell == null) {
  276. cell = _prerow.createCell((_count));
  277. }
  278. } else {
  279. cell = row.createCell((_count));
  280. }
  281. cell.setCellValue(key);
  282. cell.setCellType(CellType.STRING);
  283. downloadSheet.setColumnWidth(_count, 4000);
  284. cell.setCellStyle(headStyle);
  285. }
  286. }
  287. //冻结第一行 第一列
  288. /**
  289. * 冻结参数......
  290. */
  291. downloadSheet.createFreezePane(this.locksize + 1, this.level + 1);
  292. }
  293. private void cE() {
  294. wb = null;
  295. wb = new HSSFWorkbook();
  296. HSSFPalette palette = wb.getCustomPalette(); //wb HSSFWorkbook对象
  297. palette.setColorAtIndex((short) 11, (byte) (228), (byte) (228), (byte) (228));//#E4E4E4
  298. style = wb.createCellStyle();
  299. downloadSheet = wb.createSheet();
  300. headStyle = wb.createCellStyle();
  301. dataStyle = wb.createCellStyle();
  302. rowStyle = wb.createCellStyle();
  303. dataFormat = wb.createDataFormat();
  304. headFont = wb.createFont();
  305. dataFont = wb.createFont();
  306. rowFont = wb.createFont();
  307. // headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  308. // headFont.setBoldweight((short) 700);
  309. headFont.setBold(true);
  310. dataFont.setFontHeightInPoints((short) 10);
  311. dataFont.setColor(IndexedColors.LIGHT_BLUE.index);
  312. // dataFont.setBoldweight((short) 500);
  313. dataFont.setBold(true);
  314. rowFont.setColor(IndexedColors.DARK_RED.index);
  315. rowFont.setFontHeightInPoints((short) 12);
  316. headStyle.setFont(headFont);
  317. headStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  318. headStyle.setAlignment(HorizontalAlignment.CENTER);
  319. headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  320. headStyle.setFillForegroundColor((short) 11);
  321. headStyle.setLocked(true);
  322. dataStyle.setFont(dataFont);
  323. dataStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  324. dataStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  325. dataStyle.setFillForegroundColor((short) 11);
  326. dataStyle.setBorderBottom(BorderStyle.THIN);
  327. dataStyle.setBorderTop(BorderStyle.THIN);
  328. dataStyle.setBorderLeft(BorderStyle.THIN);
  329. dataStyle.setBorderRight(BorderStyle.THIN);
  330. dataStyle.setWrapText(true);
  331. dataStyle.setLocked(true);
  332. rowStyle.setFont(rowFont);
  333. rowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  334. rowStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index);
  335. rowStyle.setBorderLeft(BorderStyle.THICK);
  336. rowStyle.setBorderBottom(BorderStyle.THIN);
  337. }
  338. }