Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ApachePOI在Excel上编写二维整数数组_Java_Excel_Multidimensional Array_Methods_Apache Poi - Fatal编程技术网

Java 使用ApachePOI在Excel上编写二维整数数组

Java 使用ApachePOI在Excel上编写二维整数数组,java,excel,multidimensional-array,methods,apache-poi,Java,Excel,Multidimensional Array,Methods,Apache Poi,我正在尝试将二维整数数组导出到Excel。确切地说,我需要导出两个相同维度的整数数组。我之前安装了使用EclipseIDE在Java中使用ApachePOI的库 我的想法是将2个整数数组(arrayOne和arrayTwo)作为writeExcel方法的参数传递,并得到一个Excel文件,其中2个矩阵用各自的值写入。例如,对于两个矩阵都来自5x5维的情况,我尝试了以下方法: import org.apache.poi.xssf.usermodel.XSSFCell; import org.apa

我正在尝试将二维整数数组导出到Excel。确切地说,我需要导出两个相同维度的整数数组。我之前安装了使用EclipseIDE在Java中使用ApachePOI的库

我的想法是将2个整数数组(arrayOne和arrayTwo)作为writeExcel方法的参数传递,并得到一个Excel文件,其中2个矩阵用各自的值写入。例如,对于两个矩阵都来自5x5维的情况,我尝试了以下方法:

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExportToExcel {
    
    public static void WriteExcel(int[][] arrayOne, int[][] arrayTwo) {
        
        String fileName = "MyExcelFile.xlsx";
        String sheet = "5x5";
        
        XSSFWorkbook book = new XSSFWorkbook();
        XSSFSheet hoja1 = book.createSheet(sheet);
        
        // Header of the de Excel File
        String[] header = new String[] {"col 0", "col 1", "col 2", "col 3", "col 4"}; 
    
        // Fill the Excel File
        for (int i = 0; i <= ruta.length; i++) {
            
            XSSFRow row = hoja1.createRow(i);
            
            for (int j = 0; j < header.length; j++) {
                
                if (i == 0) {
                    
                    XSSFCell cell = row.createCell(j);
                    cell.setCellValue(header[j]);
                    
                }
                
                    else {
                    
                        XSSFCell cell = row.createCell(j);
                        // some code here??
                        
                    }
                
            }
        }
        
    }

}
Excel中的预期结果必须类似于:


如果有人能帮我解答这个问题,请提前感谢。

您应该遍历整个excel工作表,然后检查您是否在所需的单元格中,并写出适当的值

一种方法是使用
CellReference
。这样,您就可以引用一个单元格,然后对其行/列索引进行写操作

例如:

CellReference A1 = new CellReference("A1");

int row = cr.getRow();
int col = cr.getCol();
现在你得到了A1行/列的索引,你可以在这个位置上这样写:

Row row1 = ((sheet.getRow(row)==null) ? sheet.createRow(row) : sheet.getRow(row));
使用上面的行检查行是否存在,如果不存在,则创建一行

Cell cell;
if (row1.getCell(col) == null) {
    cell = row1.createCell(col);
} else {
    cell = row1.getCell(col);
}
通过以上操作,您可以检查特定行上的特定单元格(如果为空),如果为空,则创建一个新单元格,否则创建其引用

现在,您可以在A1单元上进行写入了。简单地做:

cell.SetCellValue("arrayOne");
现在,只需在工作表中使用代表行/列索引的行和列,并在您选择的单元格中写入即可

例如,通过执行
col+=1
得到B1,并在For循环中重复上述代码,每次循环中的col和/或row索引都递增,您就可以按照希望的方式在工作表中写入数组

请随时要求我进一步澄清

cell.SetCellValue("arrayOne");