我的文本文件包含这样的行
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/