winforms 如何在c#表单中通过taxt文件或excel表在数组中输入数据?

inkz8wg9  于 2023-05-18  发布在  C#
关注(0)|答案(1)|浏览(296)

我正在做一个学校项目,有2个数组,第一个数组是默认数组,程序开始,第二个数组是我用来存储新数据后,程序执行,然后NextGen数组变成CurrentGen数组。它是一个30x30的网格,不同的数字对应不同的颜色。老师要求我尝试使它,而不是改变代码中的CurrentGen中的数字,我制作一个文本文件或Excel表,其中将更容易放入数字,当程序执行时,它从所述文件中获取数据,并使用它代替CurrentGen,但我以前从未做过类似的事情。有人能帮我一下吗?我用C#写,使用WinForms。

public int[,] CurrentGen = new int[,] { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,3,3,1,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,3,1,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1,1,1,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,1,3,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,2,0,2,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,3,3,3,0,0,0,0,0,0,0,0,0,0,2,2,0,2,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0},
                                         {0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0},
                                         {0,0,0,0,0,0,0,4,1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0},
                                         {0,0,0,0,0,0,0,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},};
        
        private int[,] NextGen = new int[30,30];

我试着在网上寻找类似的东西,但没有像我正在寻找的东西出现。

fkaflof6

fkaflof61#

将txt数据读到excel已经在评论区给出了有价值的评论。根据你的描述,我写了一段代码,可以读取excel数据并将其存储到int类型的二维数组中。如果我的理解不正确,请纠正:

using System.Data.OleDb;
using DataTable = System.Data.DataTable;

namespace readExcel
{
 public class program
 {
     static void Main()
     {
         //You want to read the path of excel
         string path = @"C:\Users\Administrator\Desktop\11.xlsx";

         DataTable dt = ImportExcelToDataTable2(path);

         int[,] NextGen = new int[dt.Rows.Count, dt.Columns.Count];
         //string[,] array = new string[dt.Rows.Count, dt.Columns.Count];
         for (int i = 0; i < dt.Rows.Count; i++)
         {
             for (int j = 0; j < dt.Columns.Count; j++)
             {
                 NextGen[i, j] = Convert.ToInt32(dt.Rows[i][j]);
             }
         }
     }

     public static DataTable ImportExcelToDataTable2(string path)
     {
         //Note: Think of an excel file as a database, and a sheet as a table. Syntax "SELECT * FROM [sheet1] &quot;, the form should use &quot; [ ] &quot; and &quot; ]&quot;, the form should use &quot;[]&quot; and &quot;]", the form should use "[]" and ""
         //The following two ways can realize Access database
         //string conStr = string.Format(“Provider=Microsoft.ACE.OLEDB.12.0; Data source={0}; Extended Properties=Excel 12.0;”, path);
         string conStr = "Provider = Microsoft.Ace.OLEDB.12.0; Data Source = " + path + "; Extended Properties ='Excel 12.0; HDR = Yes; IMEX = 2'";
         using (OleDbConnection conn = new OleDbConnection(conStr))
         {
             conn. Open();
             // Obtain relevant information of all Sheets (the following 2 ways can be achieved)
             //DataTable dtSheet = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
             DataTable dtSheet = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
             if (dtSheet. Rows. Count > 0)
             {
                 // Get the name of the first Sheet
                 string sheetName = dtSheet.Rows[0]["TABLE_NAME"].ToString().Trim();
                 string sql = string.Format("select * from[{0}]", sheetName);
                 using (OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn))
                 {
                     DataTable dt = new DataTable();
                     oda. Fill(dt);
                     return dt;
                 }
             }
             return null;
         }
     }
 }

}
此时,excel数据已存储在NextGen中:

相关问题