1bd04a385e7f220e63591e2f9e507af692ee7521.svn-base 14 KB

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