首页 > 编程 > .NET > 正文

使用Aspose.Cells实现导入导出

2024-07-10 12:48:35
字体:
来源:转载
供稿:网友

本文实例为大家分享了Aspose.Cells实现导入导出的具体代码,供大家参考,具体内容如下

这是自己整理的导入导出类,里面有注释。

using System;using System.Collections.Generic;using System.Data;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using Aspose.Cells;namespace Lzd.Mvc.EasyUi.Common.ExcelUtil{  ///   /// excel操作基类  ///   ///  public  class BaseExcelUtil  {    private Workbook m_Wb = null;        ///     /// 生成Excel    ///     /// 模板Excel的路径+文件名    /// Excel文件的字节对象    public byte[] CreateExcel(string url)    {      FileStream fs = null;      try      {        //读取模板Excel文件的中内容        fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.Read);         m_Wb = new Workbook();         m_Wb.Open(fs);         setValue(m_Wb);         //转换为字节对象并返回        return m_Wb.SaveToStream().ToArray();       }      catch (Exception ex)      {        throw ex;      }      finally      {        fs.Close();      }    }      ///     /// 设定Excel中的数据     /// 数据源为datable类型    ///     /// 工作簿    public virtual void setValue(Workbook wb)    {      throw new Exception("The method or operation is not implemented.");    }             ///     /// 读取Excel    ///     /// Excel的路径+文件名    /// Excel文件的字节对象    public DataTable GetExcel(string url)    {      FileStream fs = null;      try      {        //读取Excel文件的中内容        fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.Read);         m_Wb = new Workbook();         m_Wb.Open(fs);         //设定Excel中的数据       return  getValue(m_Wb);       }      finally      {        fs.Close();      }    }     ///     /// 取得Excel中的数据    ///     /// 工作簿    public virtual DataTable getValue(Workbook wb)    {      throw new Exception("The method or operation is not implemented.");    }    ///     /// 设置字符串值    ///     ///     ///     public void putValue(Cell c, object value)    {      try      {        if (value == null || object.Equals(value, DBNull.Value) || value.ToString().Trim().Length == 0)        {         }        else        {          c.PutValue(value.ToString());        }      }      catch (Exception)      {        c.PutValue("--");      }    }    ///     /// 设置数值值    ///     ///     ///     public void putValueDouble(Cell c, object value)    {      try      {        if (value == null || object.Equals(value, DBNull.Value) || value.ToString().Trim().Length == 0)        {         }        else        {          c.PutValue(Decimal.Parse(value.ToString()));        }      }      catch (Exception)      {        c.PutValue(value.ToString());      }    }    ///     /// 设置日期值    ///     ///     ///     public void putDateValue(Cell c, object value)    {      try      {        if (value == null || object.Equals(value, DBNull.Value) || value.ToString().Trim().Length == 0)        {         }        else        {          c.PutValue(DateTime.Parse(value.ToString()));        }      }      catch (Exception)      {        c.PutValue(value.ToString());      }    }    }  }            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表