wns9778.com_威尼斯wns.9778官网

热门关键词: wns9778.com,威尼斯wns.9778官网
wns9778.com > 计算机教程 > 动态列 Excel 导出

原标题:动态列 Excel 导出

浏览次数:79 时间:2019-05-11

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

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

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

}

    {

  if (datas.Count > 0)
  {
    work.Worksheets.Add(WorksheetName);
    sheet = work.Worksheets[0];
wns9778.com,    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);
    }

    

 

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

     string name=string.Empty;

  {

 

    var data = new List<ClassA>()

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

}

public class ExcelHelper

  }

  /// <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;

 

   }

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

 

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

    //单元格
    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;

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

  public void CreateReportToExcel()

    };

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

     return work;

{

    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";

     int id =0;

{

public class ClassA

    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;
      }
     }

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

  }

本文由wns9778.com发布于计算机教程,转载请注明出处:动态列 Excel 导出

关键词: wns9778.com

上一篇:wns9778.com:load和loads的区别

下一篇:没有了