使用Aspose.Cells实现导入导出
程序员文章站
2022-09-05 08:10:49
本文实例为大家分享了aspose.cells实现导入导出的具体代码,供大家参考,具体内容如下
这是自己整理的导入导出类,里面有注释。
using system;...
本文实例为大家分享了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()); } } } }
////实现基类
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 { 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; } /// /// ///导出设定值 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; } } } /// /// 导入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); } } }
/////导出调用方法
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"); }
///导入调用方法
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"); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: PHP设计模式之模板模式定义与用法详解
下一篇: 日常收集C#接口知识(知识全面)