如何根据数据集中的列索引和行索引更改单元格颜色?

时间:2019-01-30 17:21:07

标签: c# ssis

我需要根据从数据集中的变量获得的列索引和行索引来更改.xlsx文件中单元格的颜色:

我实际上将列值与第二张表中的列名列表进行比较,如果它们匹配,那么我将获得列索引并对行执行相同的操作。因此,我需要根据索引在第一个工作表中突出显示该单元格。

public void Main()
{
 String FolderPath = Dts.Variables["User::Folder_To_Be_Processed_Path"].Value.ToString();
        String File_Name = Dts.Variables["User::File_Name"].Value.ToString();
        string fileFullPath = "";
        string Error_Sheet = Dts.Variables["User::Error_Sheet_Name"].Value.ToString();
        string Data_Sheet = Dts.Variables["User::Sheet_Name"].Value.ToString();
        string ColumnErrorCellsValue;
        string RowsErrorCellsValue;
        string ColumnDataCellsValue;
        string RowsDataCellsValue;
        fileFullPath = FolderPath + "\\" + "ERR_" + File_Name;
        String strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
                  + fileFullPath + ";Extended Properties=\"Excel 
        12.0;HDR=YES;IMEX=1\";";
        //MessageBox.Show(fileFullPath);
        int RowIndex = 0;
        int ColumnIndex = 0;

        Microsoft.Office.Interop.Excel.Application xlApp = new 
        Microsoft.Office.Interop.Excel.Application();

        Workbook excelBook = xlApp.Workbooks.Open(fileFullPath);
        Worksheet Data_Sheet_H = (Worksheet)excelBook.Sheets[Data_Sheet];

        string strSQLErrorColumns = "Select * From [" + Error_Sheet + 
        "AC1:AC100000]";
        string strSQLDataColumns = "Select * From [" + Data_Sheet + 
        "B1:AB1]";
        string strSQLErrorRows = "Select * From [" + Error_Sheet + 
        "A1:A100000]";
        string strSQLDataRows = "Select * From [" + Data_Sheet + 
        "A1:A100000]";
        string strSQLDataCell = "Select * From [" + Data_Sheet + 
        "B1:AB100000]";




        OleDbConnection cn = new OleDbConnection(strCn);


        OleDbDataAdapter objAdapterErrorColumns = new 
        OleDbDataAdapter(strSQLErrorColumns, cn);
        DataSet dsErrorColumns = new DataSet();
        objAdapterErrorColumns.Fill(dsErrorColumns, Error_Sheet);
        System.Data.DataTable dtErrorColumns = 
        dsErrorColumns.Tables[Error_Sheet];

        OleDbDataAdapter objAdapterDataColumns = new 
        OleDbDataAdapter(strSQLDataColumns, cn);
        DataSet dsDataColumns = new DataSet();
        objAdapterDataColumns.Fill(dsDataColumns, Data_Sheet);
        System.Data.DataTable dtDataColumns = 
        dsDataColumns.Tables[Data_Sheet];

        OleDbDataAdapter objAdapterErrorRows = new 
        OleDbDataAdapter(strSQLErrorRows, cn);
        DataSet dsErrorRows = new DataSet();
        objAdapterErrorRows.Fill(dsErrorRows, Error_Sheet);
        System.Data.DataTable dtErrorRows = dsErrorRows.Tables[Error_Sheet];

        OleDbDataAdapter objAdapterDataRows = new 
        OleDbDataAdapter(strSQLDataRows, cn);
        DataSet dsDataRows = new DataSet();
        objAdapterDataRows.Fill(dsDataRows, Data_Sheet);
        System.Data.DataTable dtDataRows = dsDataRows.Tables[Data_Sheet];


        int iCntD = 0;
        int iCntE = 0;
        foreach (DataColumn ColumnData in dtDataColumns.Columns)
        {
            ColumnDataCellsValue = 
        dtDataColumns.Columns[iCntD].ColumnName.ToString();

            iCntE = 0;
            foreach (DataRow ColumnError in dtErrorColumns.Rows)
            {
                ColumnErrorCellsValue = 
        dtErrorColumns.Rows[iCntE].ItemArray[0].ToString();


                if (ColumnDataCellsValue.Equals(ColumnErrorCellsValue)) 
        ColumnIndex = 
        ColumnData.Table.Columns[ColumnDataCellsValue].Ordinal;



                iCntE = iCntE + 1;
           }

            iCntD = iCntD + 1;

       }

        int jCntD = 0;
        int jCntE = 0;
        foreach (DataRow rowDataID in dtDataRows.Rows)
        {               
            RowsDataCellsValue = 
        dtDataRows.Rows[jCntD].ItemArray[0].ToString();

            iCntE = 0;
            foreach (DataRow rowErrorID in dtErrorRows.Rows)
            {              

                RowsErrorCellsValue = 
        dtErrorRows.Rows[jCntE].ItemArray[0].ToString();


                if (RowsDataCellsValue.Equals(RowsErrorCellsValue)) RowIndex 
        = dtDataRows.Rows.IndexOf(rowDataID);


                iCntE = iCntE + 1;   
            }
                iCntD = iCntD + 1;

        }


       ((Range)Data_Sheet_H.Cells[RowIndex, ColumnIndex]).Interior.Color = 
       ColorTranslator.ToOle(Color.Red);

        excelBook.Save();
        excelBook.Close();
        xlApp.Quit();

        Marshal.ReleaseComObject(xlApp);


        Dts.TaskResult = (int)ScriptResults.Success;

        }

谢谢您的帮助。

0 个答案:

没有答案