码迷,mamicode.com
首页 > Windows程序 > 详细

(C#)利用Aspose.Cells组件导入导出excel文件

时间:2016-11-15 19:58:52      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:system   chm   foreach   oar   导出excel   append   key   app   void   

Aspose.Cells组件可以不依赖excel来导入导出excel文件:

导入:

  1. public static System.Data.DataTable ReadExcel(String strFileName)  
  2.         {  
  3.             Workbook book = new Workbook();  
  4.             book.Open(strFileName);  
  5.             Worksheet sheet = book.Worksheets[0];  
  6.             Cells cells = sheet.Cells;  
  7.               
  8.             return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);  
  9.         }        
public static System.Data.DataTable ReadExcel(String strFileName)
        {
            Workbook book = new Workbook();
            book.Open(strFileName);
            Worksheet sheet = book.Worksheets[0];
            Cells cells = sheet.Cells;
            
            return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
        }      


 

导出:

  1. private static void Export<T>(IEnumerable<T> data, HttpResponse response)  
  2.         {  
  3.             Workbook workbook = new Workbook();  
  4.             Worksheet sheet = (Worksheet)workbook.Worksheets[0];                          
  5.   
  6.             PropertyInfo[] ps = typeof(T).GetProperties();  
  7.             var colIndex = "A";  
  8.   
  9.             foreach (var p in ps)  
  10.             {  
  11.                   
  12.                     sheet.Cells[colIndex + 1].PutValue(p.Name);  
  13.                     int i = 2;  
  14.                     foreach (var d in data)  
  15.                     {  
  16.                         sheet.Cells[colIndex + i].PutValue(p.GetValue(d, null));  
  17.                         i++;  
  18.                     }  
  19.   
  20.                     colIndex = ((char)(colIndex[0] + 1)).ToString();  
  21.             }  
  22.   
  23.             response.Clear();  
  24.             response.Buffer = true;  
  25.             response.Charset = "utf-8";  
  26.             response.AppendHeader("Content-Disposition""attachment;filename=xxx.xls");  
  27.             response.ContentEncoding = System.Text.Encoding.UTF8;  
  28.             response.ContentType = "application/ms-excel";  
  29.             response.BinaryWrite(workbook.SaveToStream().ToArray());  
  30.             response.End();  
  31.         }  
private static void Export<T>(IEnumerable<T> data, HttpResponse response)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = (Worksheet)workbook.Worksheets[0];                        

            PropertyInfo[] ps = typeof(T).GetProperties();
            var colIndex = "A";

            foreach (var p in ps)
            {
                
                    sheet.Cells[colIndex + 1].PutValue(p.Name);
                    int i = 2;
                    foreach (var d in data)
                    {
                        sheet.Cells[colIndex + i].PutValue(p.GetValue(d, null));
                        i++;
                    }

                    colIndex = ((char)(colIndex[0] + 1)).ToString();
            }

            response.Clear();
            response.Buffer = true;
            response.Charset = "utf-8";
            response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls");
            response.ContentEncoding = System.Text.Encoding.UTF8;
            response.ContentType = "application/ms-excel";
            response.BinaryWrite(workbook.SaveToStream().ToArray());
            response.End();
        }


 

非常简单,好用!
 
 
http://blog.csdn.net/weiky626/article/details/7514637

(C#)利用Aspose.Cells组件导入导出excel文件

标签:system   chm   foreach   oar   导出excel   append   key   app   void   

原文地址:http://www.cnblogs.com/tiancai/p/6066617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!