2009-07-01 4 views
3

ExcelReaderを使用してExcelシートセルにデータを書き込みます。セルに先行書き込みができるまでは問題ありません。しかし、1つのケースでは、1つのセルのみが読取り専用であり、残りのセルは書込み可能である。特定のセルがC#を使用して "ReadOnly"である場合、DataReaderを使用してExcelセルに書き込む方法

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

は、 RAMM

答えて

0

ありがとうやって私を助けてください、私はC#を使用して、特定のExcelのセルにwritinをスキップする方法を見つけ出すために多くのことを試みたが、適切なロジックを取得couldntの。

私の例:ユーザー1 user2のABCXYZ チームリーダー(必須) 顧客インターフェースフォーカルdfgidfのuser23のユーザー3

上記名のそれぞれが特定のセル内...しかし、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オペレータ '!'タイプ 'オブジェクト'のオペランドには適用できません

この場合、どのように扱うかを教えてください。

おかげ RAMM

1

はGoogleでこれを見つけたので、多分それは/多分それはない作品: 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]; 
関連する問題