8a5a0f469adef4afaba5e55a87f83af3e85855cc.svn-base 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package cn.com.goldenwater.dcproj.utils.export;
  2. import org.apache.commons.collections.MapUtils;
  3. import org.apache.poi.hssf.usermodel.*;
  4. import org.apache.poi.hssf.util.HSSFColor;
  5. import org.apache.poi.ss.usermodel.*;
  6. import java.io.OutputStream;
  7. import java.util.Map;
  8. import java.util.Set;
  9. public class ExcelExport extends ExportAbstract {
  10. private HSSFWorkbook wb = null;
  11. HSSFSheet downloadSheet;
  12. HSSFCellStyle headStyle;
  13. HSSFCellStyle dataStyle;
  14. HSSFCellStyle rowStyle;
  15. HSSFDataFormat dataFormat;
  16. HSSFFont headFont;
  17. HSSFFont dataFont;
  18. HSSFFont rowFont;
  19. HSSFRow row = null;
  20. HSSFCell cell = null;
  21. int startsheet = -1;
  22. int maxline = 50000; //一个Sheet最大数据行数
  23. // private ZipOutputStream zipOut = null;
  24. private void cE() {
  25. wb = null;
  26. wb = new HSSFWorkbook();
  27. downloadSheet = wb.createSheet();
  28. headStyle = wb.createCellStyle();
  29. dataStyle = wb.createCellStyle();
  30. rowStyle = wb.createCellStyle();
  31. dataFormat = wb.createDataFormat();
  32. headFont = wb.createFont();
  33. dataFont = wb.createFont();
  34. rowFont = wb.createFont();
  35. // headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  36. headFont.setBold(true);
  37. dataFont.setFontHeightInPoints((short) 10);
  38. dataFont.setColor(IndexedColors.LIGHT_BLUE.index);
  39. // dataFont.setBoldweight((short) 500);
  40. dataFont.setBold(true);
  41. rowFont.setColor(IndexedColors.DARK_RED.index);
  42. rowFont.setFontHeightInPoints((short) 12);
  43. headStyle.setFont(headFont);
  44. headStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  45. headStyle.setAlignment(HorizontalAlignment.CENTER);
  46. headStyle.setLocked(true);
  47. dataStyle.setFont(dataFont);
  48. dataStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  49. dataStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  50. dataStyle.setBorderBottom(BorderStyle.THIN);
  51. dataStyle.setBorderTop(BorderStyle.THIN);
  52. dataStyle.setBorderLeft(BorderStyle.THIN);
  53. dataStyle.setBorderRight(BorderStyle.THIN);
  54. dataStyle.setWrapText(true);
  55. dataStyle.setLocked(true);
  56. rowStyle.setFont(rowFont);
  57. rowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  58. rowStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index);
  59. rowStyle.setBorderLeft(BorderStyle.THICK);
  60. rowStyle.setBorderBottom(BorderStyle.THIN);
  61. }
  62. public ExcelExport() {
  63. wb = new HSSFWorkbook();
  64. downloadSheet = wb.createSheet();
  65. headStyle = wb.createCellStyle();
  66. dataStyle = wb.createCellStyle();
  67. rowStyle = wb.createCellStyle();
  68. dataFormat = wb.createDataFormat();
  69. headFont = wb.createFont();
  70. dataFont = wb.createFont();
  71. rowFont = wb.createFont();
  72. // headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  73. headFont.setBold(true);
  74. dataFont.setFontHeightInPoints((short) 10);
  75. dataFont.setColor(IndexedColors.LIGHT_BLUE.index);
  76. // dataFont.setBoldweight((short) 500);
  77. dataFont.setBold(true);
  78. rowFont.setColor(IndexedColors.DARK_RED.index);
  79. rowFont.setFontHeightInPoints((short) 12);
  80. headStyle.setFont(headFont);
  81. headStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  82. //headStyle.setBorderBottom(headStyle.BORDER_THIN);
  83. //headStyle.setBorderTop(headStyle.BORDER_THICK);
  84. //headStyle.setBorderLeft(headStyle.BORDER_THIN);
  85. //headStyle.setBorderRight(headStyle.BORDER_THIN);
  86. // headStyle.setTopBorderColor(HSSFColor.DARK_YELLOW.index);
  87. // headStyle.setBottomBorderColor(HSSFColor.DARK_BLUE.index);
  88. headStyle.setAlignment(HorizontalAlignment.CENTER);
  89. headStyle.setLocked(true);
  90. dataStyle.setFont(dataFont);
  91. dataStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  92. dataStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  93. dataStyle.setBorderBottom(BorderStyle.THIN);
  94. dataStyle.setBorderTop(BorderStyle.THIN);
  95. dataStyle.setBorderLeft(BorderStyle.THIN);
  96. dataStyle.setBorderRight(BorderStyle.THIN);
  97. dataStyle.setWrapText(true);
  98. dataStyle.setLocked(true);
  99. rowStyle.setFont(rowFont);
  100. rowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  101. rowStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index);
  102. rowStyle.setBorderLeft(BorderStyle.THICK);
  103. rowStyle.setBorderBottom(BorderStyle.THIN);
  104. }
  105. @Override
  106. public void ExportHead(OutputStream _out, java.util.Collection co) throws Exception {
  107. // Auto-generated method stub
  108. out = _out;
  109. wb.setSheetName(startsheet + 1, this.fileName + "_" + (startsheet + 2));
  110. row = downloadSheet.createRow(0);
  111. row.setHeight((short) 0x160);
  112. //冻结第一行 第一列
  113. downloadSheet.createFreezePane(1, 1, 1, 1);
  114. toHeadRow();
  115. }
  116. public void toHeadRow() {
  117. short k = 0;
  118. cell = row.createCell((0));
  119. cell.setCellValue("序号");
  120. cell.setCellType(CellType.STRING);
  121. cell.setCellStyle(headStyle);
  122. downloadSheet.setColumnWidth(0, 2400);
  123. for (int i = 0; i < rSearchfields.size(); i++) {
  124. String obj = (String) rSearchfields.get(i);
  125. //这里需要进行可读性质转换
  126. if (!index_h.containsKey(obj)) {
  127. k++;
  128. continue;
  129. }
  130. String head = "";
  131. if (!bdata) {
  132. head = obj;
  133. } else {
  134. head = loadHeadValue(obj);
  135. }
  136. cell = row.createCell((i - k + 1));
  137. cell.setCellValue(head);
  138. cell.setCellType(CellType.STRING);
  139. int columnWidth = (int) ((35.7 + 3) * ((Column) index_h.get(obj)).getGridwidth());
  140. downloadSheet.setColumnWidth((i - k + 1), columnWidth >= 65280 ? 65280 : columnWidth);
  141. cell.setCellStyle(headStyle);
  142. // downloadSheet.setColumnWidth(cj, (short) (35.7 * col.getWidth()));
  143. }
  144. }
  145. @Override
  146. public void ContinueExport(java.util.Collection co) throws Exception {
  147. java.util.Iterator it = co.iterator();
  148. System.out.println("export:" + rowcount);
  149. while (it.hasNext()) {
  150. Map<String, Object> dbrow = (Map) it.next();
  151. if (rowcount % maxline == 0) {
  152. if (startsheet != -1) {
  153. downloadSheet = wb.createSheet();
  154. wb.setSheetName(startsheet + 1, this.fileName + "_" + (startsheet + 2));//startsheet+1
  155. row = downloadSheet.createRow(0);
  156. row.setHeight((short) 0x160);
  157. //冻结第一行
  158. downloadSheet.createFreezePane(1, 1, 1, 1);
  159. toHeadRow();
  160. }
  161. startsheet++;
  162. }
  163. row = downloadSheet.createRow((rowcount % maxline + 1));
  164. toTextRow(dbrow);
  165. rowcount++;
  166. }
  167. }
  168. public void toTextRow(Map<String, Object> dbrow) {
  169. short k = 0;
  170. cell = row.createCell(0);
  171. cell.setCellValue(rowcount + 1);
  172. Set<String> keySet = dbrow.keySet();
  173. int idx = 0;
  174. for (String key : keySet) {
  175. //这里需要进行可读性质转换
  176. if (!index_h.containsKey(key)) {
  177. idx++;
  178. k++;
  179. continue;
  180. }
  181. String value = MapUtils.getString(dbrow, key);
  182. String _v;
  183. if (!bdata) {
  184. _v = (value == null) ? "" : value;
  185. } else {
  186. _v = loadDicValue(value, key);
  187. }
  188. cell = row.createCell((idx - k + 1));
  189. String type = null;//outField.getType();
  190. if ("NUMBER".equals(type)) {
  191. try {
  192. cell.setCellValue(Double.parseDouble(_v));
  193. } catch (Exception e) {
  194. cell.setCellValue(_v);
  195. }
  196. } else {
  197. cell.setCellValue(_v);
  198. }
  199. idx++;
  200. }
  201. }
  202. public void toTextRow(DBRow dbrow) {
  203. short k = 0;
  204. cell = row.createCell(0);
  205. cell.setCellValue(rowcount + 1);
  206. for (int i = 0; i < dbrow.size(); i++) {
  207. Object obj = dbrow.get(i);
  208. DataField outField = (DataField) obj;
  209. //这里需要进行可读性质转换
  210. if ("ROWNUM_".equals(outField.getFieldID()) || !index_h.containsKey(outField.getFieldID())) {
  211. k++;
  212. continue;
  213. }
  214. String value = (String) outField.getValue();
  215. String _v;
  216. if (!bdata) {
  217. _v = (value == null) ? "" : value;
  218. } else {
  219. _v = loadDicValue(value, outField.getFieldID());
  220. }
  221. cell = row.createCell((i - k + 1));
  222. String type = outField.getType();
  223. if ("NUMBER".equals(type)) {
  224. try {
  225. cell.setCellValue(Double.parseDouble(_v));
  226. } catch (Exception e) {
  227. cell.setCellValue(_v);
  228. }
  229. } else {
  230. cell.setCellValue(_v);
  231. }
  232. }
  233. }
  234. @Override
  235. public void EndExport() throws Exception {
  236. if (index_h != null) {
  237. index_h.clear();
  238. }
  239. wb.write(out);
  240. out.flush();
  241. out.close();
  242. // out.finish();
  243. // out.flush();
  244. }
  245. }