简体   繁体   中英

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. 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):

Name: John doe

phone: (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.

Example(in excel sheet generated):This is what i want it to look like!

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

1 name | Phone

2 john doe (123) 123-1234

3 jack dee (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.

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:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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