2009-07-01 63 views
3

我正在使用DataReader将数据写入excelsheet单元格。我没有问题,直到单元格写入previleges。但在一种情况下,只有一个单元是只读的,其余单元是可写的。如何使用DataReader在excel单元格中编写如果特定单元格是使用C#的“ReadOnly”

例如:10 * 10个单元,只有第一个单元是只读的。所以,我会离开这个细胞,并把它写入其余的细胞。但是对于数据读取器,它一次写完整行......所以我如何使用C#实现这一点?

Team Leader (required) , , , , , , , , , 
, , , , , , , , , , 
, , , , , , , , , , 
, , , , , , , , , , 
, , , , , , , , , , 

因此,第一个单元格不应该由datareader写入。 请帮我做这个

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; 

     // 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++; 
     } 
    } 
} 

谢谢 拉姆

回答

0

我尝试了很多弄清楚如何跳过以创作到使用C#特定的Excel单元格,但不可能得到正确的逻辑。

我如: 组长(需要)abcxyz用户1用户2 客户接口焦点dfgidf user23用户3

每个名字上面必须是在特定的细胞......但在Excel模板,第一个单元格(组长(必需))是只读的,所以我不能写到该小区,所以我最终的Excel工作表中应显示

      abcxyz User1 user2 
Customer Interface Focal dfgidf user23 user3..... 
..... 

我尝试过各种逻辑此...请参阅下面

代码10
Microsoft.Office.Interop.Excel.Workbook SelWorkBook = excelappln1.Workbooks.Open(
    curfile, 
    0, false, 5, "", "", false, 
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, 
    "", true, 
    false, 0, false, false, false); 

Microsoft.Office.Interop.Excel.Sheets excelSheets = SelWorkBook.Worksheets; 
Microsoft.Office.Interop.Excel.Worksheet excelworksheet =(Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(CurSheetName); 

Microsoft.Office.Interop.Excel.Range excelRange = excelworksheet.UsedRange; 

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++; 
} 

但错误显示,在if语句中 错误1操作符'!'不能应用于'object'类型的操作数

所以,请说出如何处理这种情况。

感谢 拉姆

1

发现这对谷歌,所以也许它的工作原理/也许它并不: here, code looks the same as yours

您可以使用一系列的AllowEdit属性,所以你可以这样做:

// Assigning the Value from reader to the particular cell in excel sheet  
Excel.Range c = (Excel.Range)excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]; 
if (c.AllowEdit) c.Value2 = values[iValueIndex]; 

而不是

// 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]; 
相关问题