• excel导出


    //写的时候查了很多资料需求了一些帮助,一个导出ef里的数据

    Db_laienTransEntities3 db = new Db_laienTransEntities3();
    XSSFWorkbook workbook = new XSSFWorkbook();
    ISheet sheet = workbook.CreateSheet("DBSET");
    IQueryable<NikeDTCDailyReport> table= db.NikeDTCDailyReport.Where(p => true);
    DataTable dt = ToDataTable(table);
    int rowIndex = 0;
    int shheCount = 1;
    foreach (DataRow item in dt.Rows)
    {
    if (rowIndex > 10000)
    {
    save(workbook, context, sheet);
    rowIndex = 0;
    shheCount++;
    workbook = new XSSFWorkbook();
    sheet = workbook.CreateSheet("DBSET" + shheCount);
    }
    IRow dataRow = sheet.CreateRow(rowIndex);
    int rowSize=0;
    foreach (DataColumn Colums in dt.Columns)
    {
    dataRow.CreateCell(rowSize).SetCellValue(item[Colums].ToString());
    rowSize++;
    }
    rowIndex++;
    }
    MemoryStream ms = new MemoryStream();
    workbook.Write(ms);
    // sting path= MappingType("/"+"/tempfiles")
    string path = context.Server.MapPath("~/"+"/Excel文件/"+sheet.SheetName+".xlsx");
    using (FileStream fs = new FileStream(path,FileMode.Create, FileAccess.Write))
    {
    byte[] bArr = ms.ToArray();
    fs.Write(bArr, 0, bArr.Length);
    fs.Flush();
    ms.Close();
    }

    //如果数据大于10000就创建一个新的excel装进去

    private void save(XSSFWorkbook workbook,HttpContext context,ISheet sheet)
    {
    MemoryStream ms = new MemoryStream();
    workbook.Write(ms);
    // sting path= MappingType("/"+"/tempfiles")
    string path = context.Server.MapPath("~/"+"/Excel文件/"+sheet.SheetName+".xlsx");
    using (FileStream fs = new FileStream(path,FileMode.Create, FileAccess.Write))
    {
    byte[] bArr = ms.ToArray();
    fs.Write(bArr, 0, bArr.Length);
    fs.Flush();
    }
    ms.Close();
    }

    //这是一个吧list转化成datatable的一个方法

    public static DataTable ToDataTable<T>(IQueryable<T> varlist)
    {
    DataTable dtReturn = new DataTable();
    // column names
    System.Reflection.PropertyInfo[] oProps = null;
    if (varlist == null)
    return dtReturn;
    foreach (T rec in varlist)
    {
    if (oProps == null)
    {
    oProps = ((Type)rec.GetType()).GetProperties();
    foreach (System.Reflection.PropertyInfo pi in oProps)
    {
    Type colType = pi.PropertyType;
    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
    == typeof(Nullable<>)))
    {
    colType = colType.GetGenericArguments()[0];
    }
    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
    }
    }
    DataRow dr = dtReturn.NewRow();
    foreach (System.Reflection.PropertyInfo pi in oProps)
    {
    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
    (rec, null);
    }
    dtReturn.Rows.Add(dr);
    }
    return dtReturn;
    }

  • 相关阅读:
    POJ 1611 : The Suspects (普通并查集)
    深入解析 Dubbo 3.0 服务端暴露全流程
    智能搜索推荐一体化营收增长解决方案
    云上应用系统数据存储架构演进
    EMR on ACK 全新发布,助力企业高效构建大数据平台
    介绍一下闭包和闭包常用场景?
    BFC
    高斯消元学习笔记
    云虚拟主机如何设置域名解析
    Centos7.3安装openJDK
  • 原文地址:https://www.cnblogs.com/xiaojian1/p/5465418.html
Copyright © 2020-2023  润新知