使用Aspose.Cells实现导入导出

时间:2022-09-12 13:08:52

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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());
      }
    }
 
 
  }
  
}

////实现基类 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells;
namespace Lzd.Mvc.EasyUi.Common.ExcelUtil
{
  ///
  /// Excel帮助类
  ///
  public class ExcelUtil :BaseExcelUtil
  {
    private DataTable dt;
    private string title;
   
    public ExcelUtil() {
      
 
    }
 
    ///
    /// 从第几行开始读取
    ///
    public int FirstRow { get; set; }
    ///
    /// 从第几列开始读取
    ///
    public int FirstColumns { get; set; }
 
    ///
    /// excel标题
    ///
    public string Title
    {
      get { return title; }
      set { id="codetool">

/////导出调用方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public ActionResult ToExcel() {
      List list = new List();
      for (int i = 0; i < 100; i++)
      {
        UserInfo info = new UserInfo();
        info.Age = i.ToString();
        info.ID = i;
        info.Name = "姓名" + i;
        list.Add(info);
      }
      ///将list类型转换为datatable
      DataTable dt= DataTableHelper.IListToDataTable(list);
      //实例化帮助类
      ExcelUtil exc = new ExcelUtil();
      exc.Dt = dt;
      exc.FileName = "导出测试.xls";
      exc.Title = "导出测试";
      //需要写入的模板
      string url = Server.MapPath("/Content/Down/template.xls");
      byte[] data = exc.CreateExcel(url);
      //浏览器下载文件
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + exc.FileName);//HttpUtility.UrlEncode(r.FileName, Encoding.UTF8));
      Response.ContentType = "application/ms-excel";
      Response.AddHeader("Content-Length", data.Length.ToString());
      Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
      Response.BinaryWrite(data);
      System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
      return Content("ss");
    }

///导入调用方法

?
1
2
3
4
5
6
7
8
9
10
11
12
public ActionResult ImportExcel()
    {
      string url = Server.MapPath("/Content/Down/Import.xls");
      ExcelUtil exc = new ExcelUtil();
      exc.FirstRow = 1;
      exc.FirstColumns = 0;
       DataTable dt= exc.GetExcel(url);
      
   
 
      return Content("ss");
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/lzd1164961158/article/details/77073867

延伸 · 阅读

精彩推荐