zoukankan      html  css  js  c++  java
  • 文件导出 Aspose

    /// <summary>
    /// 使用Aspose读取Excel数据
    /// </summary>
    public class ExcelHelperCore
    {
    /// <summary>
    ///
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="fields"></param>
    /// <param name="list"></param>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static OperationResult ExportExcel<T>(Dictionary<string, string> fields, IEnumerable<T> list, string fileName)
    where T : new()
    {
    if (fields == null || fields.Count == 0)
    {
    return new OperationResult(OperationResultType.ParamError, "先指定字段");
    }

    var workbook = new Workbook();
    var sheet = workbook.Worksheets[0];
    var cells = sheet.Cells;
    Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
    style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
    style.Font.Name = "宋体";//文字字体
    style.Font.Size = 10;//文字大小
    style.Font.IsBold = true;//粗体
    int i = 0;
    foreach (KeyValuePair<string, string> keyValuePair in fields)
    {
    cells[0, i].SetStyle(style); //给单元格关联样式
    cells.SetRowHeight(0, 18); //设置行高
    cells.SetColumnWidth(i, 16);//设置列宽
    //列头
    cells[0, i].PutValue(keyValuePair.Value);
    i++;
    }

    PropertyInfo[] properties = new T().GetType().GetProperties();
    int row = 1;
    foreach (T item in list)
    {
    i = 0;
    foreach (KeyValuePair<string, string> keyValuePair in fields)
    {
    foreach (PropertyInfo property in properties)
    {
    if (property.Name == keyValuePair.Key)
    {
    if (property.PropertyType.Name.StartsWith("DateTime"))
    {
    var time = Convertor.ConvertToDateTime(property.GetValue(item), DateTime.MinValue);
    if (time == DateTime.MinValue || time < new DateTime(2010, 1, 1))
    {
    cells[row, i].PutValue("");
    }
    else
    {
    cells[row, i].PutValue(time.ToString("yyyy-MM-dd HH:mm"));
    }
    }
    else
    {
    cells[row, i].PutValue(property.GetValue(item));
    }
    }
    }

    i++;
    }
    row++;
    }

    workbook.Save(HttpContext.Current.Response, fileName, ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Xlsx));
    return new OperationResult(OperationResultType.Success, "导出成功");
    }

    /// <summary>
    /// 表单导出
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="colums"></param>
    /// <param name="property"></param>
    /// <param name="list"></param>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static OperationResult ExportExcelForm<T>(List<string> colums, string property, IEnumerable<IEnumerable<T>> list, string fileName)
    where T : new()
    {
    if (colums == null || colums.Count == 0)
    {
    return new OperationResult(OperationResultType.ParamError, "先指定字段");
    }

    var workbook = new Workbook();
    var sheet = workbook.Worksheets[0];
    var cells = sheet.Cells;
    Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
    style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
    style.Font.Name = "宋体";//文字字体
    style.Font.Size = 10;//文字大小
    style.Font.IsBold = true;//粗体
    int i = 0;
    #region collumn set
    foreach (var c in colums)
    {
    cells[0, i].SetStyle(style); //给单元格关联样式
    cells.SetRowHeight(0, 18); //设置行高
    cells.SetColumnWidth(i, 16);//设置列宽
    //列头
    cells[0, i].PutValue(c);
    i++;
    }
    #endregion

    if (null == list || !list.Any())
    {
    return new OperationResult(OperationResultType.ParamError, "导出内容不能为空");
    }

    PropertyInfo[] properties = new T().GetType().GetProperties();
    int row = 1;
    foreach (IEnumerable<T> lst in list)
    {
    i = 0;
    foreach (T item in lst)
    {
    if (i > colums.Count)
    {
    break;
    }
    foreach (PropertyInfo p in properties)
    {
    if (p.Name == property)
    {
    cells[row, i].PutValue(p.GetValue(item));
    }
    }
    i++;
    }
    row++;
    }

    workbook.Save(HttpContext.Current.Response, fileName, ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Xlsx));
    return new OperationResult(OperationResultType.Success, "导出成功");
    }

    public static DataTable ExcelToDatatalbe(String strFileName)//导入
    {
    Workbook book = new Workbook(strFileName);
    Worksheet sheet = book.Worksheets[0];
    Cells cells = sheet.Cells;
    //获取excel中的数据保存到一个datatable中
    DataTable dt_Import = cells.ExportDataTableAsString(1, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false);
    // dt_Import.
    return dt_Import;
    }
    }

  • 相关阅读:
    laravel 5.5 仓库模式 文件之间接口与实现操作
    php 无线分类 根据子级找父级
    php 无限极分类,根据父级 找子级
    laravel5.4 中 dd和dump的区别。
    laravel hash密码生成和密码验证
    oracle建表详细信息
    关于组件的认识
    java的Thread Dump诊断工具
    weblogic连接池
    详解Oracle数据字典
  • 原文地址:https://www.cnblogs.com/zlj-rechio/p/9953662.html
Copyright © 2011-2022 走看看