如何检查Excel单元格是否被锁定?

发布于 2024-07-25 23:46:57 字数 3277 浏览 7 评论 0原文

如何检查该范围内的特定单元格是否被锁定,

我正在发布我的代码的小片段,请建议我,如果单元格未锁定,则写入单元格的更好方法。

if (reader.HasRows) { minRow = 0; 最小颜色 = 0;

                                // Process each result in the result set
                                while (reader.Read())
                                {
                                    // Create an array big enough to hold the column values
                                    object[] values = new object[reader.FieldCount];

                                    // Add the array to the ArrayList
                                    rowList.Add(values);

                                    // Get the column values into the array
                                    reader.GetValues(values);

                                    int iValueIndex = 0;
                                    int jValueIndex = 1;

                                    // If the Reading Format is by ColumnByColumn 
                                    if (CurTaskNode.ReadFormat == "ColumnbyColumn")
                                    {
                                        minCol = 0;
                                        int lengthHeader = 0;
                                        if (CurTaskNode.ReadHeader == true)
                                        {
                                            lengthHeader = CurTaskNode.HEADER_MAX_ROW - CurTaskNode.HEADER_MIN_ROW;
                                        }
                                        else
                                        {
                                            lengthHeader = CurTaskNode.HeaderData.Length;

                                        }
                                        for (int iCol = 0; iCol < lengthHeader; iCol++)
                                        {

                                            // Checking whether the Header data exists or not
                                            if (CurTaskNode.HeaderData[minCol] != "")
                                            {
                                                //if (!(excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]).Locked)
                                                //{

                                                // Assigning the Value from reader to the particular cell in excel sheet

                                                        excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex];
                                                        iValueIndex++;

                                                //}

                                            }
                                            minCol++;
                                        }
                                        minRow++;
                                    }

在代码中,写入excel单元格为

excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex];

这里我必须检查单元格是否被锁定的条件,我尝试过,但这不正确。

CurTaskNode.DATA_MIN_ROW 是要在 Excel 工作表中写入的数据的 minrow 值,该值来自 XML 文件,如果我必须从 (10, 2) 写入到 (20, 10) 单元格,其中如果第一个单元格是说锁定,我无法将其写入该单元格,我必须继续写入其他单元格。

请帮我做这件事。 谢谢,拉姆

How to Check if a particluar cell in that range is locked,

I am posting the small snippet of my code, Please suggestme , the better way of writing to cell if the cell is not locked.

if (reader.HasRows) { minRow = 0; minCol = 0;

                                // Process each result in the result set
                                while (reader.Read())
                                {
                                    // Create an array big enough to hold the column values
                                    object[] values = new object[reader.FieldCount];

                                    // Add the array to the ArrayList
                                    rowList.Add(values);

                                    // Get the column values into the array
                                    reader.GetValues(values);

                                    int iValueIndex = 0;
                                    int jValueIndex = 1;

                                    // If the Reading Format is by ColumnByColumn 
                                    if (CurTaskNode.ReadFormat == "ColumnbyColumn")
                                    {
                                        minCol = 0;
                                        int lengthHeader = 0;
                                        if (CurTaskNode.ReadHeader == true)
                                        {
                                            lengthHeader = CurTaskNode.HEADER_MAX_ROW - CurTaskNode.HEADER_MIN_ROW;
                                        }
                                        else
                                        {
                                            lengthHeader = CurTaskNode.HeaderData.Length;

                                        }
                                        for (int iCol = 0; iCol < lengthHeader; iCol++)
                                        {

                                            // Checking whether the Header data exists or not
                                            if (CurTaskNode.HeaderData[minCol] != "")
                                            {
                                                //if (!(excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]).Locked)
                                                //{

                                                // Assigning the Value from reader to the particular cell in excel sheet

                                                        excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex];
                                                        iValueIndex++;

                                                //}

                                            }
                                            minCol++;
                                        }
                                        minRow++;
                                    }

In the code, writing to excel cell is

excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex];

here I have to check the condition whether the cell is locked or not, I tried it, but this is not correct.

CurTaskNode.DATA_MIN_ROW is the minrow value for that data to write in excel sheet, this value comes from XML file and if I have to write from (10, 2) to (20, 10) , cells, in which if the first cell is say locked, I cant write it to that cell and I have to proceed writing with other cells.

Please help me in doing this. Thanks, Ramm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫漫岁月 2024-08-01 23:46:57

((Range)excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]).Locked 是查找单元格锁定属性的属性。

excelworksheet.Protection.AllowBlahBlahBlah 是获取/设置工作表保护的属性。

excelworksheet.ProtectionMode 是一个只读布尔值,用于确定是否启用了由AllowBlahBlahBlah 定义的保护。

所以我可能会用

Range r = (Range)excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]?? new Range(); // cover null
    if(
    r.Locked && // is cell locked
    excelworksheet.ProtectionMode && // is sheet protected
    !excelworksheet.Protection.AllowFormattingCells) // are locked cells not allowed to be messed with
        DoSomethingBecauseItsProtected();

((Range)excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]).Locked is the property to find a cells locked attribute.

excelworksheet.Protection.AllowBlahBlahBlah is the property to get/set a kind of protection for a worksheet.

excelworksheet.ProtectionMode is a read only boolean to find out if the protection defined by the AllowBlahBlahBlah's is enabled.

So I might use

Range r = (Range)excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]?? new Range(); // cover null
    if(
    r.Locked && // is cell locked
    excelworksheet.ProtectionMode && // is sheet protected
    !excelworksheet.Protection.AllowFormattingCells) // are locked cells not allowed to be messed with
        DoSomethingBecauseItsProtected();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文