Java 将arraylist流式处理为固定的二维数组并打印?

Java 将arraylist流式处理为固定的二维数组并打印?,java,arraylist,multidimensional-array,apache-poi,java-stream,Java,Arraylist,Multidimensional Array,Apache Poi,Java Stream,我成功地将XLS工作表读取到ArrayList。现在棘手的部分是,我试图将其映射到一个固定的二维数组数组列表(i)->数组(x)(y)其中i=1705和x=155和y=11 编辑:也许我对前面提到的问题不清楚。 我试图将下面提到的Excel(Book1.xls)中的元素复制到二维数组中。到目前为止,我已经成功地将ti复制到arraylist中:“singleRows”。但我很难将它们转移到二维数组:“myReplicate” 我想使用这个数组的元素来创建SQL insert语句。 希望我在说明要

我成功地将XLS工作表读取到
ArrayList
。现在棘手的部分是,我试图将其映射到一个固定的二维数组<代码>数组列表(i)->数组(x)(y)其中
i=1705
x=155
y=11

编辑:也许我对前面提到的问题不清楚。 我试图将下面提到的Excel(Book1.xls)中的元素复制到二维数组中。到目前为止,我已经成功地将ti复制到arraylist中:“singleRows”。但我很难将它们转移到二维数组:“myReplicate”

我想使用这个数组的元素来创建SQL insert语句。 希望我在说明要求时没有遗漏任何内容

package excelread3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;

public class ExcelRead3
{
    public static void main(String[] args) throws IOException 
    {
         FileInputStream file = new FileInputStream(new File("C:\\Users\\CReaper\\Documents\\docs\\Opice\\Book1.xls"));
         HSSFWorkbook wb = new HSSFWorkbook(file);
         HSSFSheet sheet = wb.getSheetAt(0);
         HSSFRow firstRow = sheet.getRow(0);
         int rowcount = sheet.getPhysicalNumberOfRows();
         int colcount = firstRow.getLastCellNum();
         Iterator<Row> rowIterator = sheet.iterator();
         ArrayList<Object> singleRows = new ArrayList<Object>();
         ArrayList<Object> list = new ArrayList<Object>();
         while (rowIterator.hasNext()) 
         {
            Row row = rowIterator.next();
            Iterator <Cell> cellIterator = row.cellIterator();
            for( int i =1 ; i < colcount ; i++ )
            {
                while (cellIterator.hasNext()) 
                {
                    Cell cell = cellIterator.next();
                    //System.out.print(cell.getStringCellValue() + "\t\t");
                    singleRows.add(cell.getStringCellValue());
                }
            }
            list.add(singleRows);
        }
        // System.out.println(rowcount + "\n");
        // System.out.println(colcount + "\n");
        Stream<Object> stringStream;
        stringStream = singleRows.stream();
        String[][] myReplicate = new String[rowcount][colcount];
        myReplicate = stringStream.toArray(new String[stringStream.size()]);
        for (int i = 0; i < rowcount; i++)
        {
            for(int j = 0; j < colcount; j++)
            {
                System.out.printf("%5d ", myReplicate[i][j]);
            }
            System.out.println();
        }
        file.close();
    }
}
包excelread3;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入java.util.stream.stream;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.DateUtil;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFCell;
公共课ExcelRead3
{
公共静态void main(字符串[]args)引发IOException
{
FileInputStream file=newfileinputstream(新文件(“C:\\Users\\CReaper\\Documents\\docs\\Opice\\Book1.xls”);
HSSF工作簿wb=新的HSSF工作簿(文件);
HSSFSheet sheet=wb.getSheetAt(0);
HSSFRow firstRow=sheet.getRow(0);
int rowcount=sheet.getPhysicalNumberOfRows();
int colcount=firstRow.getLastCellNum();
迭代器rowIterator=sheet.Iterator();
ArrayList singleRows=新的ArrayList();
ArrayList=新建ArrayList();
while(roweiterator.hasNext())
{
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
for(int i=1;i

我正在尝试在填充数组元素之前将ArrayList转换为流。

刚刚检查了excel到二维数组的映射。 并发现:

        String[][] excelData = new String[numRows][numCols];
        for (int i=0; i<numRows; i++) 
                {
                HSSFRow row = mySheet.getRow(i);
                for (int j=0; j<numCols; j++) 
                    {
                    HSSFCell cell = row.getCell(j);
                    String value = cell.getStringCellValue();
                    excelData[i][j] = value;
                    System.out.println(Arrays.deepToString(excelData));     
                    }   
                System.out.println();
                }       
                    }   
                System.out.println();
                }
String[][]excelData=新字符串[numRows][numCols];

对于(int i=0;iWhy您的代码中的每一行都是注释?另外,请远离机械部分,解释您试图完成的任务。这个问题不清楚,可能会被否决并搁置。您好,这是我第一次尝试将代码插入stackoverflow,它一直说代码格式不正确我将注释每一行(Ctrl+K)…我在这里要做的是将excel映射到二维数组,然后为sql insert语句创建实例。。