C#语言MVC框架Aspose.Cells控件导出Excel表数据
更新时间:2020年6月25日 11:15 点击:2066
本文实例为大家分享了Aspose.Cells控件导出Excel表数据的具体代码,供大家参考,具体内容如下
控件bin文件下载地址
@{ ViewBag.Title = "xx"; } <script type="text/javascript" language="javascript"> function getparam() { var param = {}; param.sear = $("#sear").textbox('getValue'); return param; } //这样写是为了传参数 function print_dc() { var param = getparam();//参数 var formobj=$("<form id='form1' action='dcExcel' method='post'></from>"); //参数 $("body").append(formobj); $.each(param,function(i,o){ var input1=$("<input type='hidden' value='"+o+"' name='"+i+"'/>"); input1.appendTo(formobj); }); formobj.submit(); } </script> <input class="easyui-textbox" id="sear" data-options="width:80"/> <button type="button" class="but-primary" onclick="print_dc()"> 汇总</button>
public ActionResult dcExcel(stream sear) { DataTable dt = dal.GetKslyTj(" kd='"+sear+"'"); var stream=PrintExcelApose.PrintExcel(dt, Server.MapPath(Path.Combine("../Ex/dd/", "ddd.xls")), "xx信息"); return File(stream, "application/octet-stream", "xx信息.xls"); }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using Aspose.Cells; using System.IO; namespace YidiTutor.Common { public class PrintExcelApose { public PrintExcelApose() { } /// <summary> /// 打印Excel模板 /// </summary> /// <param name="dt">数据源Datatable</param> /// <param name="path">Excel路径</param> /// <param name="filename">导出的文件名称</param> public static byte[] PrintExcel(DataTable dt, string path, string filename) { //(&=[yddt].xh) filename = filename + ".xls"; Aspose.Cells.WorkbookDesigner designer = new Aspose.Cells.WorkbookDesigner(); dt.TableName = "yddt"; designer.Open(path); //打开Excel模板 designer.SetDataSource(dt);//设置数据源 designer.Process();//自动赋值 if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename); // designer.Save(System.Web.HttpUtility.UrlDecode(System.Text.Encoding.UTF8.GetBytes(filename),System.Text.Encoding.UTF8), Aspose.Cells.SaveType.OpenInExcel, Aspose.Cells.FileFormatType.Excel2003, HttpContext.Current.Response); return designer.Workbook.SaveToStream().ToArray(); //HttpContext.Current.Response.End(); } /// <summary> /// 打印Excel模板 /// </summary> /// <param name="dtinfo">datatable数据</param> /// <param name="dtlist">datatable数据源</param> /// <param name="dict">Dictionary数据</param> /// <param name="path">Excel地址</param> /// <param name="filename">导出文件名称</param> /// <param name="protect">是否可编辑,true不可修改,false可修改</param> public static byte[] PrintExcel(DataTable dtinfo, DataTable dtlist, Dictionary<string, string> dict, string path, string filename, bool protect = false) { filename = filename + ".xls"; Aspose.Cells.WorkbookDesigner designer = new Aspose.Cells.WorkbookDesigner(); dtlist.TableName = "yddt"; designer.Open(path); //打开Excel模板 Workbook workbook = designer.Workbook; //工作簿 if (dtinfo != null && dtinfo.Rows.Count > 0) { for (int i = 0; i < dtinfo.Rows.Count; i++) { for (int k = 0; k < dtinfo.Columns.Count; k++) { string column = dtinfo.Columns[k].ColumnName; workbook.Worksheets[0].Replace("$" + column.ToLower() + "$", dtinfo.Rows[i][column].ToString()); } } } if (dict != null) { foreach (string j in dict.Keys) { workbook.Worksheets[0].Replace("$" + j.ToLower() + "$", dict[j].ToString()); } } if (protect) workbook.Worksheets[0].Protect(ProtectionType.All, "xakj..123", ""); designer.SetDataSource(dtlist);//设置数据源 designer.Process();//自动赋值 if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename); // designer.Save(System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename)), Aspose.Cells.SaveType.OpenInExcel, Aspose.Cells.FileFormatType.Excel2003, HttpContext.Current.Response); //designer.Save(System.Web.HttpUtility.UrlDecode(System.Text.Encoding.UTF8.GetBytes(filename),System.Text.Encoding.UTF8), Aspose.Cells.SaveType.OpenInExcel, Aspose.Cells.FileFormatType.Excel2003, HttpContext.Current.Response); //HttpContext.Current.Response.End(); return designer.Workbook.SaveToStream().ToArray(); } /// <summary> /// 打印Excel模板 /// </summary> /// <param name="printexcel">填充Excel数据类</param> public static byte[] PrintExcel(PrintExcel printexcel) { printexcel.filename = printexcel.filename + ".xls"; Aspose.Cells.WorkbookDesigner designer = new Aspose.Cells.WorkbookDesigner(); printexcel.dtlist.TableName = "yddt"; designer.Open(printexcel.path); //打开Excel模板 MergeExcel merge = printexcel.merge;//合并单元格 Workbook workbook = designer.Workbook; //工作簿 if (printexcel.dtinfo != null && printexcel.dtinfo.Rows.Count > 0) { for (int i = 0; i < printexcel.dtinfo.Rows.Count; i++) { for (int k = 0; k < printexcel.dtinfo.Columns.Count; k++) { string column = printexcel.dtinfo.Columns[k].ColumnName; workbook.Worksheets[0].Replace("$" + column.ToLower() + "$", printexcel.dtinfo.Rows[i][column].ToString()); } } } if (printexcel.dict != null) { foreach (string j in printexcel.dict.Keys) { workbook.Worksheets[0].Replace("$" + j.ToLower() + "$", printexcel.dict[j].ToString()); } } if (printexcel.protect) workbook.Worksheets[0].Protect(ProtectionType.All, "xakj..123", ""); designer.SetDataSource(printexcel.dtlist);//设置数据源 designer.Process();//自动赋值 //合格单元格 if (printexcel.dtlist != null && printexcel.dtlist.Rows.Count > 0) { Aspose.Cells.Worksheet sheet = designer.Workbook.Worksheets[0]; Cells cel = sheet.Cells; List<int> column = printexcel.merge.firstColumn; int j = 1; for (int i = 0; i < printexcel.dtlist.Rows.Count; i++) { for (int kk = 0; kk < column.Count; kk++) { cel.Merge(merge.firstRow, column[kk], Convert.ToInt32(printexcel.dtlist.Rows[i][merge.columnname].ToString().Trim()), merge.columnNumber); cel[merge.firstRow, column[kk]].PutValue(j); i = i + Convert.ToInt32(printexcel.dtlist.Rows[i][merge.columnname].ToString().Trim()) - 1; j++; } } } if (System.IO.File.Exists(printexcel.filename)) System.IO.File.Delete(printexcel.filename); //designer.Save(System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(printexcel.filename)), Aspose.Cells.SaveType.OpenInExcel, Aspose.Cells.FileFormatType.Excel2003, HttpContext.Current.Response); //HttpContext.Current.Response.End(); return designer.Workbook.SaveToStream().ToArray(); } } /// <summary> /// 填充Excel数据 /// </summary> public class PrintExcel { /// <summary> /// 数据 /// </summary> public DataTable dtinfo; /// <summary> /// 数据源 /// </summary> public DataTable dtlist; /// <summary> /// 数据 /// </summary> public Dictionary<string, string> dict; /// <summary> /// Excel地址 /// </summary> public string path; /// <summary> /// 导出Excel名称 /// </summary> public string filename; /// <summary> /// 是否可编辑 /// </summary> public bool protect = false; /// <summary> /// 合并单元格 /// </summary> public MergeExcel merge; } /// <summary> /// 合格单元格 /// </summary> public class MergeExcel { /// <summary> /// 第几行 /// </summary> public int firstRow; /// <summary> /// 合并的第几列 /// </summary> public List<int> firstColumn; /// <summary> /// 合并行数 /// </summary> public int rowNumber; /// <summary> /// 合并的列数 /// </summary> public int columnNumber; /// <summary> /// datatable合并行数的列名称字段 /// </summary> public string columnname; } public class AsposeExcell { public static DataTable ExportToDataTableAsString(string excelFilePath, bool showTitle = true) { Workbook workbook = new Workbook(); workbook.Open(excelFilePath); Cells cells = workbook.Worksheets[0].Cells; System.Data.DataTable dataTable2 = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 1, showTitle);//showTitle return dataTable2; } public static DataTable ExportToDataTableAsString(Stream stream, bool showTitle = true) { Workbook workbook = new Workbook(); workbook.Open(stream); Cells cells = workbook.Worksheets[0].Cells; System.Data.DataTable dataTable2 = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 1, showTitle);//showTitle return dataTable2; } public static Stream FileToStream(string fileName) { // 打开文件 FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); // 读取文件的 byte[] byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Close(); // 把 byte[] 转换成 Stream Stream stream = new MemoryStream(bytes); return stream; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
- 这篇文章主要介绍了c#读取excel方法,实例分析了C#读取excel文件的原理与相关技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了Python导入数值型Excel数据并生成矩阵操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-09
- 下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- C# 导出 Excel 的6种简单方法:数据表导出到 Excel,对象集合导出到 Excel,数据库导出到 Excel,微软网格控件导出到 Excel,数组导出到 Excel,CSV 导出到 Excel,你都会了吗?需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
C#实现Excel表数据导入Sql Server数据库中的方法
这篇文章主要介绍了C#实现Excel表数据导入Sql Server数据库中的方法,结合实例形式详细分析了C#读取Excel表数据及导入Sql Server数据库的具体操作步骤与相关操作技巧,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#实现将数据导出到word或者Excel中的方法,涉及C#操作word及Excel格式文件的方法,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C#使用Ado.Net更新和添加数据到Excel表格的方法,较为详细的分析了OLEDB的原理与使用技巧,可实现较为方便的操作Excel数据,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了C#使用Aspose.Cells创建和读取Excel文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了C#使用oledb导出数据到excel的方法,结合实例形式分析了C#操作oledb导出数据的相关技巧与注意事项,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C#操作excel打印的示例,帮助大家利用c#打印表格,提高办公效率,感兴趣的朋友可以了解下...2020-12-08
- 在本篇文章里小编给大家分享了关于C#删除Excel中的图片的实例代码内容,有兴趣的朋友们参考学习下。...2020-06-25
- 本文介绍Aspose.Cells基础的用法,供大家参考。...2020-06-25
- 首先 你需要添加引用Microsoft Excel 11.0 Object Library...2020-06-25
- 这篇文章主要介绍了python操作mysql、excel、pdf的示例,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-03-29
- 这篇文章主要介绍了python3 循环读取excel文件并写入json操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-14
- 今天小编就为大家分享一篇关于C#将Excel转成PDF的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...2020-06-25
- 在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式...2020-06-25
phpexcel导入xlsx文件报错xlsx is not recognised as an OLE file 怎么办
phpexcel是一款php读写excel的插件了,小编有一个这样的功能要来实现,但是在导入xlsx时发现xlsx is not recognised as an OLE file 了,但是导入xls是没有问题了,碰到这种...2016-11-25解决python pandas读取excel中多个不同sheet表格存在的问题
这篇文章主要介绍了解决python pandas读取excel中多个不同sheet表格存在的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-14