java - 有没有办法在 Excel 的两列中打印出两个不同的数组?
问题描述
当我尝试在 excel 文件的同一个“工作表”中打印第二个数组“Object columnB []”时,第一个数组会消失吗?有什么方法可以轻松解决这个问题吗?我需要两者,因为第一个数组是“固定的”。第二个数组要复制多次,因为有不同的参赛者
package sprint2;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
public class ExcelTest {
public static void main(String[] args) throws IOException {
// Create workbook in .xls format
Workbook workbook = new HSSFWorkbook();
// For .xslx workbooks use XSSFWoorkbok();
// Create Sheets
Sheet sh = workbook.createSheet("Decathlon");
Sheet sh2 = workbook.createSheet("Heptathlon");
// cell.setCellValue("some string");
// Values for Column A for Decathlon
Object columnA[][] = { { "Name" },
{ "Number" },
{ "" },
{ "Event 1" },
{ "Event 2" },
{ "Event 3" },
{ "Event 4" },
{ "Event 5" },
{ "DAY 1 TOTAL" },
{ "DAY 1 PLACE" }, };
// Create Cells and Column A for Decathlon
int rowCount = 0;
for (Object emp[] : columnA) {
Row row = sh.createRow(rowCount++);
int columnCount = 0;
for (Object value : emp) {
Cell cell = row.createCell(columnCount++);
if (value instanceof String)
cell.setCellValue((String) value);
if (value instanceof Integer)
cell.setCellValue((Integer) value);
if (value instanceof Boolean)
cell.setCellValue((Boolean) value);
}
}
//-----------------------------------------------------------------------------------------
// Values for Column B for Decathlon
Object columnB[][] = { { "Calvin hall" },
{ 100 },
{ "RESULT", "SCORE" },
{ "E1 Result", "E1 Score" },
{ "E2 Result", "E2 Score" },
{ "E3 Result", "E3 Score" },
{ "E4 Result", "E4 Score" },
{ "E5 Result", "E5 Score" },
{ "D1 TOTAL" },
{ "D1 PLACE" }, };
// Create Row and Column B for Decathlon
int rowCount1 = 0;
for (Object emp1[] : columnB) {
Row row1 = sh.createRow(rowCount1++);
int columnCount1 = 1;
for (Object value1 : emp1) {
Cell cell1 = row1.createCell(columnCount1++);
if (value1 instanceof String)
cell1.setCellValue((String) value1);
if (value1 instanceof Integer)
cell1.setCellValue((Integer) value1);
if (value1 instanceof Boolean)
cell1.setCellValue((Boolean) value1);
}
}
// Merge cells on row 1,2,9,10 Column B and C
sh2.addMergedRegion(new CellRangeAddress(0, 0, 1, 2));
sh2.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
sh2.addMergedRegion(new CellRangeAddress(8, 8, 1, 2));
sh2.addMergedRegion(new CellRangeAddress(9, 9, 1, 2));
// Autosize Decathlon-columns
for (int i = 0; i < 16; i++) {
sh.autoSizeColumn(i);
}
try {
// Write the output to file
FileOutputStream output = new FileOutputStream("C:\\Users\\snick\\Desktop\\Exceltest.xls");
workbook.write(output);
output.close();
workbook.close();
System.out.println("Excel-file is Completed");
} catch (Exception e) {
e.printStackTrace();
}
}
}
解决方案
迭代对象 B 时不要重新创建同一行。替换
Row row1 = sh.createRow(rowCount1++);
和
Row row1 = sh.getRow(rowCount1++);
但是,您必须确保该行确实存在,即 columnA 数组的行数至少与 columnB 数组的行数相同。否则,当 rowCount1 大于 columnA.length 时,您需要使用 createRow。您可以先尝试getRow
,如果它返回 null - 使用createRow
添加一个新的。
createCell
第二个循环中的调用也有类似的问题。
推荐阅读
- django - 交易的django计算问题
- flutter - 错误:flutter/lib/ui/ui_dart_state.cc(199) 未处理的异常:空值检查运算符用于空值
- java - Eclipse RCP:如何使用 Workbench Site 键绑定服务注册 Jface 操作?
- python-3.x - 如何使用 PyPDF2 或任何其他包从旋转的 PDF 中提取文本而不将其从 Web 响应对象中保存?
- reactjs - reactPlayer 无法播放本地破折号(mpd)文件它返回 403 状态代码(禁止错误)
- java - JavaFX 应用程序在 IDE 中运行,带有 .bat,但双击时不运行
- flutter - 不使用google api根据位置查询firebase结果
- python - 我想要同一张图的两个图例。在给出的示例中,我想要红色和蓝色颜色条的两个图例
- python - Python 参数解析器 - 仅从控制台解析
- python - 如何从不明确的 url 中获取文件名?