2016-03-31 6 views
0

こんにちは私は異なる色で複数のセルをフォーマットしようとします。したがって、リストに複数のオブジェクトがある場合、範囲が変わります。私はそれがexpectetとして働く焦がす範囲を聖霊降臨祭だけの行にフェーズ6と複数のオブジェクトがある場合に、新しい色を取得しようとするとC#Excel複数のセルの色をフォーマットします

 public void writeExcelFile(SPpowerPlant powerPlant, string sqlServer, string database) 
     { 

      //Path to template 
      String tenplate = @"C:\Users\AAN\Documents\Visual Studio 2015\Projects\PowerPlants\Powerplants\bin\Debug\PROJEKTSTATUS_GESAMT_neues_Layout_template.xlsm"; 

      //open Excel (programm) 
      Excel.Application excelApplication = new Excel.Application(); 

#if DEBUG 
      excelApplication.Visible = true; 
      excelApplication.DisplayAlerts = true; 
#else 
       excelApplication.Visible = false; 
       excelApplication.DisplayAlerts = false; 
#endif 

      //Open a Excel File 
      Excel.Workbook excelWorkbook = excelApplication.Workbooks.Add(tenplate); 
      Excel._Worksheet excelSheet = excelWorkbook.ActiveSheet; 
      SPpowerPlantList powerPlantList = loadProjectsAndComponentsFromSqlServer(sqlServer, database, Convert.ToDateTime(powerPlant.timestamp), powerPlant.note); 

      //Lists for the Excel Sheet 
      List<String> projectName = new List<String>(); //1 
      List<String> phase = new List<String>();  //2 
      List<String> countryshort = new List<String>(); //3 
      List<double> projectShareWeb = new List<double>(); //4 
      List<double> mwWeb = new List<double>();  //5 
      List<double> projectProgress = new List<double>(); //6 
      List<double> mwDeveloped = new List<double>(); //7 
      List<double> yearlyYieldOfWholeProject = new List<double>(); //8 
      List<double> capexWholeProject = new List<double>(); //9 
      List<double> equityAmount = new List<double>(); //10 
      List<double> equityIrr = new List<double>(); //11 
      List<String> cod = new List<String>();  //12 
      List<String> projectmanager = new List<string>(); //13 
      List<String> changes = new List<string>(); //14 
      List<String> technology = new List<string>(); //15 
      List<String> countrylong = new List<string>(); //16 
      List<String> state = new List<string>(); //17 

      //to get the values into the lists from a powerplant 
      foreach (SPpowerPlant powerPlantItem in powerPlantList) 
      { 

       if (powerPlantItem.phase == "Phase 6") 
       { 
        ////Format the Header row to make it Bold and blue 
        excelSheet.get_Range("B4", "O4").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
        //excelSheet.get_Range("A1", "B1").Font.Bold = true; 
       } 

       projectName.Add(powerPlantItem.projectName); //1 
       phase.Add(powerPlantItem.phase); //2 
       countryshort.Add(powerPlantItem.country); //3 
       projectShareWeb.Add(powerPlantItem.projectShareWeb); //4 
       mwWeb.Add(powerPlantItem.mwWeb); //5 
       projectProgress.Add(powerPlantItem.projectProgress); //6 
       mwDeveloped.Add(powerPlantItem.mwDeveloped); //7 
       foreach (SPeconomicIndicator economicIndicatoritem in powerPlantItem.economicIndicators) 
       { 
        yearlyYieldOfWholeProject.Add(economicIndicatoritem.yearlyYieldOfWholeProject); //8 
        capexWholeProject.Add(economicIndicatoritem.capexWholeProject); //9 
        equityAmount.Add(economicIndicatoritem.equityAmount); //10 
        equityIrr.Add(economicIndicatoritem.equityIrr); //11 
       } 
       cod.Add(powerPlantItem.cod.Value.Year.ToString()); //12 
       projectmanager.Add(powerPlantItem.projectManager); //13 
       changes.Add("???"); //14 
       technology.Add(powerPlantItem.technology); //15 
       countrylong.Add(powerPlantItem.country); //16 
       state.Add(powerPlantItem.state); //17 

      } 

      //adding it to the excel sheet 
      int i = 0; 
      for (i = 0; i < powerPlantList.Count; i++) 
      { 

       excelSheet.Cells[i + 4, 2] = projectName[i]; //1 
       excelSheet.Cells[i + 4, 3] = phase[i]; //2 
       excelSheet.Cells[i + 4, 4] = countryshort[i]; //3 
       excelSheet.Cells[i + 4, 5] = projectShareWeb[i]; //4 
       excelSheet.Cells[i + 4, 6] = mwWeb[i]; //5 
       excelSheet.Cells[i + 4, 7] = projectProgress[i]; //6 
       excelSheet.Cells[i + 4, 8] = mwDeveloped[i]; //7 
       excelSheet.Cells[i + 4, 9] = yearlyYieldOfWholeProject[i]; //8 
       excelSheet.Cells[i + 4, 10] = capexWholeProject[i]; //9 
       excelSheet.Cells[i + 4, 11] = equityAmount[i]; //10 
       excelSheet.Cells[i + 4, 12] = equityIrr[i]; //11 
       excelSheet.Cells[i + 4, 13] = cod[i]; //12 
       excelSheet.Cells[i + 4, 14] = projectmanager[i]; //13 
       excelSheet.Cells[i + 4, 15] = changes[i]; //14; 
       excelSheet.Cells[i + 4, 16] = technology[i]; //15 
       excelSheet.Cells[i + 4, 17] = countrylong[i]; //16 
       excelSheet.Cells[i + 4, 18] = state[i]; //17 

      } 

      //Save the excel workbook under a different name 
      excelWorkbook.SaveAs(@"C:\Users\AAN\Documents\Visual Studio 2015\Projects\PowerPlants\Powerplants\bin\Debug\PROJEKTSTATUS_GESAMT_neues_Layout.xlsm", Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); 

      excelApplication.Quit(); 

     } 

は、ここに私のコードです。では、どうやって行を変更することができますか?したがって、フェーズ6のすべての価値は私が望む形式を得ますか?

ご協力いただきありがとうございます。ありがとうございます。

答えて

1

コードはかなり長く、ほとんどの場合、あなたの質問には関係しません。他の人がコードを自分のマシンにコピーしてデバッグしようとするように、一般的なコードサンプルとして適切な問題を抽出する方が良いでしょう。

主な問題は、最初は範囲B4:O4を割り当てている間を除いて、どこのセル範囲にも色を割り当てていないことです。したがって、フェーズ6に出くわすたびに、同じ範囲に色が割り当てられますが、配置しようとしている行は割り当てられません。

あなたが(それは少し不格好ですが、あなたが探している正確な範囲に対応していない場合があります)、このいくつかのバリエーションを試みることができる:

List<string> powerPlantList = new List<string> 
    { 
     "test", 
     "blah", 
     "phase 6", 
     "foo", 
     "bar", 
     "phase 6" 
    }; 

    Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application(); 
    Excel.Visible = true; 
    Workbook wkbk = Excel.Workbooks.Add(); 
    Worksheet sheet = wkbk.ActiveSheet; 


    int initialRow = 4; 
    for (int i = 0; i < powerPlantList.Count; i++) 
    { 
     string s = powerPlantList[i]; 
     string row = (i+ initialRow).ToString(); 
     if (s.Equals("phase 6")) 
     { 
      sheet.get_Range("B" + row, "O"+row).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
     // This is where you assign the color to the current row range 

     } 
     sheet.Cells[i + initialRow, 4] = s; 

    } 

あなたはまた、過度にあなたのアプローチを複雑にしているように見えます各powerPlantItemを解析します。タイプSPowerPlantのリストを作成してから、各PowerPlantをループしてワークシートに直接入力する方が良いでしょう。おそらく、各PowerPlantのプロパティを格納するために一連のリストを作成する必要はありません。

+0

あなたの答えは本当に素晴らしいですし、私はSPowerPlantで試してみましたが、私はうまく動作しませんでしたが、私はExcelでC#の方が良くなるように努力しています。私の仕事場では悪くないので、決してそれを見ることはありません。 – opelhatza

関連する問題