2015-02-11 115 views
0

我已經編碼了以下程序,以便在四個JTextField中獲得用戶輸入並將其保存在.csv文件中。但我得到了不同的行。我想在.csv文件輸出爲逐列將數據從JTextField保存爲.csv文件中的列

我的代碼如下:

public class panel extends FirstClick { 

       final JTextField items; 
       final JTextField number; 
       final JTextField cost; 
       final JTextField amount; 

       public panel() {  
        JFrame myFrame = new JFrame();  
        myFrame.setLayout(new FlowLayout()); 

        myFrame.setTitle("GUI Demo - ISTE-121"); 
        myFrame.pack(); 
        myFrame.setLocation(600,300); 
        myFrame.setSize(400,200); 
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        myFrame.setVisible(true); 

       JPanel order = new JPanel(); 
       order.setLayout(new GridLayout(5,5,2,2)); 

       myFrame.add(order, BorderLayout.CENTER); 

       order.add(new JLabel("Item no:", SwingConstants.RIGHT)); 
       order.add(items = new JTextField(3)); 

       order.add(new JLabel("Number of:", SwingConstants.RIGHT)); 
       order.add(number = new JTextField(3)); 

       order.add(new JLabel("Cost", SwingConstants.RIGHT)); 
       order.add(cost = new JTextField(3)); 

       order.add(new JLabel("Amount owed:", SwingConstants.RIGHT)); 
       order.add(amount = new JTextField(10)); 
       amount.setEditable(false);     

       JPanel buttons = new JPanel(); 
       buttons.setLayout(new GridLayout(1,1,2,2)); 

      myFrame.add(buttons ,BorderLayout.SOUTH); 

       JButton calculate; 
       buttons.add(calculate=new JButton("Calculate")); 

       calculate.addActionListener(new ActionListener() 
       { 
       @Override 
       public void actionPerformed(ActionEvent e) 
       { 
        // TODO Auto-generated method stub 
        double cost1 = new Double(cost.getText().trim()).doubleValue(); 
        double number1 = new Double(number.getText().trim()).doubleValue(); 

        double result = cost1*number1; 
        amount.setText(String.valueOf(result));      
       }      
       }); 

       JButton save; 
       buttons.add(save = new JButton("Save")); 

       save.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent e) { 

        // The name of the file to open. 
        String fileName = "temp1.csv";  
        try { 
         // Assume default encoding. 
         FileWriter fileWriter = 
          new FileWriter(fileName, true); 

         // Always wrap FileWriter in BufferedWriter. 
         BufferedWriter bufferedWriter = 
          new BufferedWriter(fileWriter); 

         // Note that write() does not automatically 
         // append a newline character. 
         bufferedWriter.write(items.getText()); 
         bufferedWriter.newLine(); 
         bufferedWriter.write(number.getText()); 
         bufferedWriter.newLine(); 
         bufferedWriter.write(cost.getText()); 
         bufferedWriter.newLine(); 
         bufferedWriter.write(amount.getText()); 
         bufferedWriter.newLine(); 
         // Always close files. 
         bufferedWriter.close(); 
        } 
        catch(IOException ex) { 
         System.out.println(
          "Error writing to file '" 
          + fileName + "'"); 
         // Or we could just do this: 
         // ex.printStackTrace(); 
        } 

       }} 
       ); 

       JButton clear; 
       buttons.add(clear=new JButton("Clear")); 

       clear.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        // TODO Auto-generated method stub 
        items.setText(null); 
        number.setText(null); 
        cost.setText(null); 
        amount.setText(null); 
       }}); 

       JButton exit; 
       buttons.add(exit= new JButton("Exit")); 

       exit.addActionListener(new ActionListener() 
       { 

      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       // TODO Auto-generated method stub 
       System.exit(0); 

      } 
       }); 

     } 
} 



public class FirstClick { 

    public static void main(String args[]) { 
     panel second = new panel(); 


    } // end main 
} 

回答

0

你寫你的域之間的換行

刪除換行符()調用和取而代之的是寫';'

bufferedWriter.write(items.getText()); 
bufferedWriter.write(';'); 
bufferedWriter.write(number.getText()); 
bufferedWriter.write(';'); 
+0

我越來越對excel表如下輸入: 項目600; 3; 3; 9.0; 我希望它在列的單獨字段中 – 2015-02-11 11:20:52

+0

輸出來自單個單元格,如: item 600; 3; 3; 9.0; 我想從列到列 – 2015-02-11 11:29:22

+0

然後你Excel是肌動。閱讀http://kb.paessler.com/en/topic/2293-i-have-trouble-opening-csv-files-with-microsoft-excel-is-there-a-quick-way-to-fix-this谷歌爲其他類似的文字或使用第三方程序,如http://commons.apache.org/proper/commons-csv/user-guide.html – Peter 2015-02-11 11:36:02

0

試試這個: 複製這種模式在CSV:

「標籤」, 「值」

FileWriter fw = new FileWriter("/CSV/data.csv"); 
BufferedWriter bufferedWriter = new BufferedWriter(fw); 

bufferedWriter.write("\"ItemCode\","+"\""+items.getText()+"\""); 
bufferedWriter.write("\"ItemNumber\","+"\""+number.getText()+"\""); 
bufferedWriter.write("\"ItemCost\","+"\""+cost.getText()+"\""); 
bufferedWriter.write("\"AmountOwned\","+"\""+amount.getText()+"\""); 

bufferedWriter.close(); 
+0

感謝您的答覆。我認爲它幾乎完成了。它只是我在Excel文件中得到這樣的東西: ABCDE 商品編號444''Number 5''Cost of 45''Amount 225 我在列中獲取它們,但JTextField項目值在數字和數字它的成本來到 – 2015-02-11 14:53:14

+0

你的意思是「JTextField項目的價值是數字來的」JTextField.getText()==字符串,更好地檢查你的用戶界面 – Sarz 2015-02-12 05:02:40