C# 连接EXCEL 文本框保存输入信息
部分代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsFormsApp8 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string Path = textBox1.Text.Trim(); string sql = textBox2.Text.Trim(); //string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(connStr); conn.Open(); OleDbDataAdapter myCommand = null; DataSet ds = null; myCommand = new OleDbDataAdapter(sql, connStr); ds = new DataSet(); myCommand.Fill(ds, "table1"); dataGridView1.DataSource = ds.Tables[0]; //dataGridView1.Columns["username"].Visible = false; //dataGridView1.RowHeadersVisible = false; //button2.Enabled = false; dataGridView1.Columns["UserId"].ReadOnly = true; conn.Close(); } private void Form1_Load(object sender, EventArgs e) { textBox1.Text = Properties.Settings.Default.defaultPath; textBox2.Text = Properties.Settings.Default.defaultSQL; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Properties.Settings.Default.defaultPath = textBox1.Text.Trim(); Properties.Settings.Default.defaultSQL = textBox2.Text.Trim(); Properties.Settings.Default.Save(); } private void button2_Click(object sender, EventArgs e) { string msg = string.Format("第{0}行,第{1}列", dataGridView1.CurrentCell.RowIndex, dataGridView1.CurrentCell.ColumnIndex); richTextBox1.Text = msg+"\n值为:"+dataGridView1.CurrentCell.Value.ToString(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!