Use Npoi to export the information in the database to an Excel table and prompt the user to download

Source: Internet
Author: User

Using Npoi to export Excel tables Basically, I find it very rare to export Excel forms and prompt them to download.

The simple code is as follows

1         //MVC projects can pass multiple IDs with comma-separated strings2          PublicActionResult Execl (stringIDs)3         {4list<paylog> list =NewList<paylog>();5             string[] idsstring = IDs. Split (New Char[] {','}, Stringsplitoptions.removeemptyentries);//Split String6              for(intj =0; J < Idsstring. Length; J + +)7             {8                 stringstr =Idsstring[j];9List. ADD (DbSession.PayLogRepository.Fetch (x = X.finvoiceid = = str));//link Database Read data objectTen             } OneHssfworkbook work =NewHssfworkbook ();//Create page AHssfsheet sheet = work. Createsheet ();//Create Columns -Hssfrow row = sheet. CreateRow (0);//First line -Row. Createcell (0, hssfcell.cell_type_string). Setcellvalue ("Serial Number"); theRow. Createcell (1, hssfcell.cell_type_string). Setcellvalue ("Coding"); -             //looping object collections creating data Columns -              for(inti =0; I < list. Count; i++) -             { +Hssfrow rows = Sheet. CreateRow (i +1); -Rows. Createcell (0, hssfcell.cell_type_string). Setcellvalue (List[i]. Fpayinno); +Rows. Createcell (1, hssfcell.cell_type_string). Setcellvalue (List[i]. Fenterpriseid); A             } at             stringPath =@"f:\ information. xls";//the project should be changed to a relative path instead of an absolute path -             using(FileStream file =NewFileStream (path, filemode.create)) -             { - Work . Write (file); -             } -             returnFile (NewFileStream (Path, FileMode.Open),"Application/ms-excel","information. xls");

Results

Use Npoi to export the information in the database to an Excel table and prompt the user to download

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.