繁体   English   中英

在Java中的csv文件的列中写入字符串数组

[英]writing an array of strings down a column of a csv file in java

因此,我要完成的工作是在该列的每个单元格中写入一个数据数组,以容纳我拥有的许多数组元素。 我希望我在Excel中也像这样:

buyer | parts |  price| quantity  | comments
tom   | blah  | 546   | 5         |   blah
      |  blah | 56    |  67       |

我的文件比这多得多的列,但我想举一个简单的例子。 也造成我问题的是,一旦完成数组数据集,我就不想在编写器所在的最后一行上打印它之后的下一列,因此我希望它位于那一行上。 我已经研究了其他库来完成此任务,但是没有什么可以满足我的需求的。 我确实了解CSV文件的性质,如果不手动添加空白单元格,可能不允许这种情况发生,但是还有其他方法可以做到这一点吗?

Enumeration <String> paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements()) 
        {
            String paramName = (String)paramNames.nextElement();
                            writer.append(paramName);
            writer.append(',');
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1)
            {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                {
                    writer.append("No Value");
                    writer.append('\n');
                }
                else
                {
                    writer.append(paramValue);
                    writer.append('\n');
                }
            }
            else
            {
                for(int i = 0; i<paramValues.length; i++)
                {
                    writer.append(paramValues[i]);
                    writer.append(',');
                }
                writer.append('\n');
            }
            previousname = paramName;

        }
        writer.flush();
        writer.close();
    }

还为了弄清楚该数据来自何处,在表单中间是一个JSP表,因此我正在读取表中的值数组。

来自: http : //javacodeonline.blogspot.ca/2009/09/java-code-to-write-to-csv-file.html

/** The below Java Code writes 
 * some data to a file in CSV
 * (Comma Separated Value) File
 */

package developer;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MakeCSVFile {

    public static void main(String[] args) throws IOException {

        //Note the "\\" used in the path of the file 
        //instead of "\", this is required to read 
        //the path in the String format.
        FileWriter fw = new FileWriter("D:\\JavaCodeFile\\WriteTest.csv");
        PrintWriter pw = new PrintWriter(fw);

        //Write to file for the first row
        pw.print("Hello guys");
        pw.print(",");
        pw.print("Java Code Online is maing");
        pw.print(",");
        pw.println("a csv file and now a new line is going to come");

        //Write to file for the second row
        pw.print("Hey");
        pw.print(",");
        pw.print("It's a");
        pw.print(",");
        pw.print("New Line");

        //Flush the output to the file
        pw.flush();

        //Close the Print Writer
        pw.close();

        //Close the File Writer
        fw.close();        
    }
}

可能不进行硬打印,而是循环数组并打印每个值,并用逗号分隔。

就像是 :

Array array;

    Iterator<String> iter = array.iterator();
    while (iter.hasNext())
    {
        object = iter.next();
        pw.print(object.getBuyer());
        pw.print(",");
        pw.print(object.getTitle());
        ..          
    }

如果要使用Java编写CSV文件,请考虑使用OpenCSV库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM