1

我正在尝试将 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){

    }
}
}
4

1 回答 1

1

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

于 2013-11-11T07:37:48.937 回答