为什么 Excel 将单元格视为受保护的,即使它们不受保护? [英] Why Excel treating cells as protected even if they are not?

查看:48
本文介绍了为什么 Excel 将单元格视为受保护的,即使它们不受保护?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了保护并隐藏了MasterData"表,我从中创建了一些命名范围,我在第二个表customerAssets"中使用了该范围.这是代码片段:

I have provided protection and also hide the sheet 'MasterData' out of which I have created some named range which i have used in second sheet 'customerAssets'. Here is code snippet :

    workbook.setSheetHidden(0,  true);  //to hides masterData
    workbook.setActiveSheet(1);          // sets active sheet as Customer Assets Sheet
    masterDataSheet.protectSheet("12345");  // protect MasterData sheet

但是打开excel后:在此处输入图片描述

But after opening excel: enter image description here

  1. 它不允许编辑客户资产"表,说它受保护.
  2. 但如果我打开Sheet1",然后打开Customer Assets",它允许编辑.

它可能有什么问题?

推荐答案

Excel 或 Calc 中的工作表可以同时处于活动状态(视图中您面前的工作表)和已选中(可以选择多个工作表作为一组).

A sheet in Excel or Calc can be both, active (the sheet in front of you in the view) and selected (more than one sheet can be selected as a group).

第一个创建的工作表将始终处于活动状态并被选中.因此,如果您的 MasterData 工作表是第一个创建的工作表,则它处于活动状态并被选中.workbook.setActiveSheet 会更改活动状态,但不会更改选定状态.因此,您的 MasterData 表保持选中状态.活动单元格中的更改将始终应用于所有选定的工作表.因此,您确实尝试更改受保护的单元格,因为还选择了受保护的 MasterData 表.

The first created sheet will always be both active and selected. So if your MasterData sheet is the first created sheet, then it is active and selected. The workbook.setActiveSheet changes the active state but not the selected state. So your MasterData sheet stays selected. Changes in the active cell will always be applied to all selected sheets. So you do really try to change a protected cell, since the protected MasterData sheet is selected also.

如果您通过单击鼠标选择单个工作表,则选择将被更改,并且不再选择选定的工作表组.

If you select single sheets with a mouse click, then the selection will be changed and the selected group of sheets will no more be selected.

我们至少需要取消选择 MasterData 表.但我们也应该选择另一张纸.

We need at least to unselect the MasterData sheet. But we should also select another sheet too.

import java.io.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

public class CreateSheets {

 public static void main(String[] args) throws Exception {
  Workbook workbook = new XSSFWorkbook();
  Sheet masterDataSheet = workbook.createSheet("MasterData"); //first sheeet will be both active and selected
  Sheet customerAssetsSheet = workbook.createSheet("Customer Assets");
  Sheet sheet1 = workbook.createSheet("Sheet1");

  workbook.setSheetHidden(0,  true);       //hide masterDataSheet
  masterDataSheet.setSelected(false);      //unselect masterDataSheet

  workbook.setActiveSheet(1);              //sets active sheet as Customer Assets Sheet
  //customerAssetsSheet.setSelected(true); //not necessary but recommended: set Customer Assets Sheet selected

  masterDataSheet.protectSheet("12345");   // protect MasterData sheet

  FileOutputStream fileOut = new FileOutputStream("CreateSheets.xlsx");
  workbook.write(fileOut);
  fileOut.close();
  workbook.close();
 }
}

这篇关于为什么 Excel 将单元格视为受保护的,即使它们不受保护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆