1 28 package net.sf.jasperreports.view.save; 29 30 import java.io.File ; 31 import java.text.MessageFormat ; 32 33 import javax.swing.JOptionPane ; 34 35 import net.sf.jasperreports.engine.JRException; 36 import net.sf.jasperreports.engine.JRExporterParameter; 37 import net.sf.jasperreports.engine.JasperPrint; 38 import net.sf.jasperreports.engine.export.JRXlsExporter; 39 import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter; 40 import net.sf.jasperreports.view.JRSaveContributor; 41 42 46 public class JRSingleSheetXlsSaveContributor extends JRSaveContributor 47 { 48 49 52 private static final String EXTENSION_XLS = ".xls"; 53 public static final JRSingleSheetXlsSaveContributor INSTANCE = new JRSingleSheetXlsSaveContributor(); 54 55 58 public static JRSingleSheetXlsSaveContributor getInstance() 59 { 60 return INSTANCE; 61 } 62 63 66 public boolean accept(File file) 67 { 68 if (file.isDirectory()) 69 { 70 return true; 71 } 72 return file.getName().toLowerCase().endsWith(EXTENSION_XLS); 73 } 74 75 78 public String getDescription() 79 { 80 return "Single sheet XLS (*.xls)"; 81 } 82 83 86 public void save(JasperPrint jasperPrint, File file) throws JRException 87 { 88 if (!file.getName().endsWith(EXTENSION_XLS)) 89 { 90 file = new File (file.getAbsolutePath() + EXTENSION_XLS); 91 } 92 93 if ( 94 !file.exists() || 95 JOptionPane.OK_OPTION == 96 JOptionPane.showConfirmDialog( 97 null, 98 MessageFormat.format( 99 java.util.ResourceBundle.getBundle("net/sf/jasperreports/view/viewer").getString("file.exists"), 100 new Object []{file.getName()} 101 ), 102 java.util.ResourceBundle.getBundle("net/sf/jasperreports/view/viewer").getString("save"), 103 JOptionPane.OK_CANCEL_OPTION 104 ) 105 ) 106 { 107 JRXlsExporter exporter = new JRXlsExporter(); 108 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 109 exporter.setParameter(JRExporterParameter.OUTPUT_FILE, file); 110 exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 111 exporter.exportReport(); 112 } 113 } 114 115 } 116 | Popular Tags |