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

  • 相关阅读:
    统一前后台数据交互格式
    volatile关键字 学习记录2
    利用AOP与ToStringBuilder简化日志记录
    JAVA的那些数据结构实现总结,实现,扩容说明
    JAVA中的数据结构
    对把JDK源码的一些注解,笔记
    分析下为什么spring 整合mybatis后为啥用不上session缓存
    JAVA内存关注总结,作为个程序员需要对自己系统的每块内存做到了如指掌
    写颗搜索二叉树动动脑,开启今年的旅程,新年快乐
    内存快照排查OOM,加密时错误方法指定provider方式错误引起的OOM
  • 原文地址:https://www.cnblogs.com/zlj-rechio/p/9953662.html
Copyright © 2011-2022 走看看