c#Winform窗体 自动生成EXCEL并可以插入数据


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Windows.Forms;

using System.Data.OleDb;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;

namespace EPAS.f06PreData//自己命名
{
public partial class frmSetThreshold : Form
{
public frmSetThreshold()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//按钮一进行动态创建excel
{
try
{
if (File.Exists("E:\\example.xls"))
File.Delete("E:\\example.xls");
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=example.xls;Extended Properties='Excel 8.0;HDR=Yes'"))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("CREATE TABLE booksheet1 ([测项ID] varchar, [名字] varchar,[最大阈值] varchar,[最小阈值] varchar,[备注] varchar )", conn);
cmd.ExecuteNonQuery();
MessageBox.Show("创建excle成功");
}
}
catch (System.Exception )
{
MessageBox.Show("阈值.xls已存在!");
}
}
private void button2_Click(object sender, EventArgs e)//按钮二点击插入数据
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
{
MessageBox.Show("输入值不能为空!");
}
else
{
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=example.xls;Extended Properties='Excel 8.0;HDR=Yes'";//HDR=Yes表示第一行作为标题使用
OleDbConnection conn = new OleDbConnection(connstring);
string commandstring = "SELECT * FROM [booksheet1]";
OleDbDataAdapter adapter = new OleDbDataAdapter(commandstring, conn);
OleDbCommandBuilder cd = new OleDbCommandBuilder(adapter);
DataSet ds = new DataSet();
try
{
conn.Open();
adapter.Fill(ds, "ww");
DataRow new_row1 = ds.Tables["ww"].NewRow();
new_row1["测项ID"] = textBox1.Text;
new_row1["名字"] = textBox2.Text;
new_row1["最大阈值"] = textBox3.Text;
new_row1["最小阈值"] = textBox4.Text;
new_row1["备注"] = textBox5.Text;

ds.Tables["ww"].Rows.Add(new_row1);

adapter.Update(ds, "ww");
conn.Close();
MessageBox.Show("成功导入EXCEL!");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}


}
}

 

//这趟封闭开发2017-2-16至2017-2-24  我写代码的能力一般,此次c#对winform窗体的控制多引用他人代码并加以修改。

引用窗体的图下次补上  没有来得及附图...

转载于:https://www.cnblogs.com/yjm5/p/6431415.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# WinForm中,窗体间互传数据可以通过多种方式实现。一种常用的方式是使用委托和事件。以下是一种示例的实现方法: 首先,在发送数据窗体类中定义一个委托和事件,用于触发数据传递的动作。在代码的注释中,我使用了这个引用来说明代码的来源。 ```csharp // 在发送数据窗体类中 public delegate void DataPassedHandler(Student student); // 声明一个委托 public event DataPassedHandler DataPassed; // 声明一个事件 // 在发送数据的动作中触发事件 private void btnSendData_Click(object sender, EventArgs e) { // 创建一个Student对象,设置属性值 Student student = new Student(); student.Name = "张三"; student.Age = 20; student.Six = "男"; student.ClaslRoom = "一班"; student.Hoppy = "篮球"; // 触发事件,将Student对象传递给订阅了该事件的窗体 DataPassed?.Invoke(student); } ``` 然后,在接收数据窗体类中订阅该事件,并编写事件处理方法。同样,我使用了这个引用来说明代码的来源。 ```csharp // 在接收数据窗体类中 public void ReceiveData(Student student) { // 在这里处理接收到的数据 // 可以将接收到的数据显示在窗体的控件上,或者进行其他操作 // 例如,将数据显示在文本框中 txtName.Text = student.Name; txtAge.Text = student.Age.ToString(); txtSex.Text = student.Six; txtClass.Text = student.ClaslRoom; txtHobby.Text = student.Hoppy; } // 在窗体加载时订阅事件 private void Form2_Load(object sender, EventArgs e) { Form1 form1 = (Form1)this.Owner; // 获取父窗体的实例 form1.DataPassed += ReceiveData; // 订阅事件 } ``` 通过以上的代码,你可以在发送数据窗体中通过点击按钮来触发数据传递的动作,将Student对象传递给订阅了该事件的窗体,在接收数据窗体中处理接收到的数据。这样就实现了C# WinForm窗体间的数据传递。<span class="em">1</span> #### 引用[.reference_title] - *1* [c#winform窗体如何实现数据的保存和读取](https://blog.csdn.net/weixin_64740767/article/details/128329504)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值