首页 > 解决方案 > 有没有办法在 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();
            }
        }
    }

标签: javaarraysexceleclipse

解决方案


迭代对象 B 时不要重新创建同一行。替换

Row row1  = sh.createRow(rowCount1++);

Row row1 = sh.getRow(rowCount1++);

但是,您必须确保该行确实存在,即 columnA 数组的行数至少与 columnB 数组的行数相同。否则,当 rowCount1 大于 columnA.length 时,您需要使用 createRow。您可以先尝试getRow,如果它返回 null - 使用createRow添加一个新的。

createCell第二个循环中的调用也有类似的问题。


推荐阅读