PageRenderTime 97ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/srcZk/com/smj/webui/component/SmjXlsReport.java

https://bitbucket.org/axelrob/axelrob-espinoza
Java | 555 lines | 421 code | 28 blank | 106 comment | 57 complexity | d7355e6b4eb5fc3ca49f1cc7b1f65dbd MD5 | raw file
  1. package com.smj.webui.component;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.math.BigDecimal;
  7. import java.text.DecimalFormat;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
  13. import org.apache.poi.hssf.usermodel.HSSFFont;
  14. import org.apache.poi.hssf.usermodel.HSSFPatriarch;
  15. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  16. import org.apache.poi.hssf.usermodel.HSSFRow;
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.hssf.util.Region;
  20. import org.compiere.model.MImage;
  21. import org.compiere.report.MReportColumn;
  22. import org.compiere.util.Env;
  23. import org.compiere.util.Msg;
  24. import com.smj.entity.ReportTO;
  25. /**
  26. * @version <li>SmartJSP: SmjXlsReport.java, 2012/02/15
  27. * <ul TYPE ="circle">
  28. * <li> Crea el XLS de la Tabla T_Report teniendo en cuenta las
  29. * prametrizaciones personalizadas
  30. * <li>Create XLS Report from T_Report Table. It takes custom settings
  31. * for report
  32. * </ul>
  33. * @author Freddy Rodriguez - "SmartJSP" - http://www.smartjsp.com/
  34. *
  35. */
  36. public class SmjXlsReport {
  37. private int cols = 0;
  38. private short endRegion=2;
  39. public HSSFWorkbook generate(LinkedList<ReportTO> data,
  40. String generalTitle[], String clientName, String clientNIT,
  41. String periodName, String currencyName, MReportColumn[] m_columns,
  42. String city, Integer logoId) {
  43. int fila = 0;
  44. HSSFRow row;
  45. cols = m_columns.length + 2;
  46. endRegion = (short) (cols -1);
  47. try {
  48. // create workbook
  49. HSSFWorkbook book = new HSSFWorkbook();
  50. // crea hoja - create sheet
  51. HSSFSheet sheet = book.createSheet(generalTitle[0]);
  52. // crea fuente - Create Font
  53. HSSFFont font = book.createFont();
  54. font.setFontHeightInPoints((short) 13);
  55. font.setFontName(HSSFFont.FONT_ARIAL);
  56. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  57. // estio celda - cell style
  58. HSSFCellStyle cellStyle = book.createCellStyle();
  59. cellStyle.setWrapText(true);
  60. cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  61. cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  62. cellStyle.setFont(font);
  63. // //////////////////////////////////////////////////////////////////////////////////////
  64. // agrega el logo
  65. // add logo
  66. if (logoId > 0) {
  67. MImage mimage = MImage.get(Env.getCtx(), logoId);
  68. byte[] imageData = mimage.getData();
  69. HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
  70. HSSFClientAnchor anchor;
  71. anchor = new HSSFClientAnchor(100,50,200,255,(short)0,0,(short)1,1);
  72. anchor.setAnchorType( 2 );
  73. int pictureIndex = book.addPicture(imageData, HSSFWorkbook.PICTURE_TYPE_PNG );
  74. patriarch.createPicture(anchor, pictureIndex);
  75. for (int i=0;i<5;i++)
  76. row = sheet.createRow(fila++);
  77. }//if Logo report
  78. // Titulo General - general Title
  79. row = sheet.createRow(fila++);
  80. HSSFRichTextString text = new HSSFRichTextString(generalTitle[0]);
  81. HSSFCell cell = row.createCell((short) 0);
  82. cell.setCellStyle(cellStyle);
  83. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  84. cell.setCellValue(text);
  85. Region region = new Region(fila-1,(short)0,fila-1,endRegion);
  86. sheet.addMergedRegion(region);
  87. // empresa - Company
  88. row = sheet.createRow(fila++);
  89. text = new HSSFRichTextString(clientName);
  90. cell = row.createCell((short) 0);
  91. cell.setCellStyle(cellStyle);
  92. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  93. cell.setCellValue(text);
  94. region = new Region(fila-1,(short)0,fila-1,endRegion);
  95. sheet.addMergedRegion(region);
  96. // Ciudad - City
  97. row = sheet.createRow(fila++);
  98. text = new HSSFRichTextString(city);
  99. cell = row.createCell((short) 0);
  100. cell.setCellStyle(cellStyle);
  101. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  102. cell.setCellValue(text);
  103. region = new Region(fila-1,(short)0,fila-1,endRegion);
  104. sheet.addMergedRegion(region);
  105. // NIT
  106. row = sheet.createRow(fila++);
  107. text = new HSSFRichTextString(clientNIT);
  108. cell = row.createCell((short) 0);
  109. cell.setCellStyle(cellStyle);
  110. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  111. cell.setCellValue(text);
  112. region = new Region(fila-1,(short)0,fila-1,endRegion);
  113. sheet.addMergedRegion(region);
  114. // periodo - Period
  115. String pn = "";
  116. if (generalTitle[1]!=null && generalTitle[1].length()>0){
  117. pn = generalTitle[1]+" "+periodName;
  118. }else{
  119. pn = periodName;
  120. }
  121. if (generalTitle[2]!=null && generalTitle[2].length()>0){
  122. pn = pn+" "+generalTitle[2];
  123. }
  124. row = sheet.createRow(fila++);
  125. text = new HSSFRichTextString(pn);
  126. cell = row.createCell((short) 0);
  127. cell.setCellStyle(cellStyle);
  128. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  129. cell.setCellValue(text);
  130. region = new Region(fila-1,(short)0,fila-1,endRegion);
  131. sheet.addMergedRegion(region);
  132. // tipo moneda - currency
  133. row = sheet.createRow(fila++);
  134. text = new HSSFRichTextString(currencyName);
  135. cell = row.createCell((short) 0);
  136. cell.setCellStyle(cellStyle);
  137. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  138. cell.setCellValue(text);
  139. region = new Region(fila-1,(short)0,fila-1,endRegion);
  140. sheet.addMergedRegion(region);
  141. row = sheet.createRow(fila++);
  142. titleTable(book, sheet, fila++, m_columns);
  143. // llena datos del reporte - fill data report
  144. reportTable(book, data, sheet, fila);
  145. return book;
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. return null;
  149. }//try/catch
  150. }// generate
  151. /**
  152. * Crea la fila de titulos - create title row
  153. * @param wb
  154. * @param hs
  155. * @param fila
  156. * @param colsName
  157. */
  158. private void titleTable(HSSFWorkbook book, HSSFSheet sheet, int fila,
  159. MReportColumn[] m_columns) {
  160. short col = 0;
  161. // crea fuente - create font
  162. HSSFFont font = book.createFont();
  163. font.setFontHeightInPoints((short) 13);
  164. font.setFontName(HSSFFont.FONT_ARIAL);
  165. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  166. // font.setColor(HSSFColor.BLUE.index);
  167. // estio celda - cell style
  168. HSSFCellStyle cellStyle = book.createCellStyle();
  169. cellStyle.setWrapText(true);
  170. cellStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
  171. cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  172. // cellStyle.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
  173. // cellStyle.setFillBackgroundColor(HSSFColor.GREY_40_PERCENT.index);
  174. cellStyle.setFont(font);
  175. // //Titulos de la tabla - Table titles
  176. HSSFRow row = sheet.createRow(fila);
  177. // Nombre - name
  178. HSSFRichTextString text = new HSSFRichTextString(Msg.translate(
  179. Env.getCtx(), "name").toUpperCase());
  180. HSSFCell cell = row.createCell(col++);
  181. cell.setCellStyle(cellStyle);
  182. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  183. cell.setCellValue(text);
  184. // Desripcion - description
  185. text = new HSSFRichTextString(Msg
  186. .translate(Env.getCtx(), "description").toUpperCase());
  187. cell = row.createCell(col++);
  188. cell.setCellStyle(cellStyle);
  189. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  190. cell.setCellValue(text);
  191. // columnas de valores - Value Columns
  192. for (MReportColumn mcol:m_columns){
  193. String colName = mcol.getName();
  194. text = new HSSFRichTextString(colName.toUpperCase());
  195. cell = row.createCell(col++);
  196. cell.setCellStyle(cellStyle);
  197. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  198. cell.setCellValue(text);
  199. }//for columnas
  200. }// titleTable
  201. /**
  202. * llena los datos del reporte - fill report data
  203. * @param book
  204. * @param data
  205. * @param sheet
  206. * @param fila
  207. */
  208. public void reportTable(HSSFWorkbook book, LinkedList<ReportTO> data,
  209. HSSFSheet sheet, int fila) {
  210. HSSFRow row;
  211. // crea fuente - create font
  212. HSSFFont font = book.createFont();
  213. font.setFontHeightInPoints((short) 10);
  214. font.setFontName(HSSFFont.FONT_ARIAL);
  215. HSSFRichTextString text;
  216. Iterator<ReportTO> itRep = data.iterator();
  217. Boolean newRow = false;
  218. sheet.setColumnWidth((short)0, (short)(13*256));
  219. sheet.setColumnWidth((short)1, (short)(60*256));
  220. for (int i = 2;i<(cols);i++){
  221. sheet.setColumnWidth((short)i, (short)(15*256));
  222. }//for
  223. // estio celda - cell style
  224. HSSFCellStyle cellStyle = book.createCellStyle();
  225. HSSFCellStyle cellStyleD = book.createCellStyle();
  226. HSSFCellStyle cellStyleN = book.createCellStyle();
  227. while (itRep.hasNext()) {
  228. short col = 0;
  229. ReportTO rpt = itRep.next();
  230. if (!newRow){
  231. cellStyle = book.createCellStyle();
  232. cellStyleD = book.createCellStyle();
  233. cellStyleN = book.createCellStyle();
  234. }//if
  235. newRow = false;
  236. if (rpt.getSmj_reportline() != null && rpt.getSmj_reportline().equals("T")) {
  237. // Coloca titulo - put title
  238. row = sheet.createRow(fila++);
  239. HSSFFont fontT = book.createFont();
  240. fontT.setFontHeightInPoints((short) 12);
  241. fontT.setFontName(HSSFFont.FONT_ARIAL);
  242. fontT.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  243. HSSFCellStyle cellStyleT = book.createCellStyle();
  244. cellStyleT.setWrapText(true);
  245. cellStyleT.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  246. cellStyleT.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  247. cellStyleT.setFont(fontT);
  248. Region region = new Region(fila-1,(short)0,fila-1,endRegion);
  249. sheet.addMergedRegion(region);
  250. text = new HSSFRichTextString(rpt.getDescription());
  251. HSSFCell cellT = row.createCell(col);
  252. cellT.setCellStyle(cellStyleT);
  253. cellT.setCellValue(text);
  254. newRow = true;
  255. } else if (rpt.getSmj_reportline() != null && rpt.getSmj_reportline().equals("L")) {
  256. // coloca linea en el reporte - Put under line in the report
  257. cellStyle.setWrapText(true);
  258. cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  259. cellStyle.setBottomBorderColor((short)8);
  260. cellStyleD.setWrapText(true);
  261. cellStyleD.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  262. cellStyleD.setBottomBorderColor((short)8);
  263. cellStyleN.setWrapText(true);
  264. cellStyleN.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  265. cellStyleN.setBottomBorderColor((short)8);
  266. newRow = true;
  267. } else if (rpt.getSmj_reportline() != null && rpt.getSmj_reportline().equals("X")) {
  268. // coloca linea de total - Put total line
  269. cellStyle.setWrapText(true);
  270. cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  271. cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  272. cellStyle.setBottomBorderColor((short)8);
  273. newRow = true;
  274. }else if (rpt.getSmj_reportline() != null && rpt.getSmj_reportline().equals("Z")) {
  275. // coloca linea doble de total - Put total line doble
  276. cellStyle.setWrapText(true);
  277. cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  278. cellStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
  279. cellStyle.setBottomBorderColor((short)8);
  280. //--------------
  281. row = sheet.createRow(fila++);
  282. ReportTO rptD = new ReportTO();
  283. putRow(cellStyle, cellStyleD, cellStyleN, sheet, row, fila, rptD);
  284. cellStyle = book.createCellStyle();
  285. newRow = true;
  286. }else if (rpt.getSmj_reportline() != null && rpt.getSmj_reportline().equals("D")) {
  287. // coloca liena de descripcion - put description line
  288. cellStyleD.setWrapText(true);
  289. cellStyleD.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  290. cellStyleD.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  291. cellStyleD.setBottomBorderColor((short)8);
  292. newRow = true;
  293. }else if (rpt.getSmj_reportline() != null && rpt.getSmj_reportline().equals("S")) {
  294. // coloca linea en blanco - put empty line
  295. row = sheet.createRow(fila++);
  296. newRow = true;
  297. }else if (rpt.getSmj_hierarchylevel() != null && rpt.getSmj_hierarchylevel() > 0) {
  298. // coloca espacios a la izquierda para simular jeraquia - put
  299. // left spaces to simulate hierarchy
  300. row = sheet.createRow(fila++);
  301. String jerarchy = "";
  302. for (int i = 1; i <= rpt.getSmj_hierarchylevel(); i++) {
  303. jerarchy = jerarchy + " ";
  304. }//for
  305. Region region = new Region(fila-1,(short)0,fila-1,endRegion);
  306. sheet.addMergedRegion(region);
  307. text = new HSSFRichTextString(jerarchy+rpt.getDescription());
  308. HSSFCell cellJ = row.createCell(col);
  309. cellJ.setCellValue(text);
  310. newRow = true;
  311. }else {
  312. row = sheet.createRow(fila++);
  313. putRow(cellStyle, cellStyleD, cellStyleN, sheet, row, fila, rpt);
  314. }//else
  315. }// while itData
  316. }// reportTable
  317. /**
  318. * pone la linea de informacion en el XLS
  319. * put information line into XLS
  320. * @param cellStyle
  321. * @param cellStyleD
  322. * @param cellStyleN
  323. * @param sheet
  324. * @param row
  325. * @param fila
  326. * @param rpt
  327. */
  328. private void putRow(HSSFCellStyle cellStyle, HSSFCellStyle cellStyleD, HSSFCellStyle cellStyleN,
  329. HSSFSheet sheet, HSSFRow row, int fila, ReportTO rpt){
  330. HSSFRichTextString text;
  331. short col = 0;
  332. cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  333. //Nombre - Name
  334. text = new HSSFRichTextString(rpt.getName());
  335. HSSFCell cell = row.createCell(col++);
  336. cell.setCellStyle(cellStyleN);
  337. cell.setCellValue(text);
  338. //Descripcion - Description
  339. text = new HSSFRichTextString(rpt.getDescription());
  340. cell.setCellStyle(cellStyleD);
  341. cell = row.createCell(col++);
  342. cell.setCellValue(text);
  343. if (cols >= 3) {
  344. //Col0
  345. text = new HSSFRichTextString(formatValue(rpt.getCol_0()));
  346. cell = row.createCell(col++);
  347. cell.setCellStyle(cellStyle);
  348. cell.setCellValue(text);
  349. }
  350. if (cols >= 4) {
  351. //Col1
  352. text = new HSSFRichTextString(formatValue(rpt.getCol_1()));
  353. cell = row.createCell(col++);
  354. cell.setCellStyle(cellStyle);
  355. cell.setCellValue(text);
  356. }
  357. if (cols >= 5) {
  358. //Col2
  359. text = new HSSFRichTextString(formatValue(rpt.getCol_2()));
  360. cell = row.createCell(col++);
  361. cell.setCellStyle(cellStyle);
  362. cell.setCellValue(text);
  363. }
  364. if (cols >= 6) {
  365. //Col3
  366. text = new HSSFRichTextString(formatValue(rpt.getCol_3()));
  367. cell = row.createCell(col++);
  368. cell.setCellStyle(cellStyle);
  369. cell.setCellValue(text);
  370. }
  371. if (cols >= 7) {
  372. //Col4
  373. text = new HSSFRichTextString(formatValue(rpt.getCol_4()));
  374. cell = row.createCell(col++);
  375. cell.setCellStyle(cellStyle);
  376. cell.setCellValue(text);
  377. }
  378. if (cols >= 8) {
  379. //Col5
  380. text = new HSSFRichTextString(formatValue(rpt.getCol_5()));
  381. cell = row.createCell(col++);
  382. cell.setCellStyle(cellStyle);
  383. cell.setCellValue(text);
  384. }
  385. if (cols >= 9) {
  386. //Col6
  387. text = new HSSFRichTextString(formatValue(rpt.getCol_6()));
  388. cell = row.createCell(col++);
  389. cell.setCellStyle(cellStyle);
  390. cell.setCellValue(text);
  391. }
  392. if (cols >= 10) {
  393. //Col7
  394. text = new HSSFRichTextString(formatValue(rpt.getCol_7()));
  395. cell = row.createCell(col++);
  396. cell.setCellStyle(cellStyle);
  397. cell.setCellValue(text);
  398. }
  399. if (cols >= 11) {
  400. //Col8
  401. text = new HSSFRichTextString(formatValue(rpt.getCol_8()));
  402. cell = row.createCell(col++);
  403. cell.setCellStyle(cellStyle);
  404. cell.setCellValue(text);
  405. }
  406. if (cols >= 12) {
  407. //Col9
  408. text = new HSSFRichTextString(formatValue(rpt.getCol_9()));
  409. cell = row.createCell(col++);
  410. cell.setCellStyle(cellStyle);
  411. cell.setCellValue(text);
  412. }
  413. if (cols >= 13) {
  414. //Col10
  415. text = new HSSFRichTextString(formatValue(rpt.getCol_10()));
  416. cell = row.createCell(col++);
  417. cell.setCellStyle(cellStyle);
  418. cell.setCellValue(text);
  419. }
  420. if (cols >= 14) {
  421. //Col11
  422. text = new HSSFRichTextString(formatValue(rpt.getCol_11()));
  423. cell = row.createCell(col++);
  424. cell.setCellStyle(cellStyle);
  425. cell.setCellValue(text);
  426. }
  427. if (cols >= 15) {
  428. //Col12
  429. text = new HSSFRichTextString(formatValue(rpt.getCol_12()));
  430. cell = row.createCell(col++);
  431. cell.setCellStyle(cellStyle);
  432. cell.setCellValue(text);
  433. }
  434. if (cols >= 16) {
  435. //Col13
  436. text = new HSSFRichTextString(formatValue(rpt.getCol_13()));
  437. cell = row.createCell(col++);
  438. cell.setCellStyle(cellStyle);
  439. cell.setCellValue(text);
  440. }
  441. if (cols >= 17) {
  442. //Col14
  443. text = new HSSFRichTextString(formatValue(rpt.getCol_14()));
  444. cell = row.createCell(col++);
  445. cell.setCellStyle(cellStyle);
  446. cell.setCellValue(text);
  447. }
  448. if (cols >= 18) {
  449. //Col15
  450. text = new HSSFRichTextString(formatValue(rpt.getCol_15()));
  451. cell = row.createCell(col++);
  452. cell.setCellStyle(cellStyle);
  453. cell.setCellValue(text);
  454. }
  455. if (cols >= 19) {
  456. //Col16
  457. text = new HSSFRichTextString(formatValue(rpt.getCol_16()));
  458. cell = row.createCell(col++);
  459. cell.setCellStyle(cellStyle);
  460. cell.setCellValue(text);
  461. }
  462. if (cols >= 20) {
  463. //Col17
  464. text = new HSSFRichTextString(formatValue(rpt.getCol_17()));
  465. cell = row.createCell(col++);
  466. cell.setCellStyle(cellStyle);
  467. cell.setCellValue(text);
  468. }
  469. if (cols >= 21) {
  470. //Col18
  471. text = new HSSFRichTextString(formatValue(rpt.getCol_18()));
  472. cell = row.createCell(col++);
  473. cell.setCellStyle(cellStyle);
  474. cell.setCellValue(text);
  475. }
  476. if (cols >= 22) {
  477. //Col19
  478. text = new HSSFRichTextString(formatValue(rpt.getCol_19()));
  479. cell = row.createCell(col++);
  480. cell.setCellStyle(cellStyle);
  481. cell.setCellValue(text);
  482. }
  483. if (cols >= 23) {
  484. //Col20
  485. text = new HSSFRichTextString(formatValue(rpt.getCol_20()));
  486. cell = row.createCell(col++);
  487. cell.setCellStyle(cellStyle);
  488. cell.setCellValue(text);
  489. }
  490. }//putRow
  491. /**
  492. * Crea el archivo PDF a partir de un Byte[] ** Create PDF File from a Byte[]
  493. * @param wb
  494. * @param generalTitle
  495. * @return File
  496. */
  497. public File tofile(HSSFWorkbook wb, String[] generalTitle) {
  498. File file = new File(generalTitle[0]+".xls");
  499. FileOutputStream fos;
  500. try {
  501. fos = new FileOutputStream(file);
  502. // fos.write(bytes);
  503. wb.write(fos);
  504. fos.flush();
  505. fos.close();
  506. } catch (FileNotFoundException e) {
  507. e.printStackTrace();
  508. } catch (IOException e) {
  509. e.printStackTrace();
  510. }
  511. return file;
  512. }// tofile
  513. /**
  514. * regresa el valor formateado a dos caracteres
  515. * returns value format 2 characters
  516. * @param data
  517. * @return String
  518. */
  519. private String formatValue(BigDecimal data) {
  520. if (data == null)
  521. return "";
  522. else{
  523. DecimalFormat frm = new DecimalFormat("###,###,###,##0.00");
  524. return frm.format(data.setScale(2));
  525. }
  526. }// formatValue
  527. }// SmjXlsReport