我想从文本文件中读取特定的string,并将它们存储在Excel工作表中
我的文本文件包含这样的行
FLTR TID:0000003756 RPC ID:0000108159 USER: 补救应用程序服务
FLTR TID:0000003756 RPC ID:0000108159 USER: 补救应用程序服务
FLTR TID:0000003756 RPC ID:0000108159 USER: ibsdr
FLTR TID:0000003756 RPC ID:0000108159 USER: Vinay.k@in.uni.com
FLTR TID:0000003756 RPC ID:0000108159 USER: Vinay.k@in.uni.com
FLTR TID:0000003756 RPC ID:0000108159 USER: wsdl
FLTR TID:0000003756 RPC ID:0000108159 USER: stefan.hummel@uni.com
我想在Excel明细表列中明智地存储突出显示的单词,我想只存储唯一的用户名..就像:
Column1 …. Column2
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); } }
OUTPUT
S.NO …….用户名
17963 ……补救申请服务
在这个每当我执行这个程序只有第一个值被存储,而不是所有的值存储..我想只存储唯一的用户名在这个Excel表格..
请帮助我,提前致谢
- 对于唯一名称,您可以使用
ArrayList<String>
添加新的用户名添加和之前,检查用户是否存在arrayList中(通过使用contains("UName")
)并继续。 - 使用
String username=line.substring(line.lastindexOf(":")).trim();
或者你可以使用line.split(":")[1].trim();
对于Excel行创build,您可以使用循环。
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++; }
首先,我不是一个专家,从来没有尝试过,也不能在这一刻。 从看你的代码,我可以发现一些devise缺陷,可能是你的问题的原因。
你只能申报一行:
static HSSFRow row= sheet.createRow((short)1);
这意味着在你的store
方法中,同一行的单元格将被一次又一次地写入。 如果遇到唯一名称,则缺less的是创build新行的逻辑。 对于这个任务,我可以build议使用一个Set
最好是一个操作哈希值的低查找时间的好处,并存储在那里所有的唯一名称? 这样你可以使用一个简单的contains
查询。
此外,你的count
variables是closures的,并开始在2,而不是一个。 初始化为0或使用后增加。
一些伪代码:
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); }
我会build议你两个改进,这将更容易解决你的问题。
1)不要试图用Java写入Excel表格,它稍微复杂一些。 相反,你可以写入一个CSV文件。 您可以在外部打开此CSV文件并保存为Excel。
2)下面的行可能会导致用户名的修剪:
user=str.substring(48,75); // This is to get user names
相反,你可以使用
user=str.substring(48); // This is to get user names
所以,编写CSV文件不需要Excel库,也可以在Excel工作簿中打开。 🙂
公共类UniqueUSER {
静态HSSFWorkbook hwb =新HSSFWorkbook();
静态HSSFSheet sheet = hwb.createSheet(“new sheet”);
公共静态无效的主要(string[]参数)抛出IOException
{
HSSFRow行;
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){
如果(str.contains( “FLTR”))
{
String user = str.substring(97,135);
计数++;
names.add(用户);
HSSFRow row1 = sheet.createRow((short)count);
row1.createCell((short)0).setCellValue(“names”);
}
}
Iterator itr = names.iterator();
while(itr.hasNext()){
out.println(itr.next());
}
}
}