【问题标题】:Get array of index from multiple tables inside Datatable从 Datatable 内的多个表中获取索引数组
【发布时间】:2017-09-19 15:06:05
【问题描述】:

我正在构建一个应用程序,该应用程序可以通过 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;
                }
                }  
        }

【问题讨论】:

  • 所以你已经尝试了一些东西,但你卡在了某个地方。是什么,为什么你不能完成这个任务?你能展示你的尝试吗?然后我们可以帮助修复它
  • 不清楚您要做什么,如果没有您迄今为止尝试过的代码示例,我们将无法为您提供帮助。
  • 我已经输入了我拥有的代码。我的问题是将eatch表的限制放入数据表中

标签: c# arrays matrix datatable


【解决方案1】:

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

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;

                    }

            }

            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多