索引越界exception:重复获得同一行数次

这是我的代码,我用一个Excel工作表,读取其内容,并从我有一个列表添加一个GUID的每一行。

更新代码:

public class PublishOutputFile { public void publishOutputWithGuids(List<String> guids, File file) throws Exception { FileInputStream fileStream = new FileInputStream(file); HSSFWorkbook workbook = new HSSFWorkbook(fileStream); HSSFSheet sheet = workbook.getSheetAt(0); Row toprow = sheet.getRow(0); int cellsNum = toprow.getLastCellNum(); Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); int rownum = row.getRowNum(); System.out.println(rownum); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: break; case Cell.CELL_TYPE_NUMERIC: break; case Cell.CELL_TYPE_STRING: break; } if (rownum == 0) { row.createCell(cellsNum).setCellValue("GUID"); } else { // System.out.println(rownum); row.createCell(cellsNum).setCellValue(guids.get(rownum-1)); } } } fileStream.close(); FileOutputStream out = new FileOutputStream( new File( "C:/Users/U0172959/Desktop/Themes_20131120_234645-hierarchy with GUIDS.xls")); workbook.write(out); out.close(); } } 

更新输出:

 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 286, Size: 286 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at mappingForEtl.PublishOutputFile.publishOutputWithGuids(PublishOutputFile.java:43) at mappingForEtl.InputFileParsing.main(InputFileParsing.java:75) 

在输出中,每一行都被重复了5次,我认为这是导致出界exception的原因。 但我无法弄清楚是什么原因造成这种情况发生。

(我昨天对不同的文件使用了相同的代码,而且工作正常…)它可以与我作为input的Excel表中的列数有任何关系。 我昨天用过的有8栏,现在用的有20栏。

任何人有任何想法可能造成这一点? 感谢帮助。

您正在迭代一行内的单元格,并为每个单元打印行号。 你确定在你的情况下打印正确的数据的数据吗?

 if (row.getRowNum() == 0) { row.createCell(cellsNum).setCellValue("GUID"); } else { System.out.println((row.getRowNum())); // row number for each cell? row.createCell(cellsNum).setCellValue(guids.get((row.getRowNum()))); } 

更新:

重复输出的问题是你有一个循环在你的行。 每一行由许多单元组成。 你也循环了单元格。 重复的输出来自这样一个事实,即当您循环访问行中的单元格时,该行不会改变。 为了更好的理解,你应该改变你的输出:

 System.out.println("Being at row: " + row.getRowNum() + " creating cell at cellnum: " + cellsNum); 

这应该清理对正在发生的事情的理解。

更新2:

抛出IndexOutOfBoundsException,因为guid的大小是286,但索引是从零开始的,不是从1开始的。当你踩第一行不同的时候,你应该把guids.get(row.getRowNum())改为guids.get(row.getRowNum() - 1) 。 这可以工作,但不是必须的。 只要我们不知道详细的数据结构,它可能是没有价值的你… … –

关于例外

您可能意外地创build了一些新的Row可能是空白 ),并且该RowList上没有GUID 。 这可能会导致此exception。

也许这个补充可以帮助你:

 while (rowIterator.hasNext()) { Row row = rowIterator.next(); if (row.getLastCellNum() < cellsNum) continue; 

关于重复的输出

您正在遍历每一行,然后遍历每个单元格,然后打印行号。 考虑到一行有4个单元格,这将最终打印行号4次。

问题在这里:

 } else { row.createCell(cellsNum).setCellValue( guids.get((row.getRowNum()))); } 

你的guids.get抛出exception,因为它不包含索引286处的元素。

解决scheme:检索row.getRowNum - 1的元素,以便您现在检索0到285中的元素:

 } else { row.createCell(cellsNum).setCellValue( guids.get((row.getRowNum() - 1))); } 

更新:

既然看起来你不是想解决IndexOutOfBoundsException (非常奇怪),而是更多的关于为什么这些数字被打印4到5次,那么检查你的当前algorithm:

 Iterator<Row> rowIterator = sheet.iterator(); //verify if there's a row to read in the current Excel sheet while (rowIterator.hasNext()) { //get the row Row row = rowIterator.next(); //get an iterator of the cells Iterator<Cell> cellIterator = row.cellIterator(); //while there's a cell to read... while (cellIterator.hasNext()) { //get the cell //note: if the cell is empty, it won't be considered by the CellIterator, so even //if you open the file and see 20 cells, this Iterator will seek for cells with some value //from your current output, this looks to be only 5 cells Cell cell = cellIterator.next(); //verify the type of the cell... switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: //do nothing? break; case Cell.CELL_TYPE_NUMERIC: //do nothing? break; case Cell.CELL_TYPE_STRING: //do nothing? break; } if (row.getRowNum() == 0) { row.createCell(cellsNum).setCellValue("GUID"); } else { //prints the number of the row (per cell) //this causes the 1\n1\n1\n1\n2\n2\n... output System.out.println((row.getRowNum())); //writes the element in guids at index of row num row.createCell(cellsNum).setCellValue( //guids.get((row.getRowNum()))); guids.get((row.getRowNum() - 1))); } } }