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 items can pass multiple IDs separated by commas 2 public actionresult execl (string IDs) 3 {4 List<paylo g> list = new list<paylog> ();//Prepare to fill in the Excel data, Paylog can replace your own data class, here because it is the source code so did not change 5 string[] idsstring = ID S.split (new char[] {', '}, stringsplitoptions.removeemptyentries);//Split string 6 for (int j = 0; J < IDSSTRING.L Ength;                 J + +)//Find the data that needs to be poured into the list, and the data obtained from the DAL layer is also placed in list 7 {8 string str = idsstring[j]; 9 List. ADD (DbSession.PayLogRepository.Fetch (x = X.finvoiceid = = str));//This is written in MVC notation, the linked database reads the data object by}11 H Ssfworkbook work = new Hssfworkbook ();//Create Excel file object Hssfsheet sheet = work. Createsheet ();//create a page in Excel, in parentheses you can write the name of the page you want, the default sheet13 hssfrow row = sheet. CreateRow (0);//Create the first line of the current page, and row. Createcell (0, hssfcell.cell_type_string). Setcellvalue ("serial number");//Create the data on the first column of the first row on the page. Createcell (1, hssfcell.cell_type_string). Setcellvalue ("Knitting16///Loop object collection Create data columns for (int i = 0; i < list. Count; i++): {hssfrow rows = sheet. CreateRow (i + 1);//Create a second row of rows. Createcell (0, hssfcell.cell_type_string). Setcellvalue (List[i]. Fpayinno);//Create a second row with rows in the first column. Createcell (1, hssfcell.cell_type_string). Setcellvalue (List[i]. Fenterpriseid);}23 string path = @ "f:\ information. xls";//project should be changed to relative path instead of absolute path//due to not knowing the user's Excel version, the resulting file suffix is. xl S, because version 2003 of the Excel suffix named. xls,2007 is. xlsx24 using (FileStream file = new FileStream (path, FileMode.Create)//create file stream , write the Excel file that fills the data to the server's hard disk. Write (file),}28 return file (new FileStream (Path, FileMode.Open), "Application/ms-excel", "message". XL S ");//Prompt user to download files on the server

Results

Good text to the top concern me to collect the article

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.