java - 我想从文本文件中读取特定字符串并将它们存储在 Excel 工作表中

标签 java string excel poi-hssf

我的文本文件包含这样的行

FLTR TID:0000003756 RPC ID:0000108159 用户:Remedy 应用程序服务

FLTR TID:0000003756 RPC ID:0000108159 用户:Remedy 应用程序服务

FLTR TID:0000003756 RPC ID:0000108159 用户:ibsdr

FLTR TID:0000003756 RPC ID:0000108159 用户:Vinay.k@in.uni.com

FLTR TID:0000003756 RPC ID:0000108159 用户:Vinay.k@in.uni.com

FLTR TID:0000003756 RPC ID:0000108159 用户:wsdl

FLTR TID:0000003756 RPC ID:0000108159 用户:stefan.hummel@uni.com

我想在 Excel 工作表中按列存储突出显示的单词,并且我想仅存储唯一的用户名..例如:

第 1 列....第 2 列

S.NO......用户名

1.................补救措施应用服务

2.................ibsdr

3.................Vinay.k@in.uni.com

4.................wsdl

5.................stefan.hummel@uni.com

这是我尝试过的

public class User {

    static HSSFWorkbook hwb = new HSSFWorkbook();
    static HSSFSheet sheet = hwb.createSheet("new sheet");
    static HSSFRow rowhead = sheet.createRow((short) 0);
    static HSSFRow row = sheet.createRow((short) 1);
    static int count = 1;

    public static void main(String [] args) {
        try {
            rowhead.createCell((short) 0).setCellValue("SNo");
            rowhead.createCell((short) 1).setCellValue("USER NAME");
            BufferedReader br = new BufferedReader(new FileReader("filter.log"));
            String str = null;
            String user = null;
            while ((str = br.readLine()) != null) {
                if (str.contains("FLTR")) {
                    user = str.substring(48, 75); // This is to get user names
                    count++;
                    store(user, count);
                }
            }
            FileOutputStream fileOut = new FileOutputStream("D:/Excel.xls");
            hwb.write(fileOut);
            fileOut.close();
            System.out.println("Your excel file has been generated!");
        }
        catch (Exception ex) {
            System.out.println(ex);
        }
    }

    private static void store(String user, int count) {
        row.createCell((short) 0).setCellValue(count);
        row.createCell((short) 1).setCellValue(user);
    }
}

输出

S.NO......用户名

17963......补救措施申请服务

每当我执行此程序时,仅存储第一个值,而不是存储所有值..并且我只想在此 Excel 工作表中存储唯一用户名..

请帮助我,提前致谢

最佳答案

  • 对于唯一名称,您可以使用 ArrayList<String>每当新的 UserName 出现时添加添加,并在此之前检查用户是否存在于 arrayList 中(通过使用 contains("UName") ) 然后继续。
  • 使用String username=line.substring(line.lastindexOf(":")).trim();或者您可以使用line.split(":")[1].trim();

对于 Excel 行创建,您可以使用循环。

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++;
}

关于java - 我想从文本文件中读取特定字符串并将它们存储在 Excel 工作表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24074798/

相关文章:

java - 为什么这会给我一个边界错误?

Perl、unicode 和区域设置 : how to process string in non-utf8 locale using `perl -p -i -e' ?

string - Excel 列中最常见的文本

java - 如何验证(通过单元测试)错误堆栈是否打印在日志文件中?

java - 为什么 ArrayList add() 和 add(int index, E) 复杂度是摊销常数时间?为什么 add() 不是 O(1),add(int index, E) 不是 O(n)?

java - 将字符串从 Java 类迁移到资源

excel - 获取 Excel 中选定范围内可用的事件单元格?

vba - 如何在每半小时后获得最近的日期

java - Spring security Remember-me 无法正常工作,数据库表无法正常工作

c# 将 console.writeline 转换为字符串