是否可以解锁受保护工作表中的特定单元格(使用Aspose)

时间:2013-08-01 14:30:11

标签: java excel spreadsheet aspose-cells

我正在使用Aspose-Cells和java在我的系统中导出excel模板。

在这种特殊情况下,我正在生成一个电子表格,其中有两张我想要保护的表格。 在其中一个中,我需要让用户只编辑4个单元格。其余的都应该受到保护。 最简单的实现应该是:

  1. 保护表
  2. 解锁我想让用户编辑的每个单元格。
  3. 问题在于我正在搜索是否可以执行此操作(保护整张纸并仅解锁几个单元格)并且似乎不可能。请..告诉我我错了,有办法做到这一点,否则我将必须锁定工作表中的所有现有单元格,只解锁其中的4个..对于我使用另一个库(PHPExcel)的经验,它在性能方面似乎非常昂贵(我必须将它应用于1000行和40多列,因此它的成本非常高)。

1 个答案:

答案 0 :(得分:2)

使用Aspose.Cells for Java可以轻松完成。你可以

  1. 首先锁定工作表中的所有列(所有单元格)
  2. 解锁特定细胞或细胞范围
  3. 请参阅下面的示例。

    String dataDir = "D:\\data\\";
    // Create or load workbook
    Workbook book = new Workbook();
    
    // Get the first worksheet
    Worksheet sheet = book.getWorksheets().get(0);
    
    Style style;
    StyleFlag flag = new StyleFlag();
    
    // First lock all columns
    for (int iCol=0 ; iCol<255 ; iCol++)
    {
        // Get style of the column
        style = sheet.getCells().getColumns().get(iCol).getStyle();
        // Apply locking to the style
        style.setLocked(true);
        flag.setLocked(true);
        sheet.getCells().getColumns().get(iCol).applyStyle(style, flag);
    }
    
    // Get the range of cells, which we want to unlock
    Range rangeUnlocked = sheet.getCells().createRange("A1:D4");
    // Add a new style
    int styleIndex = book.getStyles().add();
    Style styleUnlocked = book.getStyles().get(styleIndex);
    // Unlock cells
    styleUnlocked.setLocked(false);
    rangeUnlocked.setStyle(styleUnlocked);
    
    // Protect the sheet
    sheet.protect(ProtectionType.ALL);
    
    //Save the Excel file
    book.save(dataDir + "protectedrange.xlsx");
    

    我作为开发者布道者在Aspose工作。