码迷,mamicode.com
首页 > 其他好文 > 详细

动态列 Excel 导出

时间:2017-03-30 19:17:39      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:ksh   attr   response   ram   数据导出   horizon   动态   save   foreach   

/// <summary>
/// 导出数据通用属性 需在属性进行标记
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter | AttributeTargets.Property)]
public class ExportToExcelAttribute : Attribute
{
  private string _columnName;
  private int _width = 19;

  public ExportToExcelAttribute(string columnName)
  {
    this._columnName = columnName;
  }

  public ExportToExcelAttribute(string columnName, int columnWidth)
  {
    this._columnName = columnName;
    this._width = columnWidth;
  }

  public string ColumnName { get { return _columnName; } }
  public int ColumnWidth { get { return _width; } }

}

 

public class ClassA

{

     int id =0;

     string name=string.Empty;

  [ExportToExcel("ID", 25)]
  public int Id
  {
    get { return Id; }
    set { Id= value; }
  }

  [ExportToExcel("Name", 25)]
  public int Id
  {
    get { return Id; }
    set { Id= value; }
  }

}

public class ExcelHelper

{

  public void CreateReportToExcel()

  {

    var data = new List<ClassA>()

    {

      new ClassA(){Id=1,Name="张三“},new ClassA(){Id=2,Name="张四”}

    };

    var expressList = new List<Expression<Func<ClassA, object>>>() {{m=>m.Id},{m=>m.Name}};

    Aspose.Cells.Workbook workbook=GetExportWorkbook<ClassA>(expressList,data,"ExcelSheet1");

 

    

    Response.BinaryWrite(workbook.SaveToStream().ToArray());
    Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode("" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", System.Text.Encoding.UTF8) + "\"");
    Response.ContentType = "application/ms-excel";

  }

 

  /// <summary>
  /// 数据导出到excel通用方法
  /// </summary>
  /// <typeparam name="T"></typeparam>
  /// <param name="expressList"></param>
  /// <param name="datas"></param>
  /// <param name="WorksheetName"></param>
  /// <returns></returns>
  public static Workbook GetExportWorkbook<T>(List<Expression<Func<T, object>>> expressList, List<T> datas, string WorksheetName)
  {
  List<PropertyInfo> proList = new List<PropertyInfo>();//字段属性集合
  PropertyInfo pro;
  ExportToExcelAttribute att;

  Workbook work = new Workbook();
  work.Worksheets.Clear();
  Worksheet sheet = null;

  if (datas.Count > 0)
  {
    work.Worksheets.Add(WorksheetName);
    sheet = work.Worksheets[0];
    sheet.Cells.SetRowHeight(0, 30);
    int i = 0;
    foreach (var item in expressList)
    {
      pro = GetProperty(item);
      proList.Add(pro);
      att = pro.GetCustomAttribute<ExportToExcelAttribute>();
      sheet.Cells[0, i++].PutValue(att.ColumnName);
      sheet.Cells.SetColumnWidth(i - 1, att.ColumnWidth);
    }

    //单元格
    Cells cells = sheet.Cells;
    Style style1 = work.Styles[work.Styles.Add()];
    style1.HorizontalAlignment = TextAlignmentType.Center;    
    style1.Font.Name = "宋体";
    style1.Font.Size = 11;
    style1.IsLocked = true;
    style1.Font.IsBold = true;

    int n = 1;
      foreach (T item in datas)
      {
        int j = 0;
        foreach (var p in proList)
        {  
          sheet.Cells[n, j++].PutValue(p.GetValue(item));
        }
        n += 1;
      }
     }

     return work;

   }

  }

 

动态列 Excel 导出

标签:ksh   attr   response   ram   数据导出   horizon   动态   save   foreach   

原文地址:http://www.cnblogs.com/newnj/p/6647354.html

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