You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms如何从文本/Excel文件读取数据填充30×30数组?

适配C# WinForms的数组文件读取方案

以下是两种完全替代硬编码数组的实现方案,适配你的30×30二维数组需求:


方案一:使用文本文件存储(无额外依赖)

1. 生成初始文本文件

先运行一次导出代码,把原数组内容写入文本文件(生成后可注释掉该方法):

// 仅第一次执行,生成初始数据文件
private void ExportDefaultToText()
{
    int[,] defaultGen = 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},};
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 30; i++)
    {
        List<string> rowValues = new List<string>();
        for (int j = 0; j < 30; j++)
        {
            rowValues.Add(defaultGen[i, j].ToString());
        }
        sb.AppendLine(string.Join(",", rowValues));
    }
    File.WriteAllText("CurrentGen.txt", sb.ToString());
}

2. 从文本文件读取数据

在窗体加载事件中读取文件,填充CurrentGen:

public int[,] CurrentGen;
private int[,] NextGen = new int[30, 30];

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        CurrentGen = new int[30, 30];
        string[] lines = File.ReadAllLines("CurrentGen.txt");
        
        if (lines.Length != 30)
            throw new Exception("文本文件需包含30行数据");
        
        for (int i = 0; i < 30; i++)
        {
            string[] values = lines[i].Split(',', StringSplitOptions.RemoveEmptyEntries);
            if (values.Length != 30)
                throw new Exception($"第{i+1}行需包含30个数值");
            
            for (int j = 0; j < 30; j++)
            {
                if (!int.TryParse(values[j], out int val))
                    throw new Exception($"第{i+1}行第{j+1}列不是有效整数");
                
                CurrentGen[i, j] = val;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"读取失败:{ex.Message}\n将使用默认数组", "错误提示");
        //  fallback到原默认数组
        CurrentGen = new int[,] { /* 此处复制原数组内容 */ };
    }
}

方案二:使用Excel表格存储(需EPPlus库)

1. 安装依赖

在Visual Studio中右键项目 → 管理NuGet程序包 → 搜索EPPlus(推荐4.x版本,无商业授权限制)并安装。

2. 生成初始Excel文件

运行一次导出代码生成初始Excel:

private void ExportDefaultToExcel()
{
    int[,] defaultGen = new int[,] { /* 复制原数组内容 */ };
    using (var package = new ExcelPackage())
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("CurrentGen");
        for (int i = 0; i < 30; i++)
        {
            for (int j = 0; j < 30; j++)
            {
                worksheet.Cells[i+1, j+1].Value = defaultGen[i, j];
            }
        }
        package.SaveAs(new FileInfo("CurrentGen.xlsx"));
    }
}

3. 从Excel读取数据

在窗体加载事件中读取Excel:

public int[,] CurrentGen;
private int[,] NextGen = new int[30, 30];

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        CurrentGen = new int[30, 30];
        FileInfo file = new FileInfo("CurrentGen.xlsx");
        using (var package = new ExcelPackage(file))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets["CurrentGen"];
            if (worksheet == null)
                throw new Exception("Excel中不存在名为CurrentGen的工作表");
            
            if (worksheet.Dimension.Rows != 30 || worksheet.Dimension.Columns != 30)
                throw new Exception("Excel工作表需为30行30列");
            
            for (int i = 0; i < 30; i++)
            {
                for (int j = 0; j < 30; j++)
                {
                    if (!int.TryParse(worksheet.Cells[i+1, j+1].Text, out int val))
                        throw new Exception($"第{i+1}行第{j+1}列不是有效整数");
                    
                    CurrentGen[i, j] = val;
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"读取失败:{ex.Message}\n将使用默认数组", "错误提示");
        CurrentGen = new int[,] { /* 复制原数组内容 */ };
    }
}

注意事项

  • 数据文件需放在程序输出目录(如bin/Debug),或使用绝对路径指定位置
  • 异常处理可避免文件损坏时程序崩溃
  • 若导师要求强制使用外部文件,可移除fallback的默认数组逻辑

内容的提问来源于stack exchange,提问作者Ant123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 05:23:09