从Datatable中的多个表中获取索引数组

时间:2017-09-19 15:06:06

标签: c# arrays matrix datatable

我正在构建一个应用程序,该应用程序转到excel以获取工作表中的多个表,众所周知,这些表被空列分隔。我将工作表导入数据表,现在我正在尝试构建索引数组以获取表的限制并导出到其他数据表。

ArrIndex[0,0] = 1,1
ArrIndex[0,1] = 1,3
ArrIndex[1,0] = 4,0
ArrIndex[1,1] = 4,4

数据表是这样的:

  A  B  C  D    A  B  C  D  E    A  B  C
1 V  V  V  V    V  V  V  V  V    V  V  V
2 V  V  V  V    V  V  V  V  V    V  V  V
3 V  V  V  V    V  V  V  V  V    V  V  V
4 V  V  V  V    V  V  V  V  V    V  V  V

现在我想要第一个表的边界来填充像这样的新数据表:

  A  B  C  D 
1 V  V  V  V 
2 V  V  V  V 
3 V  V  V  V 
4 V  V  V  V 

此时痘痘混乱 我有这个功能:

public void ImportTablesToSQL(DataTable DT)
        {
            //Array to know when start and end etch table
            int[,] TablesIndex = new int[1, 1];

            int[,] ColumnBefore = new int[1,1];
            int counteri = 1;
            int counterj = 1;
            //Get the index of tables



            for (int j = 0; j>=DT.Columns.Count; j++)
                {

                if ((counteri == 0) && counterj == 0 )
                {
                    TablesIndex[counteri, counterj] = {0,0};

                }

                    if ((DT.Rows[0][j].ToString() == "") && (DT.Rows[0][j - 1].ToString() != "") &&( DT.Rows[0][j + 1].ToString() != ""))

                {
                    //recive bounders of table

                    TablesIndex[counteri, counterj] = j;
                }
                }  
        }

1 个答案:

答案 0 :(得分:0)

几个小时后,我的回归能够解决。 如果有人需要,我会在这里发布我的解决方案:

DT.Rows.Add(); //Add new line to get last line of table without errors
        DT.Rows.Add(); //Add new line to get last line of table without errors
        DT.Columns.Add(); //Add new column to get last column wihout errors
        DT.Columns.Add();//Add new column to get last column wihout errors

        //Array to know when start and end etch table
        int[,] TablesIndex = new int[1, 6]; //Array to save matriz of each table
        //[X,0] = Coluna inicial
        //[X,1] = Linha inicial
        //[X,2] = Coluna final
        //[X,3] = Linha final

        int x = 1;
        int y = 1;
        bool Entra = true;
        bool UltimaTabela = false;
        //Get the index of tables



        for (int j = 1; j<=DT.Columns.Count-2; j++)
            {

            if ((x == 1) && (y == 1) && (Entra == true))
            {
                //Coluna inicio
                TablesIndex[0, 0] = 0;
                //Linha Inicio
                TablesIndex[0 , 1] = 4;
                Entra = false;
            }
            else if (Entra == true)
            {
                //Coluna Fim tabela anterior
                TablesIndex[x-1 , 0] = TablesIndex[x-2, 2] + 2;
                //Linha Inicio
                TablesIndex[x-1, 1] = 4;
                Entra = false;


            }

                if (DT.Rows[5][j - 1].ToString() == "Total")//é sempre o ultima coluna

            {
                //se nao for a ultima tabela
                if ((DT.Rows[5][j].ToString() == "")&&(DT.Rows[5][j + 1].ToString() != ""))
                {
                    //Coluna Fim
                    TablesIndex[x - 1, 2] = j - 1;
                }

                //Se for a ultima tabela 
                if (DT.Columns.Count - 2 == j)
                {
                    //Coluna Fim
                    TablesIndex[x - 1, 2] = j-1;
                    UltimaTabela = true;
                }

                for (int k =1; k<= DT.Rows.Count; k++)
                {

                    if((DT.Rows[k][TablesIndex[x-1, 2]].ToString() == "") && (DT.Rows[k-1][TablesIndex[x-1, 2]].ToString() != "") && (DT.Rows[k+1][TablesIndex[x-1, 2]].ToString() == "") || (DT.Columns.Count - 1 == j))
                    {
                        DT.Rows.Add();
                        //Linha Fim
                        TablesIndex[x-1, 3] = k;

                        if (UltimaTabela == false )
                        { 
                        x += 1;
                        int[,] temp = new int[x, 6];
                        if (TablesIndex != null)
                        {
                            Array.Copy(TablesIndex, temp, Math.Min(TablesIndex.Length, temp.Length));
                            TablesIndex = temp;
                            Entra = true;
                        }
                        }
                        break;

                    }

            }

            }

        }