我有一个程序(下面),它接受txt文件并将它们放入excel电子表格中。我的目标是从文本文件中提取数据,但只在冒号后提取文本。
示例txt文件(txt中“name:”和“phone:”行之间没有空格):
姓名:John doe
电话:(xxx)xxx-xxxx
我希望我的程序只在excel表中保存john doe并标题为“Name”列: 我希望我的程序只在excel表中保存(xxx)xxx-xxxx,标题为“phone”列 我希望能够在电子表格中添加多个信息。
示例(在excel表中生成):这就是我想要的样子!
....一个..............乙
1个名字|电话
2 john doe(123)123-1234
3 jack dee(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);
}}
答案 0 :(得分:0)
如果您的文件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
}