填报表导出excel非可写单元格锁定问题



问题描述:

 

填报表单元格分为可写和不可写两种状态,当填报表在web上展现的时候可写单元格可以进行数据填报和修改,非可写单元格不可操作。

报表导出为excel时,润乾导出excel包默认情况下不对excel单元格属性进行特殊修改,当导出的报表为填报表时,报表中设定的单元格可写属性并不能在导出后的excel中体现,也就是说无论是可写还是非可写的单元格都会在excel中变成普通单元格,而恰恰一些客户在导出excel后会对excel进行修改然后再将excel导入报表,这时候就必然需要导出后的excel中单元格的可写非可写属性要和报表中的一致,此时就需要导出excel的报表中的非可写单元格在excel中锁定。

 

解决思路:

   

通过Java类来操作excel文件每个单元格的属性是很容易实现的,我们这里用到了jxl这个java操作excel类来实现功能。

思路,不使用润乾提供的导出excel方法,直接通过自定义按钮调用java代码实现对导出非可写单元格的锁定:

在导出excel的过程中,要先遍历报表中的所有单元格取得所有数据。

在这个遍历过程中做一个操作,将遍历到的可写单元格存在一个数组里。

遍历完后用润乾导出excel类导出excel

通过jxl类操作excel文件,从第二步中建立的数组中取出可以单元格,并设置这些单元格不锁定,其他的单元格设置为锁定。

 

部分代码如下(完整例子代码:附件中jsp文件夹exportExcel.jsp文件):

 

      //计算报表

      Engine enging = new Engine(rd, context);

      IReport iReport = enging.calc();

      //遍历单元格

      int tRow = iReport.getRowCount();

      int tCol = iReport.getColCount();

      boolean flag = false;

      ArrayList cellList = new ArrayList();

      for (int r = 1; r <= tRow; r++) {

             for (short c = 1; c <= tCol; c++) {

                    INormalCell iCell = iReport.getCell(r,(short)c);

                    InputProperty ip = iCell.getInputProperty();

                    if(ip!=null){

                           flag = ip.isWritable();

                           if(flag==true){

                                  String[] pointArray =new String[2];

                                  pointArray[0]=String.valueOf(r);

                                  pointArray[1]=String.valueOf(c);

                                  System.out.println(Arrays.asList(pointArray));

                                  cellList.add(pointArray);

                           }

                    }

             }

      }

      //生成Excel文件

      String saveFile = request.getRealPath(“/reportFiles/”) + “/”+ reportFile + “.xls”;

      ExcelReport eReoprt = new ExcelReport();

      eReoprt.export(iReport);

      FileOutputStream fos = new FileOutputStream(saveFile);

      eReoprt.saveTo(fos);

      fos.close();

 

//jxl锁定单元格

Workbook jxlwb=null;

WritableWorkbook book=null;

try{

      jxlwb=Workbook.getWorkbook(new File(saveFile));   

      book=Workbook.createWorkbook(new File(saveFile),jxlwb);

      WritableSheet sheet = book.getSheet(0); 

      for(int i=0;i<cellList.size();i++){

             String[] point=(String[])cellList.get(i);  

             int rowNum =Integer.parseInt(point[0].trim())-1;

             int colNum =Integer.parseInt(point[1].trim())-1;

             //去除可写单元格锁定状态

             WritableCell wCell = sheet.getWritableCell(colNum, rowNum);

             CellFormat cf = wCell.getCellFormat();

             WritableCellFormat wcf = new WritableCellFormat(cf);

             wcf.setLocked(false);

             wCell.setCellFormat(wcf);

            

      }

      //  打开sheet保护

      sheet.getSettings().setProtected(true);

      book.write();

}catch(Exception e){

}finally{ 

book.close();

jxlwb.close();

原文地址:https://www.cnblogs.com/shiGuangShiYi/p/10117506.html