【问题标题】:Stream an arraylist into a fixed two dimensional array and print?将数组列表流式传输到固定的二维数组并打印?
【发布时间】:2017-02-26 20:00:55
【问题描述】:

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

编辑:也许我对上述问题部分不清楚。 我正在尝试将下面提到的 Excel (Book1.xls) 的元素复制到二维数组中。到目前为止,我已经成功地将 ti 复制到一个数组列表中:“singleRows”。但我无法将它们移动到二维数组:“myReplicate”。

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

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();
    }
}

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

【问题讨论】:

  • 为什么代码中的每一行都是注释?另外,请远离机制并解释您正在尝试完成的内容。这个问题不清楚,可能会被否决并搁置。
  • 您好,这是我第一次尝试将代码插入到 stackoverflow 中,它一直说代码格式不正确,直到我评论每一行 (Ctrl + K).. 我我在这里尝试将excel映射到二维数组,然后为sql插入语句创建案例..

标签: java arraylist multidimensional-array apache-poi java-stream


【解决方案1】:

刚刚检查了将 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();
                }

这帮助我进一步满足了我的要求。 感谢您对此的关注。

【讨论】:

    猜你喜欢
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2016-07-26
    相关资源
    最近更新 更多