Improved reading of Excel Data to the datagridview (added the Excel export function)

Source: Internet
Author: User

 

Code
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Web;
Using system. IO;

Namespace windowsapplication1
{
Public partial class form2: Form
{
Public form2 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
This. openfiledialog1.showdialog ();
This. textbox1.text = This. openfiledialog1.filename;
}

Private void button2_click (Object sender, eventargs E)
{
String filename = "";
Filename = This. textbox1.text;
If (this. textbox1.text! = "")
{
Try
{
String strcon = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + filename + "; extended properties = Excel 8.0 ";
System. Data. oledb. oledbconnection myconn = new system. Data. oledb. oledbconnection (strcon );
String strcom = "select * from [punishcase $]";
System. Data. oledb. oledbdataadapter mycommand = new system. Data. oledb. oledbdataadapter (strcom, myconn );
System. Data. datatable dt = new system. Data. datatable ();
Mycommand. Fill (DT );
This. Maid = DT;
This. label1.text = myconn. state. tostring ();
MessageBox. Show ("imported successfully ");

}
Catch
{
MessageBox. Show ("select Excel for case import ");

}



}
Else
{
MessageBox. Show ("select an Excel file ");
}



}

Private void button3_click (Object sender, eventargs E)
{
Savefiledialog = new savefiledialog ();

Savefiledialog. Filter = "execl files (*. xls) | *. xls ";

Savefiledialog. filterindex = 0;

Savefiledialog. restoredirectory = true;

Savefiledialog. createprompt = true;

Savefiledialog. Title = "Export Excel file ";


Savefiledialog. showdialog ();


Stream mystream;

Mystream = savefiledialog. openfile ();

// Streamwriter Sw = new streamwriter (mystream, system. Text. encoding. getencoding ("gb2312 "));

Streamwriter Sw = new streamwriter (mystream, system. Text. encoding. getencoding (-0 ));

String STR = "";

Try

{

// Write the title

For (INT I = 0; I <datagridview1.columncount; I ++)

{

If (I> 0)

{

STR + = "\ t ";

}

STR + = maid [I]. headertext;

}


Sw. writeline (STR );



// Write content

For (Int J = 0; j <datagridview1.rows. Count; j ++)

{

String tempstr = "";

For (int K = 0; k <datagridview1.columns. Count; k ++)

{

If (k> 0)

{

Tempstr + = "\ t ";

}

Tempstr + = maid [J]. cells [K]. value. tostring ();

}



Sw. writeline (tempstr );

}

Sw. Close ();

Mystream. Close ();

}

Catch

{



}

Finally

{

Sw. Close ();

Mystream. Close ();

}

}



}
}

 

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.