繁体   English   中英

我有一个带管道和双正斜杠的字符串分隔,我需要在excel文件中格式化为java中的输出

[英]I have a string with pipe and double forward slash separated, that I need to format in an excel file as output in java

我从java中获取数据作为字符串,同时从XML文件中读取类似的标签。 并且这些数据是管道(|)分开的,并且为每个数据分隔了双正斜杠(//)。 在可用的标签中共享三个字符串,需要以excel格式转换为制表符分隔。

Information - Desktop | Telecom//Apple Mac//iPhone//Working with iPhone
Infrastructure - Desktop | Telecommunication//Apple iPod//iPad
Information - Desktop | Telecom//Apple Mac//iTunes 10//Settings//Troubleshooting
  • 这些是我使用java中的DOM解析器通过XML获取的3个字符串。

  • 我需要转换为excel的字符串作为制表符分隔的txt文件。

  • 输出文件的格式是,

输出格式

请建议我如何实现循环以获得这样的结果。

执行这三个迭代步骤,以将多分隔的非标准xml数据导入MS Excel。


第1步:导入数据
打开Excel →选择单元格B2Data功能区→ From Text图标( Get External Data组)→选择.\\yourFile.xml →从“文本导入向导”窗口中选择delimited →单击next →仅选择other →类型| (为你的分隔符)→点击finish →点击ok


第2步:导出剩余部分
突出显示column CCopy选择→打开Notepad (notepad.exe)→ Paste内容→ Save记事本文件.\\yourFile2.xml


第3步:继续进口
Activate Excel→选择Column CDelete列C→选择cell C2Data功能区→ From Text图标( Get External Data组)→选择.\\yourFile.xml →从“文本导入向导”窗口中选择delimited →单击next →选择只有other →键入/ (对于你的分隔符)→选择'将连续分隔符视为一个'→点击finish →点击ok


执行这三个步骤后,您应该按照上面描述的方式分离XML数据(使用它是内置的MS Excel功能)。

如果您有任何疑问,请告诉我。

我做了这样的事。 我能够以CSV格式输出数据,也许您想使用API​​将其写入Excel文件。

public class StringToExcelProblem {

    static String INPUT_DATA = 
              "Information - Desktop | Telecom//Apple Mac//iPhone//Working with iPhone\n"
            + "Infrastructure - Desktop | Telecommunication//Apple iPod//iPad\n"
            + "Information - Desktop | Telecom//Apple Mac//iTunes 10//Settings//Troubleshooting\n";

    public static void main(String[] args) {
        StringTokenizer st = new StringTokenizer(INPUT_DATA, "\n");
        List<Block> allBlocks = new ArrayList<>();
        while(st.hasMoreTokens()) {
            String blockData = st.nextToken();
            String[] st2 = blockData.split(" \\| ");
            String parent = st2[0];
            String labels = st2[1];
            StringTokenizer st3 = new StringTokenizer(labels, "//");
            int index = 0;
            Block block = null;
            while(st3.hasMoreTokens()) {
                String label = st3.nextToken();
                if(index == 0){
                    block = new Block(parent, label);
                    index ++;
                } else {
                    block.add(label);
                }
                parent = label;
            }
            allBlocks.add(block);
        }
        for(Block b: allBlocks) {
            for(RowObj r: b.rows){
                System.out.println(r);
            }
            System.out.println();
        }
    }
}

class RowObj {
    String label;
    String value;
    String parentId;
    String active;
    String parentTable;

    @Override
    public String toString() {
        return label + "," + value + "," + parentId + "," + active + "," + parentTable;
    }
}

class Block {
    List<RowObj> rows;

    public Block(String parent, String name) {
        RowObj row = new RowObj();
        row.label = name;
        row.value = name.toLowerCase();
        row.parentId = "Knowledge Base: " + parent;
        row.active = "TRUE";
        row.parentTable = "k_knowledge_base";

        rows = new ArrayList<>();
        rows.add(row);
    }

    public void add(String name) {
        RowObj parent = rows.get(rows.size()-1);

        RowObj row = new RowObj();
        row.label = name;
        row.value = name.toLowerCase();
        row.parentId = "Knowledge Category: " + parent.label;
        row.active = "TRUE";
        row.parentTable = "k_category";

        rows.add(row);
    }

    @Override
    public String toString() {
        return rows.toString();
    }
}

我能够生成以下输出。 它与屏幕截图中的内容相同。 唯一不同的是,这是CSV格式。 您只需根据需要对其进行格式化。

Telecom,telecom,Knowledge Base: Information - Desktop,TRUE,k_knowledge_base
Apple Mac,apple mac,Knowledge Category: Telecom,TRUE,k_category
iPhone,iphone,Knowledge Category: Apple Mac,TRUE,k_category
Working with iPhone,working with iphone,Knowledge Category: iPhone,TRUE,k_category

Telecommunication,telecommunication,Knowledge Base: Infrastructure - Desktop,TRUE,k_knowledge_base
Apple iPod,apple ipod,Knowledge Category: Telecommunication,TRUE,k_category
iPad,ipad,Knowledge Category: Apple iPod,TRUE,k_category

Telecom,telecom,Knowledge Base: Information - Desktop,TRUE,k_knowledge_base
Apple Mac,apple mac,Knowledge Category: Telecom,TRUE,k_category
iTunes 10,itunes 10,Knowledge Category: Apple Mac,TRUE,k_category
Settings,settings,Knowledge Category: iTunes 10,TRUE,k_category
Troubleshooting,troubleshooting,Knowledge Category: Settings,TRUE,k_category

希望这可以帮助!

暂无
暂无

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

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