PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/anozimada/idempiere-smartreport
Java | 560 lines | 426 code | 28 blank | 106 comment | 57 complexity | cca272634349e81e3def153722614272 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. row = sheet.createRow(fila++);
  289. cellStyleD.setWrapText(true);
  290. cellStyleD.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  291. cellStyleD.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
  292. cellStyleD.setBottomBorderColor((short)8);
  293. text = new HSSFRichTextString(rpt.getDescription());
  294. HSSFCell cellD = row.createCell(col+1);
  295. cellD.setCellStyle(cellStyleD);
  296. cellD.setCellValue(text);
  297. newRow = true;
  298. }else if (rpt.getSmj_reportline() != null && rpt.getSmj_reportline().equals("S")) {
  299. // coloca linea en blanco - put empty line
  300. row = sheet.createRow(fila++);
  301. newRow = true;
  302. }else if (rpt.getSmj_hierarchylevel() != null && rpt.getSmj_hierarchylevel() > 0) {
  303. // coloca espacios a la izquierda para simular jeraquia - put
  304. // left spaces to simulate hierarchy
  305. row = sheet.createRow(fila++);
  306. String jerarchy = "";
  307. for (int i = 1; i <= rpt.getSmj_hierarchylevel(); i++) {
  308. jerarchy = jerarchy + " ";
  309. }//for
  310. Region region = new Region(fila-1,(short)0,fila-1,endRegion);
  311. sheet.addMergedRegion(region);
  312. text = new HSSFRichTextString(jerarchy+rpt.getDescription());
  313. HSSFCell cellJ = row.createCell(col);
  314. cellJ.setCellValue(text);
  315. newRow = true;
  316. }else {
  317. row = sheet.createRow(fila++);
  318. putRow(cellStyle, cellStyleD, cellStyleN, sheet, row, fila, rpt);
  319. }//else
  320. }// while itData
  321. }// reportTable
  322. /**
  323. * pone la linea de informacion en el XLS
  324. * put information line into XLS
  325. * @param cellStyle
  326. * @param cellStyleD
  327. * @param cellStyleN
  328. * @param sheet
  329. * @param row
  330. * @param fila
  331. * @param rpt
  332. */
  333. private void putRow(HSSFCellStyle cellStyle, HSSFCellStyle cellStyleD, HSSFCellStyle cellStyleN,
  334. HSSFSheet sheet, HSSFRow row, int fila, ReportTO rpt){
  335. HSSFRichTextString text;
  336. short col = 0;
  337. cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  338. //Nombre - Name
  339. text = new HSSFRichTextString(rpt.getName());
  340. HSSFCell cell = row.createCell(col++);
  341. cell.setCellStyle(cellStyleN);
  342. cell.setCellValue(text);
  343. //Descripcion - Description
  344. text = new HSSFRichTextString(rpt.getDescription());
  345. cell.setCellStyle(cellStyleD);
  346. cell = row.createCell(col++);
  347. cell.setCellValue(text);
  348. if (cols >= 3) {
  349. //Col0
  350. text = new HSSFRichTextString(formatValue(rpt.getCol_0()));
  351. cell = row.createCell(col++);
  352. cell.setCellStyle(cellStyle);
  353. cell.setCellValue(text);
  354. }
  355. if (cols >= 4) {
  356. //Col1
  357. text = new HSSFRichTextString(formatValue(rpt.getCol_1()));
  358. cell = row.createCell(col++);
  359. cell.setCellStyle(cellStyle);
  360. cell.setCellValue(text);
  361. }
  362. if (cols >= 5) {
  363. //Col2
  364. text = new HSSFRichTextString(formatValue(rpt.getCol_2()));
  365. cell = row.createCell(col++);
  366. cell.setCellStyle(cellStyle);
  367. cell.setCellValue(text);
  368. }
  369. if (cols >= 6) {
  370. //Col3
  371. text = new HSSFRichTextString(formatValue(rpt.getCol_3()));
  372. cell = row.createCell(col++);
  373. cell.setCellStyle(cellStyle);
  374. cell.setCellValue(text);
  375. }
  376. if (cols >= 7) {
  377. //Col4
  378. text = new HSSFRichTextString(formatValue(rpt.getCol_4()));
  379. cell = row.createCell(col++);
  380. cell.setCellStyle(cellStyle);
  381. cell.setCellValue(text);
  382. }
  383. if (cols >= 8) {
  384. //Col5
  385. text = new HSSFRichTextString(formatValue(rpt.getCol_5()));
  386. cell = row.createCell(col++);
  387. cell.setCellStyle(cellStyle);
  388. cell.setCellValue(text);
  389. }
  390. if (cols >= 9) {
  391. //Col6
  392. text = new HSSFRichTextString(formatValue(rpt.getCol_6()));
  393. cell = row.createCell(col++);
  394. cell.setCellStyle(cellStyle);
  395. cell.setCellValue(text);
  396. }
  397. if (cols >= 10) {
  398. //Col7
  399. text = new HSSFRichTextString(formatValue(rpt.getCol_7()));
  400. cell = row.createCell(col++);
  401. cell.setCellStyle(cellStyle);
  402. cell.setCellValue(text);
  403. }
  404. if (cols >= 11) {
  405. //Col8
  406. text = new HSSFRichTextString(formatValue(rpt.getCol_8()));
  407. cell = row.createCell(col++);
  408. cell.setCellStyle(cellStyle);
  409. cell.setCellValue(text);
  410. }
  411. if (cols >= 12) {
  412. //Col9
  413. text = new HSSFRichTextString(formatValue(rpt.getCol_9()));
  414. cell = row.createCell(col++);
  415. cell.setCellStyle(cellStyle);
  416. cell.setCellValue(text);
  417. }
  418. if (cols >= 13) {
  419. //Col10
  420. text = new HSSFRichTextString(formatValue(rpt.getCol_10()));
  421. cell = row.createCell(col++);
  422. cell.setCellStyle(cellStyle);
  423. cell.setCellValue(text);
  424. }
  425. if (cols >= 14) {
  426. //Col11
  427. text = new HSSFRichTextString(formatValue(rpt.getCol_11()));
  428. cell = row.createCell(col++);
  429. cell.setCellStyle(cellStyle);
  430. cell.setCellValue(text);
  431. }
  432. if (cols >= 15) {
  433. //Col12
  434. text = new HSSFRichTextString(formatValue(rpt.getCol_12()));
  435. cell = row.createCell(col++);
  436. cell.setCellStyle(cellStyle);
  437. cell.setCellValue(text);
  438. }
  439. if (cols >= 16) {
  440. //Col13
  441. text = new HSSFRichTextString(formatValue(rpt.getCol_13()));
  442. cell = row.createCell(col++);
  443. cell.setCellStyle(cellStyle);
  444. cell.setCellValue(text);
  445. }
  446. if (cols >= 17) {
  447. //Col14
  448. text = new HSSFRichTextString(formatValue(rpt.getCol_14()));
  449. cell = row.createCell(col++);
  450. cell.setCellStyle(cellStyle);
  451. cell.setCellValue(text);
  452. }
  453. if (cols >= 18) {
  454. //Col15
  455. text = new HSSFRichTextString(formatValue(rpt.getCol_15()));
  456. cell = row.createCell(col++);
  457. cell.setCellStyle(cellStyle);
  458. cell.setCellValue(text);
  459. }
  460. if (cols >= 19) {
  461. //Col16
  462. text = new HSSFRichTextString(formatValue(rpt.getCol_16()));
  463. cell = row.createCell(col++);
  464. cell.setCellStyle(cellStyle);
  465. cell.setCellValue(text);
  466. }
  467. if (cols >= 20) {
  468. //Col17
  469. text = new HSSFRichTextString(formatValue(rpt.getCol_17()));
  470. cell = row.createCell(col++);
  471. cell.setCellStyle(cellStyle);
  472. cell.setCellValue(text);
  473. }
  474. if (cols >= 21) {
  475. //Col18
  476. text = new HSSFRichTextString(formatValue(rpt.getCol_18()));
  477. cell = row.createCell(col++);
  478. cell.setCellStyle(cellStyle);
  479. cell.setCellValue(text);
  480. }
  481. if (cols >= 22) {
  482. //Col19
  483. text = new HSSFRichTextString(formatValue(rpt.getCol_19()));
  484. cell = row.createCell(col++);
  485. cell.setCellStyle(cellStyle);
  486. cell.setCellValue(text);
  487. }
  488. if (cols >= 23) {
  489. //Col20
  490. text = new HSSFRichTextString(formatValue(rpt.getCol_20()));
  491. cell = row.createCell(col++);
  492. cell.setCellStyle(cellStyle);
  493. cell.setCellValue(text);
  494. }
  495. }//putRow
  496. /**
  497. * Crea el archivo PDF a partir de un Byte[] ** Create PDF File from a Byte[]
  498. * @param wb
  499. * @param generalTitle
  500. * @return File
  501. */
  502. public File tofile(HSSFWorkbook wb, String[] generalTitle) {
  503. File file = new File(generalTitle[0]+".xls");
  504. FileOutputStream fos;
  505. try {
  506. fos = new FileOutputStream(file);
  507. // fos.write(bytes);
  508. wb.write(fos);
  509. fos.flush();
  510. fos.close();
  511. } catch (FileNotFoundException e) {
  512. e.printStackTrace();
  513. } catch (IOException e) {
  514. e.printStackTrace();
  515. }
  516. return file;
  517. }// tofile
  518. /**
  519. * regresa el valor formateado a dos caracteres
  520. * returns value format 2 characters
  521. * @param data
  522. * @return String
  523. */
  524. private String formatValue(BigDecimal data) {
  525. if (data == null)
  526. return "";
  527. else{
  528. DecimalFormat frm = new DecimalFormat("###,###,###,##0.00");
  529. return frm.format(data.setScale(2));
  530. }
  531. }// formatValue
  532. }// SmjXlsReport