Excel import/export is compatible with all versions of the server. If EXCEL is not installed, You can import excel.

Source: Internet
Author: User

Excel import/export is compatible with all versions of the server. If EXCEL is not installed, You can import excel.

1 first, you must reference NPOI. dll (which can be downloaded online !)
2 // Import 3 public void OnSubmit () 4 {5 string path = Server. mapPath ("/upload/201410/27/2015112711020.1051.xls"); 6 FileStream fs = File. open (path, FileMode. open); 7 System. data. dataTable dt = converttoable able (fs); 8 9 foreach (DataRow row in dt. rows) 10 {11 if (row ["Mobile1"]! = Null) 12 {13 Response. write (row ["Mobile1"]. toString () + "" + row ["Mobile2"]. toString () + "14"); 15} 16} 17 Response. end (); 18} 19 20 21 // convert DataTable 22 public static DataTable ConvertToDataTable (System. IO. stream excelFileStream) 23 {24 HSSFWorkbook = new HSSFWorkbook (excelFileStream); 25 DataTable dt = new DataTable (); 26 HSSFSheet sheet = (HSSFSheet) HSSFWorkbook. getShee TAt (0); 27 System. collections. IEnumerator rows = sheet. getRowEnumerator (); 28 int n = 0; 29 while (rows. moveNext () 30 {31 HSSFRow row = (HSSFRow) rows. current; 32 if (n = 0) 33 {34 for (int I = 0; I <row. lastCellNum; I ++) 35 {36 HSSFCell cell = (HSSFCell) row. getCell (I); 37 if (cell = null) 38 continue; 39 DataColumn column = new DataColumn (cell. stringCellValue); 40 41 dt. columns. add (column ); 42} 43} 44 else 45 {46 DataRow dtRow = dt. newRow (); 47 string rValue = ""; 48 for (int I = 0, j = 0; I <row. lastCellNum; I ++) 49 {50 HSSFCell cell = (HSSFCell) row. getCell (I); 51 if (cell = null) 52 {53 dtRow [I] = ""; 54} 55 else 56 {57 dtRow [j] = cell. toString (); 58 rValue = cell. toString (); 59 j ++; 60} 61} 62 if (string. isNullOrEmpty (rValue. trim () 63 break; 64 dt. rows. add (dtRow); 65} 66 n ++; 67} 68 return dt; 69 70} 71 72 // export 73 // 74 75 // export the detail table to Excel 76 77 ///78 79 // 80 the Chinese header to be imported to Excel SQL, convert the required fields into the Chinese name 81 // 82 83 // 1-1 success-1 failure 84 protected int ExportToExcel (string SQL, HttpResponse response) 85 {86 DataTable dt = CommOtherBLL. exportToExcel (SQL); // The 87 if (dt! = Null & dt. rows. count> 0) 88 {89 StringBuilder strContent = new StringBuilder (); 90 for (int I = 0; I <dt. columns. count; I ++) 91 strContent. append (dt. columns [I] + "\ t"); 92 strContent. append ("\ n"); 93 for (int I = 0; I <dt. rows. count; I ++) 94 {95 for (int y = 0; y <dt. columns. count; y ++) 96 strContent. append (dt. rows [I] [y] + "\ t"); 97 strContent. append ("\ n"); 98} 99 response. addHeader ("Content-Disposition", "attachment; filename =" + System. web. httpUtility. urlEncode ("excel", System. text. encoding. UTF8) + ". xls "); 100 response. contentType = "application/ms-excel"; 101 response. contentEncoding = System. text. encoding. getEncoding ("GB2312"); 102 response. write (strContent. toString (); 103 response. end (); 104 return 1; 105} 106 else107 return-1; 108}

 

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.