Click here to Skip to main content
16,022,846 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello and thanks in advance for the help.

I have an SQL Query getting data on technicians and some work indicators from them into a DataTable. I have that working. I then run through the table getting the cell.Value for each cell and write them in an excel file, so far so good. Problem is I need to get the values in specific cells, depending on the name of the technicians (OPERADOR in the code) because they are divided into different teams.

- datatable has all the workers and info I need
- workers are divided into teams
- I need to export to the excel file, separating workers by team and putting them in the specific table in the file. (DataTable is ordered by alphabetical order so sorting it is not necessary).

Here is my code so far

C#
string cellContent = string.Empty;

DataTable dt = dgv_CS24.DataSource as DataTable;

foreach (DataRow dRow in dt.Rows)
{
    //EQUIPA A

    cellContent = dRow["OPERADOR"].ToString();
    if (cellContent == "csalmeida" ||
        cellContent == "bmferreira" ||
        cellContent == "dleitao" ||
        cellContent == "armauricio" ||
        cellContent == "Psoares")
    {
        int i;
        var tableRC = dgv_CS24.RowCount;
        var tableCC = dgv_CS24.ColumnCount;
        var size = 5;
        var width = 1;

        for (i = 0; i <= tableCC - 1; i++)
        {
            int j;
            for (j = 0; j <= tableRC - 1; j++)
            {
                var cell = dgv_CS24[j, i];
                xlWorkSheet.Cells[i + 6, j + 2] = cell.Value;
                if (size == 5)
                {
                    width++;
                }
            }

            size++;
        }

        //EQUIPA B
       }
    else if (cellContent == "cacaetano" ||
       cellContent == "Cborralho" ||
       cellContent == "Mfiães" ||
       cellContent == "pcaraujo" ||
       cellContent == "iacasimiro" ||
       cellContent == "Rodrigues")
    {
        int i;
        var tableRC = dgv_CS24.RowCount;
        var tableCC = dgv_CS24.ColumnCount;
        var size = 6;
        var width = 1;

        for (i = 0; i <= tableCC - 1; i++)
        {
            int j;
            for (j = 0; j <= tableRC - 1; j++)
            {
                var cell = dgv_CS24[j, i];
                xlWorkSheet.Cells[i + 15, j + 2] = cell.Value;
                if (size == 6)
                {
                    width++;
                }
            }
            size++;
        }

        //EQUIPA C
    }
       else if (cellContent == "Asalgueiro" ||
       cellContent == "mltorres" ||
       cellContent == "mrodrigues" ||
       cellContent == "Ppimenta" ||
       cellContent == "rjrosa" ||
       cellContent == "Vnogueira")
    {
        int i;
        var tableRC = dgv_CS24.RowCount;
        var tableCC = dgv_CS24.ColumnCount;
        var size = 6;
        var width = 1;

        for (i = 0; i <= tableCC - 1; i++)
        {
            int j;
            for (j = 0; j <= tableRC - 1; j++)
            {
                var cell = dgv_CS24[j, i];
                xlWorkSheet.Cells[i + 24, j + 2] = cell.Value;
                if (size == 6)
                {
                    width++;
                }
            }
            size++;
        }

        //EQUIPA D
    }
    else if (cellContent == "Ccarvalho" ||
       cellContent == "falmeida" ||
       cellContent == "maleal" ||
       cellContent == "Mendes" ||
       cellContent == "micaeiro" ||
       cellContent == "Tantunes")
    {
        int i;
        var tableRC = dgv_CS24.RowCount;
        var tableCC = dgv_CS24.ColumnCount;
        var size = 6;
        var width = 1;

        for (i = 0; i <= tableCC - 1; i++)
        {
            int j;
            for (j = 0; j <= tableRC - 1; j++)
            {
                var cell = dgv_CS24[j, i];
                xlWorkSheet.Cells[i + 33, j + 2] = cell.Value;
                if (size == 6)
                {
                    width++;
                }
            }
            size++;
        }

        //EQUIPA REFORÇO
    }
    else if (cellContent == "Rpedro" ||
       cellContent == "jcoutinho" ||
       cellContent == "rjlope" ||
       cellContent == "alopes" ||
       cellContent == "Alacerda" ||
       cellContent == "acurveira" ||
       cellContent == "smteixeira" ||
       cellContent == "mjmartins")
    {
        int i;
        var tableRC = dgv_CS24.RowCount;
        var tableCC = dgv_CS24.ColumnCount;
        var size = 8;
        var width = 1;

        for (i = 0; i <= tableRC - 1; i++)
        {
            int j;
            for (j = 0; j <= tableCC - 1; j++)
            {
                var cell = dgv_CS24[j, i];
                xlWorkSheet.Cells[i + 42, j + 2] = cell.Value;
                if (size == 8)
                {
                    width++;
                }
            }
            size++;
        }

        //PT
    }
    else if (cellContent == "cestrela")
    {
        int i;
        var tableRC = dgv_CS24.RowCount;
        var tableCC = dgv_CS24.ColumnCount;
        var size = 1;
        var width = 1;

        for (i = 0; i <= tableCC - 1; i++)
        {
            int j;
            for (j = 0; j <= tableRC - 1; j++)
            {
                var cell = dgv_CS24[j, i];
                xlWorkSheet.Cells[i + 50, j + 2] = cell.Value;
                if (size == 1)
                {
                    width++;
                }
            }
            size++;
        }

       //EQUIPA POLIVALENTES
    }
    else if (cellContent == "capestana" ||
      cellContent == "etavares" ||
      cellContent == "lmalanho" ||
      cellContent == "nvital" ||
      cellContent == "nherbert" ||
      cellContent == "ppinto" ||
      cellContent == "ppombal" ||
      cellContent == "rpessoa")
    {
        int i;
        var tableRC = dgv_CS24.RowCount;
        var tableCC = dgv_CS24.ColumnCount;
        var size = 8;
        var width = 1;

        for (i = 0; i <= tableCC - 1; i++)
        {
            int j;
            for (j = 0; j <= tableRC - 1; j++)
            {
                var cell = dgv_CS24[j, i];
                xlWorkSheet.Cells[i + 53, j + 2] = cell.Value;
                if (size == 8)
                {
                    width++;
                }
            }
            size++;
        }
    }


and here is what that exports:
https://drive.google.com/file/d/0B6ziRKPZ22ZhaXFYWTBqQ3hpcEE/edit?usp=sharing[^]
Posted
Updated 18-Jul-14 5:32am
v4
Comments
Ricardo Giro Santos 18-Jul-14 10:10am    
I was thinking that I can just insert it in the Excel file in the normal DataTable order and then choose each specific cell range (datatable row) and put it in the right place in each table... but that would make excel work harder and longer.
Ricardo Giro Santos 18-Jul-14 11:23am    
I removed the size and width variables because they didn't make much sense but the same happens. It doesn't hop out of the for cycle and back to the beginning. It just runs through the whole table and then gives me the Index out of Range error.
Ricardo Giro Santos 18-Jul-14 11:57am    
Ok now I commented out the first for's (the ones with the tableCC) and left only the second for's inside each if. Now it goes on to write the first OPERADOR acurveira in the desired place (first row of EQUIPA D table) but then goes back up to the "foreach (DataRow dRow in dt.Rows)" and runs through acurveira again, re-writing the same cells. It does this 3 additional times, four in total and then gives me the Index out of Range error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900