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//own name
{
public partial class Frmsetthreshold:form
{
Public Frmsetthreshold ()
{
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)//button one to dynamically create 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 ([item ID] varchar, [first name] varchar,[maximum threshold] varchar,[minimum threshold) varchar,[ Remarks] (varchar) ", conn);
Cmd. ExecuteNonQuery ();
MessageBox.Show ("Create excle success");
}
}
catch (System.Exception)
{
MessageBox.Show ("threshold. xls already exists!") ");
}
}
private void Button2_Click (object sender, EventArgs e)//button Two click to insert Data
{
if (TextBox1.Text = = "" | | TextBox2.Text = = "" | | TextBox3.Text = = "" | | Textbox4.text = = "" | | Textbox5.text = = "")
{
MessageBox.Show ("Input value cannot be empty!") ");
}
Else
{
String connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=example.xls; Extended properties= ' Excel 8.0; Hdr=yes ' ";//hdr=yes indicates that the first row is used as the title
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["Test item id"] = TextBox1.Text;
new_row1["name"] = TextBox2.Text;
new_row1["Maximum threshold value"] = TextBox3.Text;
new_row1["minimum threshold value"] = Textbox4.text;
New_row1["Remarks"] = Textbox5.text;
Ds. Tables["WW"]. Rows.Add (NEW_ROW1);
Adapter. Update (ds, "ww");
Conn. Close ();
MessageBox.Show ("Successfully imported excel!");
}
catch (System.Exception ex)
{
MessageBox.Show (ex. ToString ());
}
}
}
}
}
This closed development from 2017-2-16 to 2017-2-24 I write code in general, the C # Control of the WinForm form more reference to other people's Code and modified.
The chart of the reference form is not time to be appended to the drawing ...
C#winform form automatically generates Excel and can insert data