[英]Applying updates to a ResultSet with UCanAccess only updates the first row
[英]When saving XSSFWorkbook only one row updates
我正在開發一個程序,該程序從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();
}
基於Gagravarr的觀察,我的問題是基於文件輸出流在for循環內。 一旦將其移出for循環,所有內容都會按預期寫入工作簿。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.