如何检查Excel单元格是否被锁定?
如何检查该范围内的特定单元格是否被锁定,
我正在发布我的代码的小片段,请建议我,如果单元格未锁定,则写入单元格的更好方法。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
((Range)excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]).Locked 是查找单元格锁定属性的属性。
excelworksheet.Protection.AllowBlahBlahBlah 是获取/设置工作表保护的属性。
excelworksheet.ProtectionMode 是一个只读布尔值,用于确定是否启用了由AllowBlahBlahBlah 定义的保护。
所以我可能会用
((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