C # How to export an Excel file .?

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. Data;
Using system. Windows. forms;
Using Microsoft. Office. InterOP. Excel;

Namespace property_management
{
Class Export
{
Private Static system. Windows. Forms. datagridview gridview;
Private Static system. Windows. Forms. progressbar toolstripprogressbar1;
Private Static timer time;
Private Static dataset objset = new dataset ();
Private Static savefiledialog = new savefiledialog ();
Private Static savefiledialog savefiledialog2 = new savefiledialog ();

Public static system. Windows. Forms. datagridview _ gridview
{
Get {return gridview ;}
Set {gridview = value ;}
}
Public static system. Windows. Forms. progressbar _ toolstripprogressbar1
{
Get {return toolstripprogressbar1 ;}
Set {toolstripprogressbar1 = value ;}
}
Public static timer _ time
{
Get {return time ;}
Set {time = value ;}
}
Public static dataset _ objset
{
Get {return objset ;}
Set {objset. Clear (); objset = value ;}
}

Public static void exporttoexcel2 ()
{
If (gridview. Rows. Count = 0)
{
MessageBox. Show ("no data is available for export! "," Prompt ", messageboxbuttons. OK, messageboxicon. information );
Return;
}
Else
{
Savefiledialog. Filter = "execl files (*. xls) | *. xls ";
Savefiledialog. filterindex = 0;
Savefiledialog. restoredirectory = true;
Savefiledialog. createprompt = true;
Savefiledialog. Title = "export file storage path ";
Savefiledialog. showdialog ();
String strname = savefiledialog. filename;
If (strname. length! = 0)
{
Toolstripprogressbar1.visible = true;
System. reflection. Missing Miss = system. reflection. Missing. value;
Microsoft. Office. InterOP. Excel. applicationclass Excel = new Microsoft. Office. InterOP. Excel. applicationclass ();
Excel. application. workbooks. Add (true );;
Excel. Visible = false; // if it is true, the Excel page is displayed during export.
If (Excel = NULL)
{
MessageBox. Show ("Excel cannot be started! "," Error ", messageboxbuttons. OK, messageboxicon. Error );
Return;
}
Microsoft. Office. InterOP. Excel. workbooks books = (Microsoft. Office. InterOP. Excel. workbooks) excel. workbooks;
Microsoft. Office. InterOP. Excel. Workbook book = (Microsoft. Office. InterOP. Excel. workbook) (books. Add (MISs ));
Microsoft. Office. InterOP. Excel. worksheet sheet = (Microsoft. Office. InterOP. Excel. worksheet) book. activesheet;
Sheet. Name = "test ";

// Generate the field name
For (INT I = 0; I <gridview. columncount; I ++)
{
Excel. cells [1, I + 1] = gridview. Columns [I]. headertext. tostring ();
}
// Fill in data
For (INT I = 0; I <gridview. rowcount; I ++)
{
For (Int J = 0; j <gridview. columncount; j ++)
{
If (gridview [J, I]. value. GetType () = typeof (string ))
{
Excel. cells [I + 2, J + 1] = "'" + gridview [J, I]. value. tostring ();
}
Else
{
Excel. cells [I + 2, J + 1] = gridview [J, I]. value. tostring ();

}
}
Toolstripprogressbar1.value + = 100/gridview. rowcount;
}
Sheet. saveas (strname, miss, Microsoft. Office. InterOP. Excel. xlsaveasaccessmode. xlnochange, miss );
Book. Close (false, miss, miss );
Books. Close ();
Excel. Quit ();
System. runtime. interopservices. Marshal. releasecomobject (sheet );
System. runtime. interopservices. Marshal. releasecomobject (book );
System. runtime. interopservices. Marshal. releasecomobject (books );
System. runtime. interopservices. Marshal. releasecomobject (Excel );
GC. Collect ();
MessageBox. Show ("data has been exported !! ");
Toolstripprogressbar1.value = 0;
Toolstripprogressbar1.visible = false;

System. Diagnostics. process. Start (strname );
}
}
}

}
}

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.