Use NPOI to export the information in the database to an Excel table and prompt the user to download it. npoiexcel
You can use NPOI to export an Excel table. On the Internet, I seldom find an Excel table to be exported and prompt to download it.
The simple code is as follows:
1 // The mvc project can pass multiple ids separated by commas (,). string 2 public ActionResult execl (string ids) 3 {4 List <PayLog> list = new List <PayLog> (); 5 string [] idsstring = ids. split (new char [] {','}, StringSplitOptions. removeEmptyEntries); // split string 6 for (int j = 0; j <idsstring. length; j ++) 7 {8 string str = idsstring [j]; 9 list. add (DbSession. payLogRepository. fetch (x => x. FInvoiceId = str); // link to the database to read data objects 10} 11 HSSFWorkbook work = new HSSFWorkbook (); // create page 12 HSSFSheet sheet = work. createSheet (); // create column 13 HSSFRow row = sheet. createRow (0); // The first row 14 row. createCell (0, HSSFCell. CELL_TYPE_STRING ). setCellValue ("Serial Number"); 15 row. createCell (1, HSSFCell. CELL_TYPE_STRING ). setCellValue ("encoding"); 16 // create data column 17 for (int I = 0; I <list. count; I ++) 18 {19 HSSFRow rows = sheet. createRow (I + 1); 20 rows. createCell (0, HSSFCell. CELL_TYPE_STRING ). setCellValue (list [I]. FPayInNo); 21 rows. createCell (1, HSSFCell. CELL_TYPE_STRING ). setCellValue (list [I]. FEnterpriseId); 22} 23 string path = @ "F: \ .xls"; // The relative path instead of the absolute path 24 using (FileStream file = new FileStream (path, FileMode. create) 25 {26 work. write (file); 27} 28 return File (new FileStream (path, FileMode. open), "application/ms-excel", ".xls ");
Result
Net export EXCEL using NPOI, if the corresponding statistical chart is automatically generated
Use the function in the EXCEL template to write the display area! After the Excel file is generated, it will be displayed in statistics!
Winform uses NPOI to export the EXCEL file. The column name is fixed. Fill in the data in the excel file.
Here is a self-Summary of helper.