Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. reflection;
Using system. Windows. forms;
Namespace pal_mes.brlibrary
{
Class cdealexcel
{
Public void exporttoexcel (datatable DT)
{
Excel. Application xlapp = new excel. Application ();
Excel. Workbook xlbook = xlapp. application. workbooks. Add (true );
Excel. worksheet xlsheet = (Excel. worksheet) xlbook. activesheet;
Xlsheet. Name = "result ";
Excel. Range RA;
Xlapp. Visible = true;
Xlapp. usercontrol = false;
Xlapp. displayalerts = false;
Int irow = DT. Rows. count;
Int icol = DT. Columns. count;
String [,] x = new string [irow, 1];
Int I = 1;
Foreach (datacolumn DC in DT. columns)
{
Xlsheet. cells [1, I] = DT. Columns [I-1]. Caption. tostring ();
Int J = 0;
Foreach (datarow DR in DT. Rows)
{
X [J, 0] = Dr [I-1]. tostring ();
J = J + 1;
}
Ra = xlsheet. get_range (xlsheet. cells [2, I], xlsheet. cells [irow + 1, I]);
RA. value2 = X;
RA. entirecolumn. autofit ();
I = I + 1;
}
Ra = xlsheet. get_range (xlsheet. cells [1, 1], xlsheet. cells [DT. Rows. Count + 1, DT. Columns. Count]);
RA. Borders. linestyle = excel. xllinestyle. xlcontinuous;
RA. Font. Name = "Arial ";
RA. Font. size = 10;
Ra = xlsheet. get_range (xlsheet. cells [1, 1], xlsheet. cells [1, DT. Columns. Count]);
RA. Interior. colorindex = 45;
RA. horizontalalignment = excel. xlhalign. xlhaligncenter;
Xlsheet = NULL;
Xlbook = NULL;
Xlapp = NULL;
GC. Collect ();
}
Public datatable getsheetfromexcel (string filename)
{
Missing Miss = missing. value;
Datatable dtsheet = new datatable ();
Excel. Application xlapp = new excel. Application ();
Excel. workbook xlbook = xlapp. workbooks. open (filename, miss, miss );
Dtsheet. Columns. Add ("sheetname ");
For (INT I = 1; I <= xlbook. Sheets. Count; I ++)
{
Dtsheet. Rows. Add (Excel. worksheet) xlbook. worksheets [I]). Name. tostring ());
}
Xlapp. Quit ();
Xlbook = NULL;
Xlapp = NULL;
Return dtsheet;
}
}
}
Call Statement: cexcel. exporttoexcel (DT); cexcel class name.