Java 值仅填充为2

Java 值仅填充为2,java,apache,apache-poi,Java,Apache,Apache Poi,我是java的新手&我试图从文本文件中读取数字数据&我想把每个数字写在Excel电子表格中的单独单元格中 输入数据如下:abc.txt 3008,45,14277,1063712232081163036,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,5066,245,223,102,4340,1,0,0,0,0,0,0,0

我是java的新手&我试图从文本文件中读取数字数据&我想把每个数字写在Excel电子表格中的单独单元格中

输入数据如下:abc.txt

3008,45,14277,1063712232081163036,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,5066,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,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2

我希望每个值位于不同的单元格中。我使用ApachePOI来使用excel工作表。这就是我现在能够编写的代码 包com.example.practice

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等


有谁能帮我一下吗?

我想你的for循环太多了,你迭代太多了,也就是说,首先你循环行中的所有项目,为这些项目你创建了100行,每行你再次迭代行中的所有项目,创建行,setCellValue中的bug你使用的是temp[I],而不是temp[cellnum],因此,您首先使用temp[0]创建100行,然后使用所有temp[1]创建100行


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

您能帮我一些解决方案吗?对不起,但我不知道你到底想做什么,因此很难提出更多建议,这实际上取决于你试图实现的程序逻辑,以及你如何相应地使用for循环。