当前位置: 首页 > news >正文

Unity 将Excel表格中的数据导入到Mysql数据表中

1.Mysql数据表users如下:

2.即将导入的Excel表格如下:

3.代码如下:

using System;
using System.Data;
using System.IO;
using Excel;
using MySql.Data.MySqlClient;
using UnityEngine;
using UnityEditor;public class ImportExcel
{// 数据库连接字符串,需根据实际情况修改private const string ConnectionString = "server=localhost;database=mygamedb;uid=root;pwd=123456;";[MenuItem("Excel操作/导入Excel数据到MySQL")]static void ImportExcelToMySQL(){string excelPath = Application.streamingAssetsPath + "/学生信息.xlsx";try{// 读取Excel数据DataRowCollection dataRows = ReadExcel(excelPath);if (dataRows != null && dataRows.Count > 0){// 连接MySQL数据库并插入数据InsertDataToMySQL(dataRows);}}catch (Exception e){Debug.LogError("导入数据时出错:" + e.Message);}}private static DataRowCollection ReadExcel(string path){try{using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read)){IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);DataSet result = excelReader.AsDataSet();return result.Tables[0].Rows;}}catch (Exception e){Debug.LogError("读取Excel文件时出错:" + e.Message);return null;}}private static void InsertDataToMySQL(DataRowCollection dataRows){using (MySqlConnection connection = new MySqlConnection(ConnectionString)){try{connection.Open();// 查询当前最大的id值string str_总数据条数 = "SELECT COUNT(*) AS total_records FROM users";MySqlCommand maxIdCommand = new MySqlCommand(str_总数据条数, connection);object obj_总数据条数 = maxIdCommand.ExecuteScalar();Debug.Log(Convert.ToInt32(obj_总数据条数));// 插入语句包含id字段,数据库自增长会自动赋值string insertQuery = "INSERT INTO users (id, username, password, registerdate) VALUES (@id, @username, @password, @registerdate)";using (MySqlCommand command = new MySqlCommand(insertQuery, connection)){// 从第二行开始插入数据,跳过标题行for (int i = 1; i < dataRows.Count; i++){DataRow row = dataRows[i];command.Parameters.Clear();command.Parameters.AddWithValue("@id", Convert.ToInt32(obj_总数据条数)+i);command.Parameters.AddWithValue("@username", row[0]);command.Parameters.AddWithValue("@password", row[1]);// 处理日期格式,假设Excel中的日期格式可直接转换DateTime registerDate;if (DateTime.TryParse(row[2].ToString(), out registerDate)){command.Parameters.AddWithValue("@registerdate", registerDate);}else{Debug.LogWarning("无法解析日期:" + row[2] + ",使用默认值");command.Parameters.AddWithValue("@registerdate", DateTime.Now);}command.ExecuteNonQuery();}}Debug.Log("数据成功导入到MySQL数据库");}catch (Exception e){Debug.LogError("插入数据到MySQL时出错:" + e.Message);}}}
}

相关文章:

  • 4.23刷题记录(栈与队列专题)
  • 将输入帧上下文打包到下一个帧的预测模型中用于视频生成
  • RocketMQ 核心架构速览
  • Elasticsearch复习笔记
  • 数据集 | 苹果目标检测数据集
  • 车载功能测试-车载域控/BCM控制器测试用例开发流程【用例导出方法+优先级划分原则】
  • 如何避免AI编造虚假文献
  • CSS-跟随图片变化的背景色
  • 从GPT-5到Claude 3:大模型竞赛的下一站是什么?
  • 【ROS2】机器人操作系统安装到Ubuntu简介
  • 基于STM32、HAL库的MCP42010T数字电位器驱动程序设计
  • WebGL简介
  • rust编程学习(三):8大容器类型
  • Transformer 零基础实践教程 - 0 - 前言与环境配置
  • Rust 学习笔记:Rust 简介
  • element-ui transfer 组件源码分享
  • 远程控制Firefox浏览器实例的挑战与Playwright的CDP和Selenium Marionette解决方案
  • STM32 SysTick定时器
  • ElasticSearch:高并发场景下如何保证读写一致性?
  • 11、认识redis的sentinel
  • 《亡命驾驶》:一场对于男子气概的终极幻想
  • 与包乐史驾帆航行|航海、钓鱼和写书:一个记者的再就业之路
  • 中方警告韩国公司不要向美军工企业出口含中国稀土矿物产品?外交部回应
  • 首映|国家自博馆4D电影《海洋深深》:潜入深海向地球发问
  • 神舟二十号主要目的发布,在空间站驻留约6个月
  • 深一度|坚守17年,这件事姚明就算赔钱也在继续做