Export data table file execel file-local test successful

Source: Internet
Author: User

Export data table file execel file-local test successful

I tested it for one day, but it was done before I went to bed.

I am so happy. I hope it will be helpful to everyone.

Add reference to execel 11.0 liburary lib

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. Data. oledb;
Using system. text;
Using system. IO;
Using system. diagnostics;
Using system. reflection;

// Button event

String currentdir = system. environment. currentdirectory;
String strconnection = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + currentdir + "// database // user. mdb ;";
Oledbconnection myconn = new oledbconnection (strconnection );

Dataset objdataset = new dataset ();
Oledbconnection objconn = new oledbconnection (strconnection );
Objconn. open ();

Oledbdataadapter objadapter = new oledbdataadapter ("select top 10 * from admin", objconn );
Objadapter. Fill (objdataset );

Dataview oview = new dataview (objdataset. Tables [0]);

Datatable mytable = new datatable ();
Mytable = objdataset. Tables [0];

// Read the data table in the database
Datagrid2.datasource = oview;

Excel. Application Excel = new excel. applicationclass ();
Excel. workbooks. Add (true );
Excel. Workbook workbook = excel. workbooks [1];

Excel. Range r = excel. get_range (Excel. cells [1, 1], Excel. cells [1, 1]);
Excel. cells [1, 1] = maid ();
R. Font. Name = ""; // font
R. Font. size = 15; // font size
R. horizontalalignment = excel. xlhalign. xlhaligncenter; // horizontally centered
R. verticalalignment = excel. xlvalign. xlvaligncenter; // center vertically
For (INT I = 0; I <mytable. Rows. Count; I ++)
{
For (Int J = 0; j <mytable. Columns. Count; j ++)
{
Excel. cells [I + 2, J + 1] = mytable. Rows [I] [J]. tostring ();
}
}
Excel. Range R1 = excel. get_range (Excel. cells [2, 1], Excel. cells [mytable. Rows. Count + 1, mytable. Columns. Count]);
R1.font. Name = "";
R1.font. size = 10;
R1.horizontalalignment = excel. xlhalign. xlhaligncenter;
R1.verticalalignment = excel. xlvalign. xlvaligncenter;
R1.entirecolumn. autofit (); // automatically adjusts the column width.
R1.entirerow. autofit (); // automatically adjust the Row Height
// Add a table line
R1.cells. Borders. linestyle = excel. xllinestyle. xlcontinuous;
R1.borders. Weight = excel. xlborderweight. xlthin;
// Bold the Outer Frame
R1.borders [Excel. xlbordersindex. xledgeleft]. Weight = excel. xlborderweight. xlmedium;
R1.borders [Excel. xlbordersindex. xledgeright]. Weight = excel. xlborderweight. xlmedium;
R1.borders [Excel. xlbordersindex. xledgetop]. Weight = excel. xlborderweight. xlmedium;
R1.borders [Excel. xlbordersindex. xledgebottom]. Weight = excel. xlborderweight. xlmedium;

String SaveFilePath = txtsaveexecelpath. text;

Workbook. saveas (SaveFilePath, missing. value, missing. value, missing. value, missing. value, missing. value, Excel. xlsaveasaccessmode. xlnochange, missing. value, missing. value, missing. value, missing. value, missing. value );

Excel. Quit ();
MessageBox. Show ("The execel file is exported successfully! ");
// Fill in data


// Fill in the database
// Datagrid2.. databindings ();
Objconn. Close ();
Objconn. Dispose ();
Objconn = NULL;

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.