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

不羁的心 提交于 2019-12-02 06:25:58
  • For Unique Names you can Use ArrayList<String> add whenever new UserName appears add and before that check whether user exist in arrayList or not (by the Use of contains("UName")) and than proceed.
  • Use String username=line.substring(line.lastindexOf(":")).trim(); or You can use line.split(":")[1].trim();

For Excel row creation you can use loop.

int i=0;
while((str=br.readLine())!=null){
row = sheet.createRow((short) i);
cell = row.createCell(i);
cell.setCellValue("UserName");//Set UserName after getting it from 'str'
i++;
}

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<String> 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);
}

I would suggest you two improvements which will make it easier to solve your issue.

1) Do not attempt to write into Excel sheet using Java, its slightly complicated. Instead you can write into a CSV file. You can externally open this CSV file and save as Excel.

2) The below line might cause trimming of the user names:

 user=str.substring(48,75);   // This is to get user names

Instead you can use

user=str.substring(48);   // This is to get user names

So, writing a CSV doesn't need Excel libraries and it can be opened in an excel workbook as well. :)

public class UniqueUSER {

static HSSFWorkbook hwb=new HSSFWorkbook();

static HSSFSheet sheet = hwb.createSheet("new sheet");

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

{

HSSFRow row;

HashSet names = new HashSet<>();

BufferedReader br=new BufferedReader(new FileReader("simle.log"));

PrintStream out=new PrintStream("D:/Excel.xls");

String str=null;

int count=0;

while((str=br.readLine())!=null) {

if(str.contains("FLTR"))

{

String user=str.substring(97, 135);

count++;

names.add(user);

HSSFRow row1= sheet.createRow((short)count);

row1.createCell((short) 0).setCellValue("names");

}

}

Iterator itr=names.iterator();

while(itr.hasNext()) {

out.println(itr.next());  

}

}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!