用java poi 把一个txt里面的内容导入excel
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
参考代码:
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);
}}
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);
}}
追问
你好,非常感谢。刚才试了一下,第一句话在第一列,可是第二句话就到第二列了,就是结果是斜着输出的,并不是在同一列上,这是什么原因啊?
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询