如何让代码以 .csv 格式显示正确的行和列

How to get the code display proper rows and columns in .csv format

我有以下代码。问题是它在列中背靠背显示。 这是我的 excel 文件中的一张图片:

我希望它在单次执行的记录从一列到另一列填满后从下一行重新开始。

这是我的代码:

public class panel extends OrderSystem {

               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.CENTER);

          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) { 



                try{

                        // Assume default encoding.
                        FileWriter fw = new FileWriter("data.csv",true);
                        BufferedWriter bufferedWriter = new BufferedWriter(fw);



                        bufferedWriter.newLine();
                        bufferedWriter.write(',');

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

                        bufferedWriter.close();


                }  

                    catch(IOException ex) {
                        System.out.println(
                            "Error writing to file '"
                            +  "'");
                        // 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);

        }


           });



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

          myFrame.add(buttons2 ,BorderLayout.SOUTH);

              JButton load;
              buttons2.add(calculate=new JButton("Load"));

              calculate.addActionListener(new ActionListener() 
              {
                @Override
                public void actionPerformed(ActionEvent e) 
                {
                    // TODO Auto-generated method stub





                }

              });




                  JButton next;
                  buttons2.add(calculate=new JButton("Next>"));

                  calculate.addActionListener(new ActionListener() 
                  {
                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        // TODO Auto-generated method stub





                    }

                  });




                      JButton prev;
                      buttons2.add(calculate=new JButton("<Prev"));

                      calculate.addActionListener(new ActionListener() 
                      {
                        @Override
                        public void actionPerformed(ActionEvent e) 
                        {
                            // TODO Auto-generated method stub





                        }

                      });

    }

}

Android: How to write newline in CSV file?

这里写着可以用write.print("\r\n")所以用"\r\n"换行

而不是写

bufferedWriter.newLine();

你写

bufferedWriter.write("\r\n");

bufferedWriter.write("\r");
bufferedWriter.newLine(); 

因为 bufferedWriter.newLine();据我所知,只将“\n”写入您的文件。 希望对你有帮助