我正在开发一个程序,该程序从 Excel 电子表格中读取每一行和单元格,然后从特定行的每个单元格中获取数据,并使用该数据进行 Web 服务调用。一旦我得到 Web 服务 XML 响应的返回,我就会将它解析为一个成功的布尔值,并在发生故障时解析一条消息。
我能够解析数据并且我正在获取每一行响应,但是当尝试编辑该行最后两个单元格时,它只更新第一行成功和结果消息单元格。
以下是我的一些代码片段:
private static XSSFRow[] retrieveRows(XSSFWorkbook wb)
{
XSSFSheet xslSheet = wb.getSheetAt(0);
XSSFRow[] sheetRows = new XSSFRow[xslSheet.getLastRowNum()];
for (int i = 1; i < xslSheet.getLastRowNum()+1; i++)
{
sheetRows[i-1] = xslSheet.getRow(i);
}
return sheetRows;
}
public static String[] processRow(XSSFRow xslRow)
{
String[] cellValues = new String[totalCells];
for (int i = 0; i < totalCells; i++)
{
XSSFCell currentCell = xslRow.getCell(i);
String cellVal;
try
{
cellVal = currentCell.getStringCellValue();
}
catch (Exception e)
{
cellVal = "";
}
cellValues[i] = cellVal;
System.out.println("Cell "+i+": "+cellVal);
}
return cellValues;
}
以及所有魔法发生的最后部分......或类似的东西。
try
{
XSSFRow[] xslRows = retrieveRows(theWorkbook);
ArrayList<String[]> rowCellVals = new ArrayList<String[]>();
String sessionKey[] = WQSServices.sessionToken();
for (int r = 0; r < xslRows.length; r++)
{
System.out.println("R Value: "+r);
rowCellVals.add(processRow(xslRows[r]));
if(sessionKey[0].equals("true"))
{
String sessionData = sessionKey[1];
String[] cellValCurrRow = rowCellVals.get(r);
String attachmentData[] = WQSServices.uploadAttachment(sessionData, cellValCurrRow);
XSSFCell cell = xslRows[r].getCell(7);
if(cell == null)
{
cell = xslRows[r].createCell(7);
}
XSSFCell cell2 = xslRows[r].getCell(8);
if(cell2 == null)
{
cell2 = xslRows[r].createCell(8);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell2.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(attachmentData[0]);
cell2.setCellValue(attachmentData[1]);
System.out.println("New Cell Data: 1-"+cell.getStringCellValue()+" 2-"+cell2.getStringCellValue());
FileOutputStream fos = new FileOutputStream(xslFile);
theWorkbook.write(fos);
fos.close();
}
else
{
XSSFCell cell = xslRows[r].getCell(7);
if(cell == null)
{
cell = xslRows[r].createCell(7);
}
System.out.println("The Cell: "+cell.getStringCellValue());
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(sessionKey[0]);
FileOutputStream fis = new FileOutputStream(xslFile);
theWorkbook.write(fis);
fis.close();
}
readFile();
}
}
catch (IOException e)
{
e.printStackTrace();
}