Asp.net two ways to export an EXCEL file

Source: Internet
Author: User

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.

Related Article

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.