将数据放在一行中在csv文件中

时间:2013-11-11 07:33:00

标签: java export-to-csv

我正在尝试将xml数据写入csv文件。一切正常,除非数据的长度太长,然后在新行中写入数据。如果地址太长,则转到下一行,然后写入数据。

public class ExportToCSV {

public static final String LINE_BREAK = "\n";
List<Line> lines = new ArrayList<Line>();
public static void main(String argv[]) {

    try {   File fXmlFile = new File("C:/Users/Master/Desktop/demo_jar/Eff_SOR.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("record");
        System.out.println("----------------------------");
        List< Map<String, String>> list = new ArrayList<Map<String,String>>();

        for (int temp = 0; temp < nList.getLength(); temp++) {
             Map<String, String> map = new HashMap<String, String>();
             Node nNode = nList.item(temp);
             if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                   Element eElement = (Element) nNode; 

                   map.put("Address", eElement.getElementsByTagName("address_").item(0).getTextContent());                                 
                   map.put("Price1", eElement.getElementsByTagName("price1").item(0).getTextContent());                  
                   map.put("Base_qty1", eElement.getElementsByTagName("base_qty1").item(0).getTextContent());
                   map.put("base_price1", eElement.getElementsByTagName("base_price1").item(0).getTextContent());         

                   list.add(map);               
              }
        }

        generateCsvFile("C:\\Users\\Master\\Desktop\\demo_jar\\testCSV.csv", list );
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static void generateCsvFile(String sFilename, List< Map<String, String>>  test) {
    try {
        FileWriter writer = new FileWriter(sFilename);

        writer.append("Address");
        writer.append(",");
        writer.append("Price1");
        writer.append(",");
        writer.append("Base_qty1");
        writer.append(",");
        writer.append("Price2");
        writer.append(",");
        writer.append("base_price1");
        writer.append(",");           
        writer.append(LINE_BREAK);

        for (Map<String, String> map : test) {              

        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String,String> mapEntry = (Map.Entry<String,String>) iterator.next();
           //ystem.out.println("key: " + mapEntry.getKey() + ", value:" + mapEntry.getValue());

            writer.append(mapEntry.getValue());
            writer.append(",");
            iterator.remove();
        }
        writer.append(LINE_BREAK);
        }
        writer.flush();
        writer.close();
    }catch(Exception e){

    }
}
}

1 个答案:

答案 0 :(得分:1)

.csv只是一个带逗号的文本文件,用于分隔字段值 - 因此,您无法控制在Excel中首次打开文件时显示的单元格大小。