C#winform form automatically generates Excel and can insert data

Source: Internet
Author: User


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.