我想从文本文件中读取特定的字符串,并将它们存储在Excel表单中 [英] I want to read particular string from a text file and store them in Excel sheets
问题描述
我的文本文件包含这样的行
My Text file Contains line like this
FLTR TID:0000003756 RPC ID:0000108159 USER:补救应用程序服务
FLTR TID: 0000003756 RPC ID: 0000108159 USER: Remedy Application Service
FLTR TID:0000003756 RPC ID:0000108159 USER:补救应用程序服务
FLTR TID: 0000003756 RPC ID: 0000108159 USER: Remedy Application Service
FLTR TID:0000003756 RPC ID:0000108159 USER: ibsdr
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: 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: wsdl
FLTR TID:0000003756 RPC ID: 0000108159 USER: stefan.hummel@uni.com
FLTR TID: 0000003756 RPC ID: 0000108159 USER: stefan.hummel@uni.com
我想在列中明确列出Excel列表中的突出显示的字词,我只想存储唯一的用户名..如:
I want to store highlighted words in Excel sheets in columns wise and i want to store only unique user names.. like:
Column1 .... 列2
S.NO...........UserName
1 .................. 补救申请服务
1..................Remedy Application Service
2 强> .................. ibsdr
2..................ibsdr
3 .................. Vinay.k@in.uni.com
3..................Vinay.k@in.uni.com
4 .................. wsdl
4..................wsdl
5 .................. stefan.hummel@uni.com
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 ....... UserName
S.NO.......UserName
17963 ...补救申请服务
17963......Remedy Application Service
在每次执行此程序时,只有第一个值正在获取存储,而不是所有的值存储。我想仅在此Excel表单中存储 UNIQUE用户名。
in this whenever I execute this program only first value is getting stored rather all values stores.. and I want to store only UNIQUE user names in this Excel sheet..
请帮助我,谢谢提前
推荐答案
- 对于唯一名称,您可以使用
ArrayList< String>
添加每当新的UserName显示添加,并且之前检查用户是否存在于arrayList或不(通过使用包含(UName)
)而不是继续。 - 使用
String username = line.substring(line.lastindexOf(:))。trim();
或者您可以使用line.split(:)[1 ] .trim();
- 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 ofcontains("UName")
) and than proceed. - Use
String username=line.substring(line.lastindexOf(":")).trim();
or You can useline.split(":")[1].trim();
对于Excel行创建,您可以使用循环。 em>
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++;
}
这篇关于我想从文本文件中读取特定的字符串,并将它们存储在Excel表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!