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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20450232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:02:15  来源:igfitidea点击:

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)?

javaexcel

提问by NILE

I have a program (below) that takes txt files and puts them into an excel spreadsheet. My goal is to extract the data from a text file but only text after the colon.

我有一个程序(如下),它接收 txt 文件并将它们放入一个 excel 电子表格中。我的目标是从文本文件中提取数据,但只提取冒号后的文本。

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 | Phone

1 名 | 电话

2 john doe (123) 123-1234

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

3 Hyman dee (123) 123-1231

3 Hyman迪 (123) 123-1231

So i looked at the string split to get only data after the ":" but cant find much documentation on it. Also getting the info to line up correctly in the excel. Any help is much appreciated.

因此,我查看了字符串 split 以仅获取“:”之后的数据,但找不到关于它的太多文档。还让信息在 excel 中正确排列。任何帮助深表感谢。

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);

}}

回答by Jorge Campos

If your file email.txtAlways 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
}