控件bin文件下载地址:https://download.csdn.net/download/u012949335/10610726
@{
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 > )
{
for (int i = ; i < dtinfo.Rows.Count; i++)
{
for (int k = ; k < dtinfo.Columns.Count; k++)
{
string column = dtinfo.Columns[k].ColumnName;
workbook.Worksheets[].Replace("$" + column.ToLower() + "$", dtinfo.Rows[i][column].ToString());
}
}
}
if (dict != null)
{
foreach (string j in dict.Keys)
{
workbook.Worksheets[].Replace("$" + j.ToLower() + "$", dict[j].ToString());
}
}
if (protect)
workbook.Worksheets[].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 > )
{
for (int i = ; i < printexcel.dtinfo.Rows.Count; i++)
{
for (int k = ; k < printexcel.dtinfo.Columns.Count; k++)
{
string column = printexcel.dtinfo.Columns[k].ColumnName;
workbook.Worksheets[].Replace("$" + column.ToLower() + "$", printexcel.dtinfo.Rows[i][column].ToString());
}
}
}
if (printexcel.dict != null)
{
foreach (string j in printexcel.dict.Keys)
{
workbook.Worksheets[].Replace("$" + j.ToLower() + "$", printexcel.dict[j].ToString());
}
}
if (printexcel.protect)
workbook.Worksheets[].Protect(ProtectionType.All, "xakj..123", "");
designer.SetDataSource(printexcel.dtlist);//设置数据源
designer.Process();//自动赋值 //合格单元格
if (printexcel.dtlist != null && printexcel.dtlist.Rows.Count > )
{
Aspose.Cells.Worksheet sheet = designer.Workbook.Worksheets[];
Cells cel = sheet.Cells;
List<int> column = printexcel.merge.firstColumn;
int j = ;
for (int i = ; i < printexcel.dtlist.Rows.Count; i++)
{
for (int kk = ; 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()) - ;
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[].Cells;
System.Data.DataTable dataTable2 = cells.ExportDataTableAsString(, , cells.MaxDataRow + , cells.MaxColumn + , showTitle);//showTitle
return dataTable2;
}
public static DataTable ExportToDataTableAsString(Stream stream, bool showTitle = true)
{
Workbook workbook = new Workbook();
workbook.Open(stream);
Cells cells = workbook.Worksheets[].Cells;
System.Data.DataTable dataTable2 = cells.ExportDataTableAsString(, , cells.MaxDataRow + , cells.MaxColumn + , 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, , bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
}
}

基于C#语言MVC框架Aspose.Cells控件导出Excel表数据的更多相关文章

  1. 基于C#语言MVC框架NPOI控件导出Excel表数据

    控件bin文件下载地址:https://download.csdn.net/download/u012949335/10610726@{ ViewBag.Title = "dcxx" ...

  2. 用Aspose.Cells控件读取Excel

    Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...

  3. C# Aspose.Cells控件读取Excel

    Workbook workbook = new Workbook(); workbook.Open("C:\\test.xlsx"); Cells cells = workbook ...

  4. 使用Aspose.Cell控件实现Excel高难度报表的生成(三)

    在之前几篇文章中,介绍了关于Apsose.cell这个强大的Excel操作控件的使用,相关文章如下: 使用Aspose.Cell控件实现Excel高难度报表的生成(一) 使用Aspose.Cell控件 ...

  5. 使用Aspose.Cell控件实现Excel高难度报表的生成(二)

    继续在上篇<使用Aspose.Cell控件实现Excel高难度报表的生成(一)>随笔基础上,研究探讨基于模板的Aspose.cell报表实现,其中提到了下面两种报表的界面,如下所示: 或者 ...

  6. 利用Aspose.Cell控件导入Excel非强类型的数据

    导入Excel的操作是非常常见的操作,可以使用Aspose.Cell.APOI.MyXls.OLEDB.Excel VBA等操作Excel文件,从而实现数据的导入,在导入数据的时候,如果是强类型的数据 ...

  7. 使用Aspose.Cell控件实现Excel高难度报表的生成

    1.使用Aspose.Cell控件实现Excel高难度报表的生成(一) http://www.cnblogs.com/wuhuacong/archive/2011/02/23/1962147.html ...

  8. aspose.cells根据模板导出excel

    又隔十多天没写博客了,最近都在忙项目的事情,公司人事变动也比较大,手头上就又多了一个项目.最近做用aspose.cells根据模板导出excel报价单的功能,顺便把相关的核心记下来,先上模板和导出的效 ...

  9. (转)使用Aspose.Cell控件实现Excel高难度报表的生成(一)

    本文章主要介绍报表的生成,基于Aspose.Cell控件的报表生成.谈到报表,估计大家都有所领悟以及个人的理解,总的来说,一般的报表生成,基本上是基于以下几种方式:一种是基于微软Excel内置的引擎来 ...

随机推荐

  1. Django的模板语言介绍

    模板语言: 1.我们先看下在命令行中渲染模板 先导入模块对象 2.渲染一个变量 <p>当前时间:{{ time }}</p> return render(request,&qu ...

  2. discuz目录结构和插件创建

    discuz目录结构 api 外部接口功能实现 archiver 静态文档,静态化所用 config 配置 data 生成的数据 install 安装目录 source 源代码核心目录 |--modu ...

  3. vsftp上传文件出现553 Could not create file

    没有权限创建文件或是目录,原因是selinux引起的登陆问题. 通过如下命令查看状态: > sestatus -b|grep ftp 设置allow_ftpd_full_access为on. 在 ...

  4. jq给动态生成的标签绑定事件的几种方法

    经常遇到给动态生成的标签绑定事件不好用,自己简单测试总结了下,结论如下了: body> <!-- 下面是用纯动态方式生成标签 --> <div id="d2" ...

  5. Devexpress RichEditControl 导入word文件后字体变为方正姚体的解决方案

    最近在做一个排版软件,用过RichEditControl 导入外部Word文件的时候,发现导的文件后字体会变成“方正姚体”,官方这个BUG至少在V16.1版本尚未解决,翻阅了大量资料,发现 DevEx ...

  6. axios 设置拦截器 全局设置带默认参数(发送 token 等)

    应用场景: 1,每个请求都带上的参数,比如token,时间戳等. 2,对返回的状态进行判断,比如token是否过期 代码如下: [javascript] view plain copy axios.i ...

  7. efcore 级联删除

    https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete#required-relationships Optional relat ...

  8. pyspider示例代码四:搜索引擎爬取

    搜索引擎爬取 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: __git_lab_fix fr ...

  9. Android属性动画之ValueAnimator的介绍

    之前两篇博客,介绍的是ObjectAnimator作用与某一个控件的某一个属性.但我们的ValueAnimator它本身并不会作用与任何一个属性,它本身也不会提供任何一种动画.它简单的来说,就是一个数 ...

  10. shell 脚本 抽取指定数量的随机学生

    #!/bin/bash # #!/bin/bash # read -p '输入数' c #指定抽取的学生人数 jw=('王浩' '谢云生' '黄科杨' '何星宇' '张宸兵' '邓培林' '刘桃' ' ...