We spent the afternoon finally realized xls document data Swing tabular data transfer xls documents and read.
Next on the code
1. pop-up file, directory selection box
exportBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = "数据.xls"; String defaultPath = "D://temp"; //构造文件保存对话框 JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); chooser.setDialogType(JFileChooser.SAVE_DIALOG); chooser.setMultiSelectionEnabled(false); Chooser.setAcceptAllFileFilterUsed ( to false ); chooser.setDialogTitle ( " save data file " ); // Set default path File defaultFile = new new File (DefaultPath + " // Data " ); IF ! ( DefaultFile.exists ()) { defaultFile .mkdirs (); } chooser.setCurrentDirectory ( new new File (DefaultPath)); // open dialog int Result = chooser.showSaveDialog (getWindow ()); // null // file determines IF (Result == JFileChooser.APPROVE_OPTION) { String outPath = chooser.getSelectedFile () getAbsolutePath ();. File F = new new File (outPath); IF (f.isDirectory ()) { outPath = outPath + " \ \ " + name; } iF ( new new file (outPath) .exists ()) { int B = DialogUtils.showYesNoOptionDialog (getWindow (), " ? file already exists, whether or not to overwrite the file " , "操作确认"); if(b!=0){ return; } } File file = new File(outPath); ExcelExporter ee = new ExcelExporter(); try { ee.exportTable(xTable, file); } catch (IOException e1) { new RuntimeException(e1); } } } });
2. Form Data Export
package net.doublecom.sdk.tool.wirelessinfo.view; import java.io.*; import org.freeabc.client.core.component.table.XTable; public class ExcelExporter { public ExcelExporter() { } public void exportTable(XTable<?> xtable, File file) throws IOException { //Xtable自定义组件,也可换成Jtable
// TableModel tableModel = new JTable().getModel();
// tableModel.getColumnCount()
FileWriter out = new FileWriter(file); for(int i=0; i < xtable.getColumnCount(); i++) { if("名称.equals(xtable.getColumnName(i))||"年龄".equals(xtable.getColumnName(i))) out.write(xtable.getColumnName(i) + "\t"); } out.write("\n"); for(int i=0; i< xtable.getRowCount(); i++) { for(int j=0; j < xtable.getColumnCount(); j++) { if("名称".equals(xtable.getColumnName(j))||"年龄".equals(xtable.getColumnName(j))) out.write(xtable.getValueAt(i,j).toString()+"\t"); } out.write("\n"); } out.close(); System.out.println("write out to: " + file); } }
3. Read the document data xls
importBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String defaultPath = "D://temp//data//"; //构造文件保存对话框 JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setDialogType(JFileChooser.SAVE_DIALOG); chooser.setMultiSelectionEnabled(false); chooser.setAcceptAllFileFilterUsed(false); chooser.setDialogTitle ( " Load Data File " ); // Set default path File defaultFile = new new File (DefaultPath); IF (defaultFile.exists ()) { chooser.setCurrentDirectory ( new new File (DefaultPath)); } // acquired file name input box setting specifies the format chooser.addChoosableFileFilter ( new new FileFilter () { @Override public String getDescription () { return " Excel file (* .xls) " ; } @Override public Boolean Accept (File F) { IF (. F.getName () endsWith ( " XLS " ) || f.isDirectory ()) { return to true ; } the else { return to false ; } } }); // open dialog int the Result = chooser.showSaveDialog (getWindow ()); // null // file to determine the IF (the Result == JFileChooser.APPROVE_OPTION) { String outPath = chooser.getSelectedFile().getAbsolutePath(); File file = new File(outPath); try { StringBuffer sb = new StringBuffer(); List<String[]> dataArrs = new ArrayList<String[]>(); FileInputStream fis = new FileInputStream(file); //InputStream is = fis.getInputStream(); BufferedReader reader = new new BufferedReader(new new InputStreamReader (FIS, " GB2312 " )); reader.readLine (); // first line of information, title information, do not, if needed, comment String Line = null ; the while ((Line = reader.readLine ( !)) = null ) { String Item [] = line.split ( " \\ T " ); // XLS format of file space separator, where segmentation according comma IF ! (item.length = 2 ) Continue ; dataArrs.add (item); } for(String[] data:dataArrs) { if(data.length>=2) { sb.append("name="+data[0]); sb.append(" age="+data[1]+";"); } } reader.close(); } catch (IOException e1) { new RuntimeException(e1); } } } });