使用Aspose.Cells实现导入导出
本文实例为大家分享了Aspose.Cells实现导入导出的具体代码,供大家参考,具体内容如下
这是自己整理的导入导出类,里面有注释。
usingSystem; usingSystem.Collections.Generic; usingSystem.Data; usingSystem.IO; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingAspose.Cells; namespaceLzd.Mvc.EasyUi.Common.ExcelUtil { /// ///excel操作基类 /// /// publicclassBaseExcelUtil { privateWorkbookm_Wb=null; /// ///生成Excel /// ///模板Excel的路径+文件名 ///Excel文件的字节对象 publicbyte[]CreateExcel(stringurl) { FileStreamfs=null; try { //读取模板Excel文件的中内容 fs=newFileStream(url,FileMode.Open,FileAccess.Read,FileShare.Read); m_Wb=newWorkbook(); m_Wb.Open(fs); setValue(m_Wb); //转换为字节对象并返回 returnm_Wb.SaveToStream().ToArray(); } catch(Exceptionex) { throwex; } finally { fs.Close(); } } /// ///设定Excel中的数据 ///数据源为datable类型 /// ///工作簿 publicvirtualvoidsetValue(Workbookwb) { thrownewException("Themethodoroperationisnotimplemented."); } /// ///读取Excel /// ///Excel的路径+文件名 ///Excel文件的字节对象 publicDataTableGetExcel(stringurl) { FileStreamfs=null; try { //读取Excel文件的中内容 fs=newFileStream(url,FileMode.Open,FileAccess.Read,FileShare.Read); m_Wb=newWorkbook(); m_Wb.Open(fs); //设定Excel中的数据 returngetValue(m_Wb); } finally { fs.Close(); } } /// ///取得Excel中的数据 /// ///工作簿 publicvirtualDataTablegetValue(Workbookwb) { thrownewException("Themethodoroperationisnotimplemented."); } /// ///设置字符串值 /// /// /// publicvoidputValue(Cellc,objectvalue) { try { if(value==null||object.Equals(value,DBNull.Value)||value.ToString().Trim().Length==0) { } else { c.PutValue(value.ToString()); } } catch(Exception) { c.PutValue("--"); } } /// ///设置数值值 /// /// /// publicvoidputValueDouble(Cellc,objectvalue) { 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()); } } /// ///设置日期值 /// /// /// publicvoidputDateValue(Cellc,objectvalue) { 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()); } } } }
////实现基类
usingSystem; usingSystem.Collections.Generic; usingSystem.Data; usingSystem.Linq; usingSystem.Reflection; usingSystem.Text; usingSystem.Threading.Tasks; usingAspose.Cells; namespaceLzd.Mvc.EasyUi.Common.ExcelUtil { /// ///Excel帮助类 /// publicclassExcelUtil:BaseExcelUtil { privateDataTabledt; privatestringtitle; publicExcelUtil(){ } /// ///从第几行开始读取 /// publicintFirstRow{get;set;} /// ///从第几列开始读取 /// publicintFirstColumns{get;set;} /// ///excel标题 /// publicstringTitle { get{returntitle;} set{title=value;} } privatestringfileName; /// ///文件名 /// publicstringFileName { get{returnfileName;} set{fileName=value;} } publicDataTableDt { get{returndt;} set{dt=value;} } publicboolFlag { set; get; } /// /// ///导出设定值 publicoverridevoidsetValue(Workbookwb) { intindex=0; Worksheetws=null; intrcount=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); inti=1; for(intj=0;j/////导出调用方法
publicActionResultToExcel(){ Listlist=newList(); for(inti=0;i<100;i++) { UserInfoinfo=newUserInfo(); info.Age=i.ToString(); info.ID=i; info.Name="姓名"+i; list.Add(info); } ///将list类型转换为datatable DataTabledt=DataTableHelper.IListToDataTable(list); //实例化帮助类 ExcelUtilexc=newExcelUtil(); exc.Dt=dt; exc.FileName="导出测试.xls"; exc.Title="导出测试"; //需要写入的模板 stringurl=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(); returnContent("ss"); }///导入调用方法
publicActionResultImportExcel() { stringurl=Server.MapPath("/Content/Down/Import.xls"); ExcelUtilexc=newExcelUtil(); exc.FirstRow=1; exc.FirstColumns=0; DataTabledt=exc.GetExcel(url); returnContent("ss"); }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。