如何检查一个单元格是否在EXCEL中只读使用C#

我从数据库导入数据到Excel工作表。 为此,我正在使用datareader。 Excel工作表模板有一些macros和几个公式计算,而不是正常的Excel工作表。 所以只有在允许写入特定单元格的情况下,才能将数据写入Excel表格。 如果不是,则不应导入数据。

所以对于这个,我有一个XML文件,说哪一列我应该开始写,在哪一行应该停止,我已经做了很多工作表。 但是在一张表中,该行的第一个单元是“只读”(locking),其余的是允许写入访问。

由于我使用Datareader从数据库中获取整行,因此无法写入locking的单元格,导致需要写入其他单元格。

我附上代码片段以供参考。

请帮助我这样做。

示例::

if (reader.HasRows) { minRow = 0; minCol = 0; Excel.Workbook SelWorkBook = excelAppln.Workbooks.Open(curfile, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, false, false, false); Excel.Sheets excelSheets = SelWorkBook.Worksheets; Excel.Worksheet excelworksheet = (Excel.Worksheet)excelSheets.get_Item(CurSheetName); // 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; // If the Reading Format is by ColumnByColumn if (CurTaskNode.ReadFormat == "ColumnbyColumn") { minCol = 0; // minRow = 0; for (int iCol = 0; iCol < CurTaskNode.HeaderData.Length; iCol++) { // Checking whether the Header data exists or not if (CurTaskNode.HeaderData[minCol] != "") { // 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++; }SelWorkBook.Close(true, curfile, null); 

请帮我解决这个问题。

谢谢,

拉姆

好吧,首先你需要检查第一个单元格的locking属性,然后如果它被locking了,那么将数组(所以你的整行减去第一列)locking,然后写入表单。 这里有一些代码,不一定是确切的,SLICE函数只是伪代码,在C#中有很多不同的切片数组的方法,使用你select的方法:

 if (!excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + 1].Locked ) { excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex]; iValueIndex++; } else { excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values.SLICE(iValueIndex); iValueIndex++; }