From 88e97111549ade348709aeb923fda6eb0f5d9079 Mon Sep 17 00:00:00 2001 From: Franta Pribyl Date: Thu, 14 Jan 2016 12:50:07 +0100 Subject: [PATCH] =?UTF-8?q?Implementov=C3=A1n=20obecn=C3=BD=20export=20prv?= =?UTF-8?q?k=C5=AF=20Listbox=20do=20excelu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #261 --- .../isspst/sort/CzechSortListheader.java | 162 +++++++++++++++++- .../WEB-INF/locales/zk-label.properties | 1 + src/main/webapp/img/excel-016.png | Bin 0 -> 765 bytes src/main/webapp/img/excel-032.png | Bin 0 -> 2116 bytes 4 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 src/main/webapp/img/excel-016.png create mode 100644 src/main/webapp/img/excel-032.png diff --git a/src/main/java/info/bukova/isspst/sort/CzechSortListheader.java b/src/main/java/info/bukova/isspst/sort/CzechSortListheader.java index 23496978..c812ae1f 100644 --- a/src/main/java/info/bukova/isspst/sort/CzechSortListheader.java +++ b/src/main/java/info/bukova/isspst/sort/CzechSortListheader.java @@ -1,13 +1,170 @@ package info.bukova.isspst.sort; -import org.zkoss.zul.Listheader; +import info.bukova.isspst.StringUtils; +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.zkoss.zk.au.AuRequest; +import org.zkoss.zk.ui.Component; +import org.zkoss.zk.ui.event.*; +import org.zkoss.zul.*; + +import java.io.ByteArrayOutputStream; +import java.util.List; @SuppressWarnings("serial") public class CzechSortListheader extends Listheader { + static { + addClientEvent(CzechSortListheader.class, Events.ON_RIGHT_CLICK, CE_IMPORTANT | CE_NON_DEFERRABLE); + } + + public void onRightClick() { + Menupopup popup = new Menupopup(); + popup.setPage(this.getPage()); + popup.setWidth("250px"); + this.setContext(popup); + + Menuitem item = new Menuitem(); + item.setLabel(StringUtils.localize("ExportToExcel")); + item.setImage("/img/excel-032.png"); + item.setParent(popup); + + item.addEventListener(Events.ON_CLICK, new EventListener() { + @Override + public void onEvent(org.zkoss.zk.ui.event.Event event) throws Exception { + Listbox listBox = getListbox(); + + if (listBox != null) { + HSSFWorkbook workBook = new HSSFWorkbook(); + HSSFSheet workSheet = workBook.createSheet("export"); + int rowNumber = 0; + Row excelRow = null; + Cell excelCell = null; + + HSSFCellStyle cellStyle = workBook.createCellStyle(); + HSSFFont defaultFont = cellStyle.getFont(workBook); + + HSSFCellStyle cellStyleNumber = workBook.createCellStyle(); + cellStyleNumber.setAlignment(CellStyle.ALIGN_RIGHT); + //http://www.roseindia.net/java/poi/setdataformat.shtml + //HSSFDataFormat dataNumberFormat = workBook.createDataFormat(); + //cellStyleNumber.setDataFormat(dataNumberFormat.getFormat("# ##0,00")); + cellStyleNumber.setDataFormat((short)4); + + HSSFCellStyle cellStyleDate = workBook.createCellStyle(); + cellStyleDate.setAlignment(CellStyle.ALIGN_RIGHT); + //http://www.roseindia.net/java/poi/setdataformat.shtml + HSSFDataFormat dataDateFormat = workBook.createDataFormat(); + cellStyleDate.setDataFormat(dataDateFormat.getFormat("DD. MM. YYYY")); + //cellStyleDate.setDataFormat((short) 15); + + int totalCols = 0; + + Listhead listHead = listBox.getListhead(); + + if (listHead != null) { + HSSFCellStyle cellStyleHeader = workBook.createCellStyle(); + cellStyleHeader.setAlignment(CellStyle.ALIGN_CENTER); + HSSFFont font = workBook.createFont(); + font.setFontName(defaultFont.getFontName()); + font.setFontHeightInPoints(defaultFont.getFontHeightInPoints()); + font.setBold(true); + cellStyleHeader.setFont(font); + + excelRow = workSheet.createRow(rowNumber++); + List headerList = listHead.getChildren(); + totalCols = headerList.size(); + + for (int i = 0; i < totalCols; i++) { + Listheader listHeader = headerList.get(i); + String label = listHeader.getLabel(); + excelCell = excelRow.createCell(i); + excelCell.setCellStyle(cellStyleHeader); + excelCell.setCellValue(label); + } + } + + List itemList = listBox.getItems(); + + for (int i = 0; i < itemList.size(); i++) { + Listitem item = itemList.get(i); + excelRow = workSheet.createRow(rowNumber++); + + List cellList = item.getChildren(); + + for (int k=0; k < cellList.size(); k++) { + Listcell cell = cellList.get(k); + String label = cell.getLabel(); + + HSSFCellStyle cellStyleActual = cellStyle; + excelCell = excelRow.createCell(k); + + if (label != null) { + if (label.isEmpty()) { + //List componentList = cell.getChildren(); + Component component = cell.getFirstChild(); + + if (component != null) { + if (component instanceof Textbox) { + Textbox textBox = (Textbox) component; + label = textBox.getValue(); + } + else if (component instanceof Button) { + Button button = (Button) component; + label = button.getLabel(); + } + } + } + + if (label.matches("^[+-]?[0-9]{1,3}(,?[0-9]{3})*(\\.[0-9]+)?$")) { + // english number -1,234.56 (change browser language) + label = label.replace(",", " "); + label = label.replace(".", ","); + } + + if (label.matches("^[+-]?[0-9]{1,3}([ \\xA0][0-9]{3})*([,][0-9]+)?$")) { + // czech number -1 234,56 + cellStyleActual = cellStyleNumber; + } + else if (label.matches("[0-9]{1,2}[.][ \\xA0]?[0-9]{1,2}[.][ \\xA0]?[0-9]{4}")) { + // czech date + cellStyleActual = cellStyleDate; + } + } + + excelCell.setCellStyle(cellStyleActual); + excelCell.setCellValue(label); + } + } + + for (int i = 0; i < totalCols; i++) { + //int width = workSheet.getColumnWidth(i); + workSheet.autoSizeColumn(i); + int width = workSheet.getColumnWidth(i); + int maxWidth = 8000; + + if (width > maxWidth) { + workSheet.setColumnWidth(i, maxWidth); + } + } + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + workBook.write(os); + Filedownload.save(os.toByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "temp" + Long.toString(System.nanoTime()) + ".xlsx"); +// //Filedownload.save(tempFile, "application/vnd.ms-excel"); + } + } + }); + } + + @Override + public void service(AuRequest request, boolean everError) { + super.service(request, everError); + } @Override public void setSort(String type) { - if (type.startsWith("czech")) { // czech(propertyName) @@ -21,5 +178,4 @@ public class CzechSortListheader extends Listheader { super.setSort(type); } } - } diff --git a/src/main/webapp/WEB-INF/locales/zk-label.properties b/src/main/webapp/WEB-INF/locales/zk-label.properties index c5a76254..de1d93e9 100644 --- a/src/main/webapp/WEB-INF/locales/zk-label.properties +++ b/src/main/webapp/WEB-INF/locales/zk-label.properties @@ -429,5 +429,6 @@ ErrApproveMustBeSigned = Schválení musí být digitálně podepsané. DigitalSignature = Elektronický podpis ContextMenu = Volby v kontextovém menu +ExportToExcel = Exportovat do excelu ErrMaterialOrServiceDescription=Zadejte popis požadavku. diff --git a/src/main/webapp/img/excel-016.png b/src/main/webapp/img/excel-016.png new file mode 100644 index 0000000000000000000000000000000000000000..e97f09839aa45aa75d7d48827eb7e3ff75cd163f GIT binary patch literal 765 zcmVPx#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D02p*dSaefwW^{L9 za%BK;VQFr3E^cLXAT%y8E;(#7eog=Y0&Yn}K~y+TWBC97|G$6#u6#bd`}Nx0uT~#= zv-bGAb*JBN0HPD`)*pVmcJHe-yI!ri`uPk{8Bj4u!@9>SRJV)i?Ut}VA`^64A?1Q< z{$-7V%bMvI)xysxIUkWR+#{jBT^y(ksv-Yuy2=hw<3r*ur)9z}Dx_ait-oW}ecwOx zx?1!lCHK=ZriaDVc8cYnNe65A_dokglG+}Tf@`rGU(Ubs`Pk<_?}5&|`|WJcqp0Tl zk>$5Tqc1qA?-j{9lLXUnHc4%t@SZnYfqXc)_4RE1!?1bJn+t9SZh5&%ZND&38BiRg zA?s|C%6{QpU=8O#A3gJF|F=J%uYWyT{UBn_v(|z;p+F5P`-C##8Zyr&D((~73D(f} zq`c!vAuvF$emProKXk^kw(Q%%n_jL|+7Hx_h^#?y2Ux@07d_oiDuIm4pU>pp4VwD2 zIrCN^P=mrg!Su5UNE-GE?tH!V@4vtEpHGz#(@w)|NIR7%u?Zw3zgJN4fRGXr0xAUx%Ip-B*esZO5@N&u|BYuGwD-!Z z?vWPXBqY9BKw=950lDHE1!cF1nH*AUJl6=-@Z!g_*4t$n=cD{~d-?9df})NDcipLe v`QsT>0}%ZE|L5nwpSaNvV0eJk{r?XDViEU`4zH$l00000NkvXXu0mjfA&hSO literal 0 HcmV?d00001 diff --git a/src/main/webapp/img/excel-032.png b/src/main/webapp/img/excel-032.png new file mode 100644 index 0000000000000000000000000000000000000000..7dba10760171196976958cd10e631a1fcbeba7ea GIT binary patch literal 2116 zcmV-K2)p-*P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D02p*dSaefwW^{L9 za%BK;VQFr3E^cLXAT%y8E;(#7eog=Y2enB=K~zXfwUvoiQ+F1}=PwxELPAK$zCx`? zH%DNq4z$`C+FE2r5OFOtD$a3c&fIg)JLkQ7-}m$G@BV)G{c7p|Y%2hQARG?I!%6KKb4;9R z-|_aa_@Rv6gBN=be${gzwJYLuNBHN)usB2L zF@5N9LwKtBlg6nEkaEg`tpFT^m`>R!|M+PY?O~8-;(- ziH^32KWdeL$Fzu}nni~j1!48VUA4S7E7|^K9hYLh1-TA_uJ-c4uWthuK>p8 zY%eOA+(N2QL4Bc^_Oiy^zta89TIRbAtS}w-P>Ud{P5hBT@>hcw_$LO>5B1{6R#AkG z7t+A_eJyia6@9ao=3Pt^DcyL5)Qu{(b!>r+^|ZqHffKfKm!W;bNO;tt^)Tgrf$+we^W)-{*b=2(IdE(8LsCaHgKa&;=lK7y*BV? z*=W+$f%lJ^M3F|}0X=_DE9dPdW-qcE^LIzjXIakO96T4%vcpU3DkuT~ChO02=Kl=nmvWUjzZmx{!xK8A zVbPYO>lgsaC$h6`=O0)vv`$yvw_eP$oXs3gv(8(R&GBC4xS$y4D*+&L=Ks7z{ZgLF zRiMTsTGvniEdWB_+tNZztoMu_-;Sj1Hi`n8-Ta!U#u;65PrPS2h~NSxF6(=$0swGJ zNI?85fCH9?JcITH`~0GPk<2BC(#ag@*k!5ha@%x`%y#3h_0sLpOJnmRUz+2^Wf)IQ z_HrEnN9D>z1V0-aU4Qoy;YO+oKnE zcWeqadEd2W?CtOmGHl%0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