From aea3de1fb05bb15c47176ebf4994d12cc7f37ae1 Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Mon, 18 Feb 2008 10:45:55 +0000 Subject: [PATCH] FR [ 1894640 ] Report Engine: Excel Export support --- .../print/export/PrintDataExcelExporter.java | 128 ++++++++++++++++++ base/src/org/compiere/print/ReportEngine.java | 13 ++ client/src/org/compiere/print/Viewer.java | 46 ++++--- migration/331b-trunk/105_FR1894640.sql | 9 ++ .../331b-trunk/postgresql/105_FR1894640.sql | 10 ++ 5 files changed, 189 insertions(+), 17 deletions(-) create mode 100644 base/src/org/adempiere/print/export/PrintDataExcelExporter.java create mode 100644 migration/331b-trunk/105_FR1894640.sql create mode 100644 migration/331b-trunk/postgresql/105_FR1894640.sql diff --git a/base/src/org/adempiere/print/export/PrintDataExcelExporter.java b/base/src/org/adempiere/print/export/PrintDataExcelExporter.java new file mode 100644 index 0000000000..687f74b867 --- /dev/null +++ b/base/src/org/adempiere/print/export/PrintDataExcelExporter.java @@ -0,0 +1,128 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 2008 SC ARHIPAC SERVICE SRL. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + *****************************************************************************/ +package org.adempiere.print.export; + +import java.sql.Timestamp; + +import org.adempiere.impexp.AbstractExcelExporter; +import org.compiere.print.MPrintFormat; +import org.compiere.print.MPrintFormatItem; +import org.compiere.print.PrintData; +import org.compiere.print.PrintDataElement; + +/** + * Export PrintData to Excel (XLS) file + * @author Teo Sarca, SC ARHIPAC SERVICE SRL + */ +public class PrintDataExcelExporter +extends AbstractExcelExporter +{ + private PrintData m_printData; + private MPrintFormat m_printFormat; + + public PrintDataExcelExporter(PrintData printData, MPrintFormat printFormat) { + super(); + this.m_printData = printData; + this.m_printFormat = printFormat; + } + + @Override + protected int getColumnCount() { + return m_printFormat.getItemCount(); + } + + private PrintDataElement getPDE(int row, int col) { + if (m_printData.getRowIndex() != row) + m_printData.setRowIndex(row); + // + MPrintFormatItem item = m_printFormat.getItem(col); + int AD_Column_ID = item.getAD_Column_ID(); + Object obj = null; + if (AD_Column_ID > 0) + obj = m_printData.getNode(Integer.valueOf(AD_Column_ID)); + if (obj != null && obj instanceof PrintDataElement) { + return (PrintDataElement)obj; + } + return null; + } + @Override + protected int getDisplayType(int row, int col) { + PrintDataElement pde = getPDE(row, col); + if (pde != null) { + return pde.getDisplayType(); + } + return -1; + // + } + + @Override + protected Object getValueAt(int row, int col) { + PrintDataElement pde = getPDE(row, col); + Object value = null; + if (pde == null) + ; + else if (pde.isDate()) { + value = (Timestamp)pde.getValue(); + } + else if (pde.isNumeric()) { + Object o = pde.getValue(); + if (o instanceof Number) { + value = ((Number)o).doubleValue(); + } + } + else if (pde.isYesNo()) { + value = pde.getValue(); + } + else if (pde.isPKey()) { + value = pde.getValueAsString(); + } + else { + value = pde.getValueDisplay(getLanguage()); + } + // + return value; + } + + @Override + protected String getHeaderName(int col) { + return m_printFormat.getItem(col).getPrintName(getLanguage()); + } + + @Override + protected int getRowCount() { + return m_printData.getRowCount(); + } + + @Override + protected boolean isColumnPrinted(int col) { + MPrintFormatItem item = m_printFormat.getItem(col); + return item.isPrinted(); + } + + @Override + protected boolean isPageBreak(int row, int col) { + PrintDataElement pde = getPDE(row, col); + return pde != null ? pde.isPageBreak() : false; + } + + @Override + protected void setCurrentRow(int row) { + m_printData.setRowIndex(row); + } + + @Override + protected boolean isFunctionRow() { + return m_printData.isFunctionRow(); + } +} diff --git a/base/src/org/compiere/print/ReportEngine.java b/base/src/org/compiere/print/ReportEngine.java index a9cc426b0d..89c4a3f87b 100644 --- a/base/src/org/compiere/print/ReportEngine.java +++ b/base/src/org/compiere/print/ReportEngine.java @@ -30,6 +30,7 @@ import javax.xml.transform.stream.*; import org.apache.ecs.*; import org.apache.ecs.xhtml.*; import org.compiere.model.*; +import org.adempiere.print.export.PrintDataExcelExporter; import org.compiere.print.layout.*; import org.compiere.process.*; import org.compiere.util.*; @@ -837,6 +838,18 @@ queued-job-count = 0 (class javax.print.attribute.standard.QueuedJobCount) return false; } // createPS + /** + * Create Excel file + * @param outFile output file + * @param language + * @throws Exception if error + */ + public void createXLS(File outFile, Language language) + throws Exception + { + PrintDataExcelExporter exp = new PrintDataExcelExporter(getPrintData(), getPrintFormat()); + exp.export(outFile, language); + } /************************************************************************** * Get Report Engine for process info diff --git a/client/src/org/compiere/print/Viewer.java b/client/src/org/compiere/print/Viewer.java index 93413ed989..9e00ec864b 100644 --- a/client/src/org/compiere/print/Viewer.java +++ b/client/src/org/compiere/print/Viewer.java @@ -44,7 +44,9 @@ import org.adempiere.pdf.*; * Colin Rooney 2007/03/20 RFE#1670185 & BUG#1684142 * Extend security to Info queries * - * @author Teo Sarca, SC ARHIPAC SERVICE SRL - FR [ 1762466 ] + * @author Teo Sarca, SC ARHIPAC SERVICE SRL + *
  • FR [ 1762466 ] Add "Window" menu to report viewer. + *
  • FR [ 1894640 ] Report Engine: Excel Export support */ public class Viewer extends CFrame implements ActionListener, ChangeListener, WindowStateListener @@ -837,6 +839,7 @@ public class Viewer extends CFrame chooser.addChoosableFileFilter(new ExtensionFileFilter("txt", Msg.getMsg(m_ctx, "FileTXT"))); chooser.addChoosableFileFilter(new ExtensionFileFilter("ssv", Msg.getMsg(m_ctx, "FileSSV"))); chooser.addChoosableFileFilter(new ExtensionFileFilter("csv", Msg.getMsg(m_ctx, "FileCSV"))); + chooser.addChoosableFileFilter(new ExtensionFileFilter("xls", Msg.getMsg(m_ctx, "FileXLS"))); // if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) return; @@ -865,22 +868,31 @@ public class Viewer extends CFrame log.config( "File=" + outFile.getPath() + "; Type=" + ext); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - if (ext.equals("pdf")) - m_reportEngine.createPDF(outFile); - else if (ext.equals("ps")) - m_reportEngine.createPS(outFile); - else if (ext.equals("xml")) - m_reportEngine.createXML(outFile); - else if (ext.equals("csv")) - m_reportEngine.createCSV(outFile, ',', m_reportEngine.getPrintFormat().getLanguage()); - else if (ext.equals("ssv")) - m_reportEngine.createCSV(outFile, ';', m_reportEngine.getPrintFormat().getLanguage()); - else if (ext.equals("txt")) - m_reportEngine.createCSV(outFile, '\t', m_reportEngine.getPrintFormat().getLanguage()); - else if (ext.equals("html") || ext.equals("htm")) - m_reportEngine.createHTML(outFile, false, m_reportEngine.getPrintFormat().getLanguage()); - else - ADialog.error(m_WindowNo, this, "FileInvalidExtension"); + try { + if (ext.equals("pdf")) + m_reportEngine.createPDF(outFile); + else if (ext.equals("ps")) + m_reportEngine.createPS(outFile); + else if (ext.equals("xml")) + m_reportEngine.createXML(outFile); + else if (ext.equals("csv")) + m_reportEngine.createCSV(outFile, ',', m_reportEngine.getPrintFormat().getLanguage()); + else if (ext.equals("ssv")) + m_reportEngine.createCSV(outFile, ';', m_reportEngine.getPrintFormat().getLanguage()); + else if (ext.equals("txt")) + m_reportEngine.createCSV(outFile, '\t', m_reportEngine.getPrintFormat().getLanguage()); + else if (ext.equals("html") || ext.equals("htm")) + m_reportEngine.createHTML(outFile, false, m_reportEngine.getPrintFormat().getLanguage()); + else if (ext.equals("xls")) + m_reportEngine.createXLS(outFile, m_reportEngine.getPrintFormat().getLanguage()); + else + ADialog.error(m_WindowNo, this, "FileInvalidExtension"); + } + catch (Exception e) { + ADialog.error(m_WindowNo, this, "Error", e.getLocalizedMessage()); + if (CLogMgt.isLevelFinest()) + e.printStackTrace(); + } cmd_drill(); // setCursor } // cmd_export diff --git a/migration/331b-trunk/105_FR1894640.sql b/migration/331b-trunk/105_FR1894640.sql new file mode 100644 index 0000000000..4edb96b30f --- /dev/null +++ b/migration/331b-trunk/105_FR1894640.sql @@ -0,0 +1,9 @@ +-- 18.02.2008 12:02:24 EET +-- FR [ 1894640 ] Report Engine: Excel Export support +INSERT INTO AD_Message (AD_Client_ID,AD_Message_ID,AD_Org_ID,Created,CreatedBy,EntityType,IsActive,MsgText,MsgType,Updated,UpdatedBy,Value) VALUES (0,53026,0,TO_DATE('2008-02-18 12:02:11','YYYY-MM-DD HH24:MI:SS'),0,'D','Y','xls - Excel file','E',TO_DATE('2008-02-18 12:02:11','YYYY-MM-DD HH24:MI:SS'),0,'FileXLS') +; + +-- 18.02.2008 12:02:24 EET +-- FR [ 1894640 ] Report Engine: Excel Export support +INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy) SELECT l.AD_Language,t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Message t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Message_ID=53026 AND EXISTS (SELECT * FROM AD_Message_Trl tt WHERE tt.AD_Language!=l.AD_Language OR tt.AD_Message_ID!=t.AD_Message_ID) +; diff --git a/migration/331b-trunk/postgresql/105_FR1894640.sql b/migration/331b-trunk/postgresql/105_FR1894640.sql new file mode 100644 index 0000000000..eea68978d6 --- /dev/null +++ b/migration/331b-trunk/postgresql/105_FR1894640.sql @@ -0,0 +1,10 @@ +-- 18.02.2008 12:02:24 EET +-- FR [ 1894640 ] Report Engine: Excel Export support +INSERT INTO AD_Message (AD_Client_ID,AD_Message_ID,AD_Org_ID,Created,CreatedBy,EntityType,IsActive,MsgText,MsgType,Updated,UpdatedBy,Value) VALUES (0,53026,0,TO_TIMESTAMP('2008-02-18 12:02:11','YYYY-MM-DD HH24:MI:SS'),0,'D','Y','xls - Excel file','E',TO_TIMESTAMP('2008-02-18 12:02:11','YYYY-MM-DD HH24:MI:SS'),0,'FileXLS') +; + +-- 18.02.2008 12:02:24 EET +-- FR [ 1894640 ] Report Engine: Excel Export support +INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy) SELECT l.AD_Language,t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Message t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Message_ID=53026 AND EXISTS (SELECT * FROM AD_Message_Trl tt WHERE tt.AD_Language!=l.AD_Language OR tt.AD_Message_ID!=t.AD_Message_ID) +; +