简体   繁体   English

如何从 txt 文件中获取数据并将其发送到 Excel 电子表格?(我只想要 txt 文件中“:”之后的所有内容)?

[英]How do i get data from a txt file and send it to excel spreadsheet?(I only want everything after the “:” in the txt file)?

I have a program (below) that takes txt files and puts them into an excel spreadsheet.我有一个程序(如下),它接收 txt 文件并将它们放入一个 excel 电子表格中。 My goal is to extract the data from a text file but only text after the colon.我的目标是从文本文件中提取数据,但只提取冒号后的文本。

Example txt file(No space between "name:" and "phone:" lines in the txt):示例 txt 文件(txt 中的“名称:”和“电话:”行之间没有空格):

Name: John doe姓名:约翰·杜

phone: (xxx) xxx-xxxx电话:(xxx)xxx-xxxx

I want my program to only save john doe in the excel sheet and title the column "Name": I want my program to only save (xxx) xxx-xxxx in the excel sheet and title the column "phone" I want to be able to add multiple entrys to the spreadsheet.我希望我的程序只在 excel 表中保存 john doe 并将列标题为“名称”:我希望我的程序只在 excel 表中保存 (xxx) xxx-xxxx 并将列标题为“电话”我希望能够将多个条目添加到电子表格。

Example(in excel sheet generated):This is what i want it to look like!示例(在生成的 excel 表中):这就是我想要的样子!

....A..............B ......A......B

1 name | 1 个名字 | Phone电话

2 john doe (123) 123-1234 2 约翰·多伊 (123) 123-1234

3 jack dee (123) 123-1231 3 杰克迪 (123) 123-1231

So i looked at the string split to get only data after the ":" but cant find much documentation on it.因此,我查看了字符串 split 以仅获取“:”之后的数据,但找不到关于它的太多文档。 Also getting the info to line up correctly in the excel.还让信息在 excel 中正确排列。 Any help is much appreciated.非常感谢任何帮助。

Thanks, James谢谢,詹姆斯

package EREW;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


public class ReadWriteXL
{
public static void main(String[] args) throws InvalidFormatException, IOException{
    ArrayList arr=new ArrayList();
File f=new File("F:\\temp\\TEXT\\email.txt");

Scanner in=new Scanner(f);
System.out.println("Read Data From The Txt file ");
while(in.hasNext())
{    

arr.add(in.nextLine());
}
System.out.println("Data From ArrayList");
System.out.println(arr);


System.out.println("Write data to an Excel Sheet");
FileOutputStream fos=new FileOutputStream("F:/temp/1.xls");
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet spreadSheet = workBook.createSheet("email");
HSSFRow row;
HSSFCell cell;
for(int i=0;i<arr.size();i++){
    row = spreadSheet.createRow((short) i);
cell = row.createCell(i);
System.out.println(arr.get(i));
cell.setCellValue(arr.get(i).toString());
}
System.out.println("Done");
workBook.write(fos);
arr.clear();
System.out.println("ReadIng From Excel Sheet");

FileInputStream fis = null;
    fis = new FileInputStream("F:/temp/1.xls");

    HSSFWorkbook workbook = new HSSFWorkbook(fis);
    HSSFSheet sheet = workbook.getSheetAt(0);
    Iterator rows = sheet.rowIterator();

    while (rows.hasNext()) {
        HSSFRow row1 = (HSSFRow) rows.next();
        Iterator cells = row1.cellIterator();
        while (cells.hasNext()) {
            HSSFCell cell1 = (HSSFCell) cells.next();
            arr.add(cell1);
        }
}
System.out.println(arr);

}}

If your file email.txt Always has the format you said:如果您的文件email.txt始终具有您所说的格式:

Name: John
phone: (xxx) xxx-xxxx

I'm not familiarized with the row and cell creation, so I will give you some pseudo code to you understand what you have to do on your code:我不熟悉行和单元格的创建,所以我会给你一些伪代码,让你了解你必须对你的代码做什么:

On this part of your code:在您的代码的这一部分:

//create a count for rows here
inr rowNumber = 0;
//Here you are reading your list with all data readed
for(int i=0;i<arr.size()-1;i+=2){ //the -1 is for the array to stop on the 
                                 //last but one row

    row = spreadSheet.createRow((short) rowNumber); //external count

    //pseudocode from here
    cell1 = row.createCell(0); 
    cell2 = row.createCell(1); 

    cell1.setCellValue(  arr.get(i).substring(arr.get(i).indexOf(":")+1) );
                              // ^ this is for name
    cell2.setCellValue(  arr.get(i+1).substring(arr.get(i+1).indexOf(":")+1) );
                               // ^ this is for phone

    rowNumber++; //increment your rowNumber
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM