[英]How can i make that everything after “,” in .txt file will be checked separetly :)?
[英]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)?
我有一個程序(如下),它接收 txt 文件並將它們放入一個 excel 電子表格中。 我的目標是從文本文件中提取數據,但只提取冒號后的文本。
示例 txt 文件(txt 中的“名稱:”和“電話:”行之間沒有空格):
姓名:約翰·杜
電話:(xxx)xxx-xxxx
我希望我的程序只在 excel 表中保存 john doe 並將列標題為“名稱”:我希望我的程序只在 excel 表中保存 (xxx) xxx-xxxx 並將列標題為“電話”我希望能夠將多個條目添加到電子表格。
示例(在生成的 excel 表中):這就是我想要的樣子!
......A......B
1 個名字 | 電話
2 約翰·多伊 (123) 123-1234
3 傑克迪 (123) 123-1231
因此,我查看了字符串 split 以僅獲取“:”之后的數據,但找不到關於它的太多文檔。 還讓信息在 excel 中正確排列。 非常感謝任何幫助。
謝謝,詹姆斯
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);
}}
如果您的文件email.txt
始終具有您所說的格式:
Name: John
phone: (xxx) xxx-xxxx
我不熟悉行和單元格的創建,所以我會給你一些偽代碼,讓你了解你必須對你的代碼做什么:
在您的代碼的這一部分:
//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.