如何将数据从表更新为excel? [英] how to update data from table to excel?

查看:107
本文介绍了如何将数据从表更新为excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从表格更新为excel表格。

这是我的代码。



I am trying to update data from the table to excel sheet.
Here is my code.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:                                     
 try{
    JFileChooser fileChooser = new JFileChooser();
    int retval = fileChooser.showSaveDialog(jButton1);

    if (retval == JFileChooser.APPROVE_OPTION)
    {
        File file = fileChooser.getSelectedFile();
        if (file != null)
        
        {
            if (!file.getName().toLowerCase().endsWith(".xls")) 
            
            {
                file = new File(file.getParentFile(), file.getName() + ".xls");
            }

            try 
            {
                Excel exp=new Excel();
                exp.exportTable(jTable1, file);


                Desktop.getDesktop().open(file);
            } 
            catch (UnsupportedEncodingException e) 
            {} 
            catch (FileNotFoundException e) 
            {
                System.out.println("not found");
            } 
            catch (IOException e)
            {}
        }
   }


   }catch(Exception e){
       System.out.println("saved new data.");
   }
}     





// Excel类文件





//Excel class file

class Excel
{
    
   public Excel()
    {}
    public void exportTable(JTable table, File file) throws IOException
    {
        TableModel model = table.getModel();
         FileWriter out = new FileWriter(file);
         BufferedWriter bw= new BufferedWriter(out);
         
         for(int i = 0; i < model.getColumnCount(); i++)
         { 
           
           bw.write(model.getColumnName(i) + "\t");
         }
         
        bw.write("\n");
        
        for(int i=0; i< model.getRowCount(); i++) {
         for(int j=0; j < model.getColumnCount(); j++) {
             
           bw.write(model.getValueAt(i,j).toString()+"\t");
        }
       bw.write("\n");
    }
     bw.close();
        System.out.println("Writing to" +file);
  }
}







我可以打开.xls文件。但无法将数据更新到它。

请指导我。

谢谢你




I can open .xls file. but can't update data into it.
Please Guide me.
Thank you

推荐答案

使用这个图书馆:



http://poi.apache.org/ [ ^ ]



它得到了很好的支持,应该可以正常工作(它适用于我们!):



http://poi.apache.org/spreadsheet/index.html [ ^ ]
use this library:

http://poi.apache.org/[^]

It's well supported and should work fine (it does for us!):

http://poi.apache.org/spreadsheet/index.html[^]


这篇关于如何将数据从表更新为excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆