在客户端服务器体系结构中编辑服务器端的Excel文件

时间:2016-06-22 12:08:16

标签: java excel file path apache-poi

我有一本excel书,在所有业务用户之间共享。作为该过程的一部分,需要在服务器端更新excel。所有用户都已映射到包含excel book的网络驱动器。之前我们曾经使用activeX控件更新客户端的excel。现在的问题是,excel book的路径无法在服务器计算机上访问,因此它会抛出文件未找到异常。除了在服务器计算机上映射网络驱动器之外,还有什么方法可以解决这个问题吗?

以下是代码: public static String updateExcelFile(String fileLocation,String dataVal,String txnId)抛出IOException {

String status="";
//fileLocation = "Z:\\check.xlsx";
//FileInputStream file = new FileInputStream(new File("C:\\JXL\\Checklist OnBoarding1.1.xlsx"));

//fileLocation.rep
FileInputStream file = new FileInputStream(new File(fileLocation));
XSSFWorkbook wb = new XSSFWorkbook (file);
XSSFSheet sheet = wb.getSheetAt(0);

Row r = null;
Cell c = null;


String data[] = dataVal.split(",");


int rowCount = sheet.getPhysicalNumberOfRows();

int rowNum = rowCount;

Iterator<Row> rowInterator =sheet.iterator();
while(rowInterator.hasNext()){
    r=rowInterator.next();

    Iterator<Cell> cellIterator = r.cellIterator();
    while(cellIterator.hasNext()){

        c=cellIterator.next();
        //System.out.println();
        switch(c.getCellType()) {

        case Cell.CELL_TYPE_NUMERIC:
            System.out.print(c.getNumericCellValue() + "\t\t");
            if(c.getNumericCellValue()==Integer.parseInt(txnId)){
                int modRow = r.getRowNum();
                System.out.println("ModRow"+modRow);
                rowNum=modRow;
            }
            break;
        case Cell.CELL_TYPE_STRING:
            System.out.print(c.getStringCellValue() + "\t\t");
            if(c.getStringCellValue().equals(txnId)){
                int modRow = r.getRowNum();
                System.out.println("ModRow"+modRow);
                rowNum=modRow;
            }
            break;
    }



    }
}


if(rowNum==rowCount){
    r=sheet.createRow(rowNum+1);
    r=sheet.getRow(rowNum+1);
}else{
    r=sheet.getRow(rowNum);
}



for(int cellNum =0; cellNum<data.length;cellNum++){

    c=r.createCell(cellNum);
    c.setCellValue(data[cellNum]);
}

file.close();


//FileOutputStream outFile =new FileOutputStream(new File("C:\\JXL\\Checklist OnBoarding1.1.xlsx"));
FileOutputStream outFile =new FileOutputStream(new File(fileLocation));
wb.write(outFile);
outFile.close();
status="success";

退货状态; }

1 个答案:

答案 0 :(得分:0)

当用户单击按钮时,

将excel文件发送到服务器:

使用ajax将文件上传到servlet, 让servlet处理该文件,然后通过指向更新文件的链接(位于服务器上的临时文件夹中)来响应客户端,然后ajax可以呈现给用户。

用户现在可以下载excel并将其放到网络驱动器上,他和其他用户可以直接在其中进行编辑。

我想这种方法需要用户至少接受保存操作。

此方法也不能保护您免受Excel上的并发更新:多个用户可以让服务器编辑Excel的副本,而其他几个用户可以直接编辑网络驱动器上的Excel。

如果有问题的Excel很大,让服务器编辑Excel可能会有问题。

嗯,我想我回答了这个问题: - )。