使用 Apache POI 在 Excel 上写入二维整数数组
Write 2d arrays of integers on Excel using Apache POI
我正在尝试将二维整数数组导出到 Excel。准确地说,我需要导出 2 个相同维度的整数数组。我之前使用 Eclipse IDE.
在 Java 中安装了使用 Apache POI 的库
我的想法是将 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??
}
}
}
}
}
显然,在前面的代码中缺少我不知道如何包含的部分。因此,如果我将以下 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]]
Excel 中的预期结果必须类似于:
如果有人能帮我解决这个问题,在此先感谢。
您应该遍历整个 excel sheet 然后检查您是否在所需的单元格中,写入适当的值。
一种方法是使用 CellReference
。有了它,您可以引用一个单元格,然后将其 row/col 索引写入它。
例如:
CellReference A1 = new CellReference("A1");
int row = cr.getRow();
int col = cr.getCol();
现在你得到了A1的row/col的索引,你可以在这个位置上这样写:
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");
现在,只需在 sheet 中使用代表 row/col 索引的行和列,然后在您选择的单元格中写入。
例如,通过执行 col+=1
得到 B1 并在 for 循环中重复上述代码并在每次循环内的 col and/or 行索引时递增,您可以将数组写入sheet.
上的方式
请随时向我询问额外的说明。
我正在尝试将二维整数数组导出到 Excel。准确地说,我需要导出 2 个相同维度的整数数组。我之前使用 Eclipse IDE.
在 Java 中安装了使用 Apache POI 的库我的想法是将 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??
}
}
}
}
}
显然,在前面的代码中缺少我不知道如何包含的部分。因此,如果我将以下 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]]
Excel 中的预期结果必须类似于:
如果有人能帮我解决这个问题,在此先感谢。
您应该遍历整个 excel sheet 然后检查您是否在所需的单元格中,写入适当的值。
一种方法是使用 CellReference
。有了它,您可以引用一个单元格,然后将其 row/col 索引写入它。
例如:
CellReference A1 = new CellReference("A1");
int row = cr.getRow();
int col = cr.getCol();
现在你得到了A1的row/col的索引,你可以在这个位置上这样写:
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");
现在,只需在 sheet 中使用代表 row/col 索引的行和列,然后在您选择的单元格中写入。
例如,通过执行 col+=1
得到 B1 并在 for 循环中重复上述代码并在每次循环内的 col and/or 行索引时递增,您可以将数组写入sheet.
请随时向我询问额外的说明。