简体   繁体   English

使用 Apache POI 在 Excel 上编写二维整数数组

[英]Write 2d arrays of integers on Excel using Apache POI

I am trying to export 2d arrays of integers to Excel.我正在尝试将二维整数数组导出到 Excel。 Precisely, I need to export 2 arrays of integers of the same dimension.准确地说,我需要导出 2 个相同维度的整数数组。 I previously installed the libraries for using Apache POI in Java using Eclipse IDE.我之前安装了使用 Eclipse IDE 在 Java 中使用 Apache POI 的库。

My idea is to pass as parameters for the writeExcel method 2 arrays of integers (arrayOne and arrayTwo) and get as a result an Excel file where 2 matrices were written with their respective values.我的想法是将 2 个整数数组(arrayOne 和 arrayTwo)作为 writeExcel 方法的参数传递,并作为结果获得一个 Excel 文件,其中 2 个矩阵是用它们各自的值写入的。 For example, for the case where both matrices are from 5x5 dimension this is what I tried:例如,对于两个矩阵都来自 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??
                        
                    }
                
            }
        }
        
    }

}

Clearly, in the previous code are missing parts that I don't know how to include.显然,在前面的代码中缺少我不知道如何包含的部分。 Thus, if I pass as a parameters to the method the following 2 matrices:因此,如果我将以下 2 个矩阵作为参数传递给该方法:

arrayOne: [[2, 0, 1, 3, 4], [0, 1, 2, 3, 4], [0, 2, 1, 4, 3], [2, 1, 0, 4, 3], [2, 0, 1, 3, 4]]
arrayTwo: [[1, 1, 4, 1, 1], [4, 4, 0, 4, 4], [0, 0, 1, 0, 0], [2, 3, 3, 3, 3], [3, 2, 2, 2, 2]]

The expected result in Excel must be something like: Excel 中的预期结果必须类似于:

在此处输入图片说明

Thanks in advance if anyone can help me with this question.如果有人可以帮助我解决这个问题,请提前致谢。

You should iterate through the whole excel sheet and then check if you are in the desired cell, write the appropriate value.您应该遍历整个 Excel 表,然后检查您是否在所需的单元格中,写入适当的值。

One way to do this, is with CellReference .一种方法是使用CellReference With that you can reference to a cell and then take its row/col index an write on it.有了它,您可以引用一个单元格,然后对其行/列索引进行写入。

For example:例如:

CellReference A1 = new CellReference("A1");

int row = cr.getRow();
int col = cr.getCol();

Now you got the index for row/col of A1 and you can write on this position like this:现在你得到了 A1 的行/列的索引,你可以在这个位置上写这样的:

Row row1 = ((sheet.getRow(row)==null) ? sheet.createRow(row) : sheet.getRow(row));

With the line above you check if the row exists, and if not, then you create one.使用上面的行检查该行是否存在,如果不存在,则创建一个。

Cell cell;
if (row1.getCell(col) == null) {
    cell = row1.createCell(col);
} else {
    cell = row1.getCell(col);
}

With the above, you check the specific cell on the specific row if its empty, and if it is then you create a new one, else you the its reference.使用上述内容,您检查特定行上的特定单元格是否为空,如果为空,则创建一个新单元格,否则您将查看它的引用。

Now, you are ready to write on A1 cell.现在,您已准备好在 A1 单元格上书写。 Simply do:简单地做:

cell.SetCellValue("arrayOne");

So now, just use row and col that represent the index for row/col in your sheet and write in the cells of your choice.所以现在,只需使用代表工作表中行/列索引的行和列,并写入您选择的单元格。

For example by doing col+=1 you getting B1 and by repeating the above code in a for loop and increment every time the col and/or row index inside your loop, you can write the arrays in the way you want on your sheet.例如,通过执行col+=1获得 B1 并通过在 for 循环中重复上述代码并在每次循环中的 col 和/或行索引时递增,您可以按照您想要的方式在工作表上编写数组。

Please feel free to ask me for extra clarification.请随时向我索取更多说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM