使用npoi導出Excel下載

   前面寫過一篇關於用npoi讀取數據導入的方法,剛纔工作中碰到一個小功能,把數據導出下載。這裏把NPOI官方的一個列子貼出來。

   

  1. HSSFWorkbook hssfworkbook = new HSSFWorkbook(); 
  2.            Sheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); 
  3.  
  4.             sheet1.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample"); 
  5.             int x = 1; 
  6.             for (int i = 1; i <= 15; i++) 
  7.             { 
  8.                 Row row = sheet1.CreateRow(i); 
  9.                 for (int j = 0; j < 15; j++) 
  10.                 { 
  11.                     row.CreateCell(j).SetCellValue(x++); 
  12.                 } 
  13.             } 
  14.  
  15.             MemoryStream file = new MemoryStream(); 
  16.             hssfworkbook.Write(file); 
  17.                         Response.BinaryWrite(file.GetBuffer()); 
  18.             Response.End(); 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章