Method 1 c # export to excel
String filename = "d: abc.xls ";
System. data. datatable dt = new system. data. datatable ();
Filestream objfilestream;
Streamwriter objstreamwriter;
String strline = "";
Objfilestream = new filestream (filename, filemode. openorcreate, fileaccess. write );
Objstreamwriter = new streamwriter (objfilestream, system. text. encoding. unicode );
For (int I = 0; I <dt. columns. count; I ++)
{
Strline = strline + dt. columns [I]. columnname. tostring () + convert. tochar (9 );
}
Objstreamwriter. writeline (strline );
Strline = "";
For (int I = 0; I <dt. rows. count; I ++)
{
Strline = strline + (I + 1) + convert. tochar (9 );
For (int j = 1; j <dt. columns. count; j ++)
{
Strline = strline + dt. rows [I] [j]. tostring () + convert. tochar (9 );
}
Objstreamwriter. writeline (strline );
Strline = "";
}
Objstreamwriter. close ();
Objfilestream. close ();
Method 2: datatable export to excel code
Private void exportexcel (datatable dt)
{
If (dt = null) return;
Microsoft. office. interop. excel. application xlapp = new microsoft. office. interop. excel. application ();
If (xlapp = null)
{
Return;
}
System. globalization. cultureinfo currentci = system. threading. thread. currentthread. currentculture;
System. threading. thread. currentthread. currentculture = new system. globalization. cultureinfo ("en-us ");
Microsoft. office. interop. excel. workbooks = xlapp. workbooks;
Microsoft. office. interop. excel. workbook = workbooks. add (microsoft. office. interop. excel. xlwbatemplate. xlwbatworksheet );
Microsoft. office. interop. excel. worksheet = (microsoft. office. interop. excel. worksheet) workbook. worksheets [1];
Microsoft. office. interop. excel. range;
Long totalcount = dt. rows. count;
Long rowread = 0;
Float percent = 0;
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;
Range. font. bold = true;
}
For (int r = 0; r <dt. rows. count; r ++)
{
For (int I = 0; I <dt. columns. count; I ++)
{
Worksheet. cells [r + 2, I + 1] = dt. rows [r] [I];
}
Rowread ++;
Percent = (float) (100 * rowread)/totalcount;
}
Xlapp. visible = true;
}
<Br> // The code is very useful.