001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.view.save;
029:
030: import java.io.File;
031: import java.text.MessageFormat;
032:
033: import javax.swing.JOptionPane;
034:
035: import net.sf.jasperreports.engine.JRException;
036: import net.sf.jasperreports.engine.JRExporterParameter;
037: import net.sf.jasperreports.engine.JasperPrint;
038: import net.sf.jasperreports.engine.export.JRXlsExporter;
039: import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
040: import net.sf.jasperreports.view.JRSaveContributor;
041:
042: /**
043: * @author Teodor Danciu (teodord@users.sourceforge.net)
044: * @version $Id: JRSingleSheetXlsSaveContributor.java 1480 2006-11-10 16:49:10Z shertage $
045: */
046: public class JRSingleSheetXlsSaveContributor extends JRSaveContributor {
047:
048: /**
049: *
050: */
051: private static final String EXTENSION_XLS = ".xls";
052: public static final JRSingleSheetXlsSaveContributor INSTANCE = new JRSingleSheetXlsSaveContributor();
053:
054: /**
055: *
056: */
057: public static JRSingleSheetXlsSaveContributor getInstance() {
058: return INSTANCE;
059: }
060:
061: /**
062: *
063: */
064: public boolean accept(File file) {
065: if (file.isDirectory()) {
066: return true;
067: }
068: return file.getName().toLowerCase().endsWith(EXTENSION_XLS);
069: }
070:
071: /**
072: *
073: */
074: public String getDescription() {
075: return "Single sheet XLS (*.xls)";
076: }
077:
078: /**
079: *
080: */
081: public void save(JasperPrint jasperPrint, File file)
082: throws JRException {
083: if (!file.getName().endsWith(EXTENSION_XLS)) {
084: file = new File(file.getAbsolutePath() + EXTENSION_XLS);
085: }
086:
087: if (!file.exists()
088: || JOptionPane.OK_OPTION == JOptionPane
089: .showConfirmDialog(
090: null,
091: MessageFormat
092: .format(
093: java.util.ResourceBundle
094: .getBundle(
095: "net/sf/jasperreports/view/viewer")
096: .getString(
097: "file.exists"),
098: new Object[] { file
099: .getName() }),
100: java.util.ResourceBundle
101: .getBundle(
102: "net/sf/jasperreports/view/viewer")
103: .getString("save"),
104: JOptionPane.OK_CANCEL_OPTION)) {
105: JRXlsExporter exporter = new JRXlsExporter();
106: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
107: jasperPrint);
108: exporter
109: .setParameter(JRExporterParameter.OUTPUT_FILE, file);
110: exporter
111: .setParameter(
112: JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET,
113: Boolean.FALSE);
114: exporter.exportReport();
115: }
116: }
117:
118: }
|