Method records for ASP. NET DataTable Export Excel (Third party)

Source: Internet
Author: User

Official website: http://npoi.codeplex.com/

Simple application, the main is to achieve the simple effect we want, hehe

Need to introduce DLL, can be downloaded on the official website, can also be downloaded below

C # code
  1. protected void Getexcel (DataTable dt)
  2. {
  3. Npoi. HSSF. Usermodel.hssfworkbook book = new Npoi. HSSF.  Usermodel.hssfworkbook ();
  4. Npoi. Ss. Usermodel.sheet Sheet = Book.  Createsheet ("test_01");
  5. Npoi. Ss. Usermodel.row Row = sheet. CreateRow (0);
  6. For (int i = 0; i < dt. Columns.count; i++)
  7. {
  8. Row. Createcell (i). Setcellvalue (dt. Columns[i]. ColumnName);
  9. }
  10. For (int i = 0; i < dt. Rows.Count; i++)
  11. {
  12. Npoi. Ss. Usermodel.row row2 = sheet. CreateRow (i + 1);
  13. For (int j = 0; j < dt. Columns.count; J + +)
  14. Row2. Createcell (j). Setcellvalue (dt. ROWS[I][J]. ToString ());
  15. }
  16. //write to client
  17. System.IO.MemoryStream ms = new System.IO.MemoryStream ();
  18. Book. Write (MS);
  19. Response.AddHeader ("Content-disposition", string. Format ("attachment;  Filename=emptyworkbook.xls "));
  20. Response.BinaryWrite (Ms. ToArray ());
  21. book = null;
  22. Ms. Close ();
  23. Ms. Dispose ();
  24. }

Excel Import

C # code
  1. Hssfworkbook Hssfworkbook;
  2. #region
  3. Public DataTable importexcelfile (string filePath)
  4. {
  5. #region//initialization information
  6. Try
  7. {
  8. using (FileStream file = new FileStream (FilePath, FileMode.Open, FileAccess.Read))
  9. {
  10. Hssfworkbook = new Hssfworkbook (file);
  11. }
  12. }
  13. catch (Exception e)
  14. {
  15. throw E;
  16. }
  17. #endregion
  18. Npoi. Ss. Usermodel.sheet Sheet = Hssfworkbook. Getsheetat (0);
  19. System.Collections.IEnumerator rows = Sheet. Getrowenumerator ();
  20. DataTable dt = new DataTable ();
  21. For (int j = 0; J < (sheet. GetRow (0). Lastcellnum); J + +)
  22. {
  23. Dt. Columns.Add (Convert.tochar (((int)' A ') + j).  ToString ());
  24. }
  25. While (rows. MoveNext ())
  26. {
  27. Hssfrow row = (hssfrow) rows. Current;
  28. DataRow dr = dt. NewRow ();
  29. For (int i = 0; i < row. Lastcellnum; i++)
  30. {
  31. Npoi. Ss. Usermodel.cell Cell = row. Getcell (i);
  32. if (cell = = null)
  33. {
  34. Dr[i] = null;
  35. }
  36. Else
  37. {
  38. Dr[i] = cell. ToString ();
  39. }
  40. }
  41. Dt. Rows.Add (DR);
  42. }
  43. return DT;
  44. }
  45. #endregion

This allows you to read an Excel file and return a DataTable

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.