I want to read particular string from a text file and store them in Excel sheets

前端 未结 4 833
遇见更好的自我
遇见更好的自我 2021-01-24 04:02

My Text file Contains line like this

FLTR TID: 0000003756 RPC ID: 0000108159 USER: Remedy Application Service

FLTR TID: 0000003756 RPC ID: 0

4条回答
  •  情话喂你
    2021-01-24 04:27

    First off, I am not an expert and have never tried this myself nor can I at the moment. From looking at your code I can spot a few design flaws that might be the reason for your problem.

    You only ever declare one row:

    static HSSFRow row= sheet.createRow((short)1);
    

    This means that in your store method, the same row's cells will be written again and again. What you are missing is the logic to create a new row if a unique name is encountered. For this task, may I suggest using a Set preferably one operating on hashes for the benefit of low lookup time, and storing all unique names in there? That way you can use a simply contains query.

    Furthermore, your countvariable is off and starts counting at 2, not at one. Initialise it to 0 or increment it after its use.

    Some pseudocode:

    private HashSet names = new HashSet<>();
    // More fields here.
    
    // Now start in your if clause:
    if(str.contains("FLTR" && !names.contains(str.substring(48))
    {
        store(user, count);
        count++;
        names.add(str.substring(48));
    }
    
    // More of your code.
    
    private static void store(String user, int count)
    {
        // Create new row
        HSSFRow row = sheet.createRow((short)rowCount);
        rowCount++;
    
    
        row.createCell((short) 0).setCellValue(count);
        row.createCell((short) 1).setCellValue(user);
    }
    

提交回复
热议问题