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.JRCsvExporter; 39 import net.sf.jasperreports.view.JRSaveContributor; 40 41 45 public class JRCsvSaveContributor extends JRSaveContributor 46 { 47 48 51 private static final String EXTENSION_CSV = ".csv"; 52 public static final JRCsvSaveContributor INSTANCE = new JRCsvSaveContributor(); 53 54 57 public static JRCsvSaveContributor getInstance() 58 { 59 return INSTANCE; 60 } 61 62 65 public boolean accept(File file) 66 { 67 if (file.isDirectory()) 68 { 69 return true; 70 } 71 return file.getName().toLowerCase().endsWith(EXTENSION_CSV); 72 } 73 74 77 public String getDescription() 78 { 79 return "CSV (*.csv)"; 80 } 81 82 85 public void save(JasperPrint jasperPrint, File file) throws JRException 86 { 87 if (!file.getName().endsWith(EXTENSION_CSV)) 88 { 89 file = new File (file.getAbsolutePath() + EXTENSION_CSV); 90 } 91 92 if ( 93 !file.exists() || 94 JOptionPane.OK_OPTION == 95 JOptionPane.showConfirmDialog( 96 null, 97 MessageFormat.format( 98 java.util.ResourceBundle.getBundle("net/sf/jasperreports/view/viewer").getString("file.exists"), 99 new Object []{file.getName()} 100 ), 101 java.util.ResourceBundle.getBundle("net/sf/jasperreports/view/viewer").getString("save"), 102 JOptionPane.OK_CANCEL_OPTION 103 ) 104 ) 105 { 106 JRCsvExporter exporter = new JRCsvExporter(); 107 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 108 exporter.setParameter(JRExporterParameter.OUTPUT_FILE, file); 109 exporter.exportReport(); 110 } 111 } 112 113 } 114 | Popular Tags |