2015-10-06 71 views
0

我是一个java的新手&我想从文本文件中读取数字数据&我想在Excel电子表格的单独单元格中编写每个数字。值只填充2

输入数据为如下:(的abc.txt)

3008,45,14,277,10,6371,223,208,116,3036,1,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,1,0,0,0,0,0,0,0,0,0,0,0,2 2893,114,16,108,30,506,245,223,102,4340,1,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,1,0,0,0,0,0,0,0,0,0,0,0,2

我想要在不同的单元格中的每个值。我使用Apache POI来使用Excel表格。这是我能够编码到现在为止 package com.example.practise;

import java.io.*; 
import java.util.*; 
import org.apache.poi.hssf.usermodel.*; 
import org.apache.poi.ss.usermodel.Cell; 
//import org.apache.poi.ss.usermodel.Creationhelper; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 


public class MultivariateDT { 

    public static void main(String[] args) throws IOException{ 

     BufferedReader bufferedReader = new BufferedReader(new FileReader("C:/Users/419803/Desktop/abc.txt")); 
     FileOutputStream out = new FileOutputStream("C:/Users/419803/Desktop/workbook.xls"); 
     HSSFWorkbook wb = new HSSFWorkbook(); 
     HSSFSheet sheet1 = wb.createSheet("new sheet"); 
     Row row = null; 
     Cell cell = null; 
     String line = null; 

     String delimiter = ","; 

     while ((line = bufferedReader.readLine()) != null) 
     { 
      String[] temp=null; 
      String str = " "; 
      temp = line.split(delimiter); 

      for(int i =0; i < temp.length ; i++) 
      { 
       str = temp[i] + str; 

       int rownum; 
       for (rownum =0; rownum < 100; rownum++) 
       { 
        row = sheet1.createRow(rownum); 

        for (int cellnum =0; cellnum <temp.length; cellnum ++) 
        { 
         cell = row.createCell(cellnum); 
         cell.setCellValue(temp[i]);   
        } 

       } 
      } 

      System.out.println(); 

     } 
     wb.write(out); 
     out.close(); 
    } 
} 

节数行&细胞必须创建是正确的,但在Excel文件中的数据是错误的。
在excel文件中只显示2,2,2,2等。

任何人都可以帮我吗?

回答

0

我认为你有太多的循环迭代太多,也就是先循环遍历行中的所有项目,因为这些循环创建了100行,并且在每一行中迭代了行中的所有项目再次,创建行,错误setCellValue()你正在使用temp [i],而不是temp [cellnum],所以你第一次创建100行与temp [0],然后100行与所有temp [1],...

这取决于生成的电子表格应该是什么样子,但我会尝试相应地调整for循环,例如对于每行一行,您将删除外部for循环,对于每行100行,您将删除单个外部for循环。

+0

你能帮我解决一些问题吗? –

+0

对不起,但我不知道你到底在做什么,因此很难提出更多的建议,这取决于你试图实现的程序逻辑以及你如何使用for() - 循环。 – centic