Public void exporttoexcel ()
{
Datatable dt = getdatatable ();
If (Dt = NULL)
{
MessageBox. Show ("no data is available for export! "," Prompt ", messageboxbuttons. OK, messageboxicon. information );
Return;
}
Microsoft. Office. InterOP. Excel. Application xlapp = new Microsoft. Office. InterOP. Excel. Application ();
If (xlapp = NULL)
{
MessageBox. Show ("an Excel object cannot be created. Excel may not be installed on your computer ");
Return;
}
System. Windows. Forms. savefiledialog savedia = new savefiledialog ();
Savedia. Filter = "Excel file (*. xls) | *. xls ";
Savedia. Title = "export as an Excel file ";
If (savedia. showdialog () = system. Windows. Forms. dialogresult. OK
&&! String. Empty. Equals (savedia. filename ))
{
Microsoft. Office. InterOP. Excel. workbooks = xlapp. workbooks;
Microsoft. Office. InterOP. Excel. Workbook workbook = workbooks. Add (Microsoft. Office. InterOP. Excel. xlwbatemplate. xlwbatworksheet );
Microsoft. Office. InterOP. Excel. worksheet = (Microsoft. Office. InterOP. Excel. worksheet) Workbook. worksheets [1]; // get sheet1
Microsoft. Office. InterOP. Excel. Range = NULL;
Long totalcount = DT. Rows. count;
Long rowread = 0;
Float percent = 0;
String filename = savedia. filename;
// Write the title
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Worksheet. cells [1, I + 1] = DT. Columns [I]. columnname;
Range = (Microsoft. Office. InterOP. Excel. Range) worksheet. cells [1, I + 1];
// Range. Interior. colorindex = 15; // background color
Range. Font. Bold = true; // bold
Range. Font. size = 11; // font size
Range. horizontalalignment = Microsoft. Office. InterOP. Excel. xlhalign. xlhaligncenter; // center
// Add a border
Range. borderaround (Microsoft. office. interOP. excel. xllinestyle. xlcontinuous, Microsoft. office. interOP. excel. xlborderweight. xlthin, Microsoft. office. interOP. excel. xlcolorindex. xlcolorindexautomatic, null );
// Range. columnwidth = 4.63; // you can specify the column width.
// Range. entirecolumn. autofit (); // automatically adjust the column width
// R1.entirerow. autofit (); // automatically adjust the Row Height
}
// Write content
For (INT r = 0; r <DT. defaultview. Count; r ++)
{
This. state. TEXT = "exporting data ...... exported "+ (R + 1 ). tostring () + "entries/total" + datagridview1.rows. count. tostring () + "entries ";
This. progressbar1.value = R + 1;
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Worksheet. cells [R + 2, I + 1] = DT. defaultview [r] [I];
Range = (Microsoft. Office. InterOP. Excel. Range) worksheet. cells [R + 2, I + 1];
Range. Font. size = 10; // font size
// Add a border
Range. borderaround (Microsoft. office. interOP. excel. xllinestyle. xlcontinuous, Microsoft. office. interOP. excel. xlborderweight. xlthin, Microsoft. office. interOP. excel. xlcolorindex. xlcolorindexautomatic, null );
Range. entirecolumn. autofit (); // automatically adjusts the column width.
}
Rowread ++;
Percent = (float) (100 * rowread)/totalcount;
System. Windows. Forms. application. doevents ();
}
Range. Borders [microsoft. Office. InterOP. Excel. xlbordersindex. xlinsidehorizontal]. Weight = Microsoft. Office. InterOP. Excel. xlborderweight. xlthin;
If (Dt. Columns. Count> 1)
{
Range. Borders [microsoft. Office. InterOP. Excel. xlbordersindex. xlinsidevertical]. Weight = Microsoft. Office. InterOP. Excel. xlborderweight. xlthin;
}
Workbook. Saved = true;
Workbook. savecopyas (filename );
Workbooks. Close ();
If (xlapp! = NULL)
{
Xlapp. workbooks. Close ();
Xlapp. Quit ();
Int Generation = system. gc. getgeneration (xlapp );
System. runtime. interopservices. Marshal. releasecomobject (xlapp );
Xlapp = NULL;
System. gc. Collect (generation );
}
GC. Collect (); // forcibly destroy
# Region forcibly kills the recently opened Excel Process
System. Diagnostics. Process [] excelproc = system. Diagnostics. process. getprocessesbyname ("Excel ");
System. datetime starttime = new datetime ();
Int M, killid = 0;
For (m = 0; m <excelproc. length; m ++)
{
If (starttime <excelproc [M]. starttime)
{
Starttime = excelproc [M]. starttime;
Killid = m;
}
}
If (excelproc [killid]. hasexited = false)
{
Excelproc [killid]. Kill ();
}
# Endregion
If (MessageBox. Show (this, "data has been successfully exported to:" + savedia. filename. tostring () + ", open the file? "," Exported ", messageboxbuttons. yesno, messageboxicon. Information) = dialogresult. Yes)
{
System. Diagnostics. process. Start (savedia. filename );
}
}
Else
{
GC. Collect (); // forcibly destroy
# Region forcibly kills the recently opened Excel Process
System. Diagnostics. Process [] excelproc = system. Diagnostics. process. getprocessesbyname ("Excel ");
System. datetime starttime = new datetime ();
Int M, killid = 0;
For (m = 0; m <excelproc. length; m ++)
{
If (starttime <excelproc [M]. starttime)
{
Starttime = excelproc [M]. starttime;
Killid = m;
}
}
If (excelproc [killid]. hasexited = false)
{
Excelproc [killid]. Kill ();
}
# Endregion
}
Panel2.visible = false;
}
# Endregion
C # implement the content progress bar of the Excel table