Winform export Excel

Source: Internet
Author: User

This is how to export dataset to excel. Just modify it.
1. Reference COM component excel. dll

2. The implementation is as follows:

Public void exportexcel (){
Dataset DS = This. dataset; // obtain the dataset bound to the DataGrid.
If (DS = NULL) return;

String savefilename = "";
Bool filesaved = false;
Savefiledialog savedialog = new savefiledialog ();
Savedialog. defaultext = "xls ";
Savedialog. Filter = "Excel file | *. xls ";
Savedialog. filename = "sheet1 ";
Savedialog. showdialog ();
Savefilename = savedialog. filename;
If (savefilename. indexof (":") <0) return; // canceled

Excel. Application xlapp = new excel. Application ();

If (xlapp = NULL ){
MessageBox. Show ("an Excel object cannot be created, maybe your computer has not installed excel ");
Return;
}

Excel. workbooks = xlapp. workbooks;
Excel. Workbook workbook = workbooks. Add (Excel. xlwbatemplate. xlwbatworksheet );
Excel. worksheet = (Excel. worksheet) Workbook. worksheets [1]; // get sheet1
Excel. Range range;

String oldcaption = This. captiontext;
Long totalcount = Ds. Tables [0]. Rows. count;
Long rowread = 0;
Float percent = 0;

Worksheet. cells [1, 1] = This. captiontext;
// Write Fields
For (INT I = 0; I <Ds. Tables [0]. Columns. Count; I ++ ){
Worksheet. cells [2, I + 1] = Ds. Tables [0]. Columns [I]. columnname;
Range = (Excel. Range) worksheet. cells [2, I + 1];
Range. Interior. colorindex = 15;
Range. Font. Bold = true;

}
// Write Value
This. captionvisible = true;
For (INT r = 0; r <Ds. Tables [0]. Rows. Count; r ++ ){
For (INT I = 0; I <Ds. Tables [0]. Columns. Count; I ++ ){
Worksheet. cells [R + 3, I + 1] = Ds. Tables [0]. Rows [r] [I];
}
Rowread ++;
Percent = (float) (100 * rowread)/totalcount;
This. captiontext = "exporting data [" + percent. tostring ("0.00") + "%]...";
Application. doevents ();
}
This. captionvisible = false;
This. captiontext = oldcaption;

Range = worksheet. get_range (worksheet. cells [2, 1], worksheet. cells [Ds. tables [0]. rows. count + 2, DS. tables [0]. columns. count]);
Range. borderaround (Excel. xllinestyle. xlcontinuous, Excel. xlborderweight. xlthin, Excel. xlcolorindex. xlcolorindexautomatic, null );

Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. colorindex = excel. xlcolorindex. xlcolorindexautomatic;
Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. linestyle = excel. xllinestyle. xlcontinuous;
Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. Weight = excel. xlborderweight. xlthin;

If (Ds. Tables [0]. Columns. Count> 1 ){
Range. Borders [Excel. xlbordersindex. xlinsidevertical]. colorindex = excel. xlcolorindex. xlcolorindexautomatic;
Range. Borders [Excel. xlbordersindex. xlinsidevertical]. linestyle = excel. xllinestyle. xlcontinuous;
Range. Borders [Excel. xlbordersindex. xlinsidevertical]. Weight = excel. xlborderweight. xlthin;
}

If (savefilename! = ""){
Try {
Workbook. Saved = true;
Workbook. savecopyas (savefilename );
Filesaved = true;
} Catch (exception ex ){
Filesaved = false;
MessageBox. Show ("An error occurred while exporting the file. The file may be opened! \ N "+ ex. Message );
}
} Else {
Filesaved = false;
}
Xlapp. Quit ();
GC. Collect (); // forcibly destroy
If (filesaved & file. exists (savefilename) system. Diagnostics. process. Start (savefilename );

}

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.