使用Aspose.Cells實現(xiàn)導(dǎo)入導(dǎo)出
本文實例為大家分享了Aspose.Cells實現(xiàn)導(dǎo)入導(dǎo)出的具體代碼,供大家參考,具體內(nèi)容如下
這是自己整理的導(dǎo)入導(dǎo)出類,里面有注釋。
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文件的字節(jié)對象
public byte[] CreateExcel(string url)
{
FileStream fs = null;
try
{
//讀取模板Excel文件的中內(nèi)容
fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.Read);
m_Wb = new Workbook();
m_Wb.Open(fs);
setValue(m_Wb);
//轉(zhuǎn)換為字節(jié)對象并返回
return m_Wb.SaveToStream().ToArray();
}
catch (Exception ex)
{
throw ex;
}
finally
{
fs.Close();
}
}
///
/// 設(shè)定Excel中的數(shù)據(jù)
/// 數(shù)據(jù)源為datable類型
///
/// 工作簿
public virtual void setValue(Workbook wb)
{
throw new Exception("The method or operation is not implemented.");
}
///
/// 讀取Excel
///
/// Excel的路徑+文件名
/// Excel文件的字節(jié)對象
public DataTable GetExcel(string url)
{
FileStream fs = null;
try
{
//讀取Excel文件的中內(nèi)容
fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.Read);
m_Wb = new Workbook();
m_Wb.Open(fs);
//設(shè)定Excel中的數(shù)據(jù)
return getValue(m_Wb);
}
finally
{
fs.Close();
}
}
///
/// 取得Excel中的數(shù)據(jù)
///
/// 工作簿
public virtual DataTable getValue(Workbook wb)
{
throw new Exception("The method or operation is not implemented.");
}
///
/// 設(shè)置字符串值
///
///
///
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("--");
}
}
///
/// 設(shè)置數(shù)值值
///
///
///
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());
}
}
///
/// 設(shè)置日期值
///
///
///
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());
}
}
}
}
////實現(xiàn)基類
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標(biāo)題
///
public string Title
{
get { return title; }
set { title = value; }
}
private string fileName;
///
/// 文件名
///
public string FileName
{
get { return fileName; }
set { fileName = value; }
}
public DataTable Dt
{
get { return dt; }
set { dt = value; }
}
public bool Flag
{
set;
get;
}
///
///
///導(dǎo)出設(shè)定值
public override void setValue(Workbook wb)
{
int index = 0;
Worksheet ws = null;
int rcount = dt.Rows.Count, columns = dt.Columns.Count;
if (dt != null && dt.Rows.Count > 0)
{
index = wb.Worksheets.AddCopy(0);
ws = wb.Worksheets[index];
ws.Name = FileName.Replace(".xls", "");
try
{
putValue(ws.Cells[0, 0], this.title);
int i = 1;
for (int j = 0; j < columns; j++)
{
putValue(ws.Cells[1, j], dt.Columns[j].ColumnName);
}
for (int j = 0; j < rcount; j++)
{
i++;
for (int h = 0; h < columns; h++)
{
putValue(ws.Cells[i, h], dt.Rows[j][h].ToString());
}
}
wb.Worksheets.RemoveAt(0);
}
catch (Exception ex)
{
throw ex;
}
}
}
///
/// 導(dǎo)入excel
///
/// 讀取的文件名
/// 從第幾行開始讀取
/// 從第幾列開始讀取
///
///
public override DataTable getValue(Workbook wb)
{
Worksheet sheet = wb.Worksheets[0];
Cells cells = sheet.Cells;
return cells.ExportDataTableAsString(FirstRow, FirstColumns, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
}
}
}
/////導(dǎo)出調(diào)用方法
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類型轉(zhuǎn)換為datatable
DataTable dt= DataTableHelper.IListToDataTable(list);
//實例化幫助類
ExcelUtil exc = new ExcelUtil();
exc.Dt = dt;
exc.FileName = "導(dǎo)出測試.xls";
exc.Title = "導(dǎo)出測試";
//需要寫入的模板
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");
}
///導(dǎo)入調(diào)用方法
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");
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
這篇文章主要給大家介紹了關(guān)于如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
asp.net類庫中添加WebService引用出現(xiàn)問題解決方法
在Web項目內(nèi)添加WebService的引用是件很簡單的事情,不過對于一些新手朋友來說,就沒有那么簡單了,因為在添加的過程中總會遇到一些困難,接下來詳細(xì)介紹如何解決,感興趣的你可不要錯過了啊2013-02-02
Asp.Net Core WebAPI使用Swagger時API隱藏和分組詳解
這篇文章主要給大家介紹了關(guān)于Asp.Net Core WebAPI使用Swagger時API隱藏和分組的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Asp.Net Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
ASP.NET Core使用Log4net實現(xiàn)日志記錄功能
這篇文章介紹了ASP.NET Core使用Log4net實現(xiàn)日志記錄功能的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
在ASP.NET Core5.0中訪問HttpContext的方法步驟
這篇文章主要介紹了在ASP.NET Core5.0中訪問HttpContext的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

