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

查看:144
本文介绍了为什么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",然后再打开客户资产",则可以进行编辑.

这可能是什么问题?

推荐答案

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天全站免登陆