c#Winform表單 自動產生EXCEL並可以插入資料

來源:互聯網
上載者:User

標籤:drawing   mode   for   ring   row   bsp   tables   產生   linq   


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表單的控制多引用他人代碼並加以修改。

引用表單的圖下次補上  沒有來得及附圖...

c#Winform表單 自動產生EXCEL並可以插入資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.