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

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
posted @ 2016-11-15 17:39  甜菜波波  阅读(3023)  评论(0编辑  收藏  举报