C#语言MVC框架Aspose.Cells控件导出Excel表数据

本文实例为大家分享了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;
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#使用Aspose.Cells导出excel

    C# winform导出excel可以使用 Microsoft.Office.Interop.Excel.dll或者Aspose.Cells.dll以及其他方法.Microsoft.Office.Interop.Excel.dll导出速度慢,不适用于数据量大情况.Aspose.Cells.dll到处速度很快.由于Aspose.Cells.dll本身收费,所以需要加载破解证书. Aspose.Cells简介:Aspose.Cells是一款功能强大的Excel文档处理和转换控件,开发人员和客户电脑无

  • C#结束Excel进程的步骤教学

    程序中,系统自带的回收功能无法马上结束Excel进程,需要自己编写程序. 1.项目中,引用添加office COM组件 2.引用接口 3.在程序中 添加代码 [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); 4.停止Excel应用,这一步不会马上结束Excel进程,但是需要写 5.立即结

  • C#实例代码之抽奖升级版可以经表格数据导入数据库,抽奖设置,补抽

    我写代码一直是这个风格,废话不多,直接给大家贴代码,现在还是老规矩,具体代码如下所示: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threadi

  • C#将Excel转成PDF的方法

    PS:公司的业务中有个超级大的作业就是把OFFICE文档转成PDF,我猜之前没程序猿们,公司那些人应该是一个个手动转.强烈为猿们感叹,帮你们做了这么多事,还在那抱怨....无法满足你们的需求啊: 微软net平台提供了对Office文档非常好的支持:其中有com组件直接集成到了VS中.利用这些API可以快速的免去N多繁琐的工作: 以下代码是翻阅了公司的代码,一个个敲出来的:奉上代码: using System; using System.Collections.Generic; using Sys

  • c#将Excel数据导入到数据库的实现代码

    假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: 代码: 复制代码 代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.OleDb;using System.Configuratio

  • C# Winform实现导入和导出Excel文件

    本文实例为大家分享了Winform实现导入导出Excel文件的具体代码,供大家参考,具体内容如下 /// <summary> /// 导出Excel文件 /// </summary> /// /// <param name="dataSet"></param> /// <param name="dataTable">数据集</param> /// <param name="isS

  • C#实现Excel表数据导入Sql Server数据库中的方法

    本文实例讲述了C#实现Excel表数据导入Sql Server数据库中的方法.分享给大家供大家参考,具体如下: Excel表数据导入Sql Server数据库的方法很多,这里只是介绍了其中一种: 1.首先,我们要先在test数据库中新建一个my_test表,该表具有三个字段tid int类型, tname nvarchar类型, tt nvarchar类型 (注意:my_test表中的数据类型必须与Excel中相应字段的类型一致) 2. 我们用SELECT * FROM  OPENROWSET(

  • C#数据导入到EXCEL的方法

    c#做公司的应用级开发时,导出数据这个是经常要做的,公司使用的是一个叫NPOI的组件.具体是什么我就不很清楚,反正这样用就是了. 具体的dll是一个叫:NPOI.HSSF.dll,引入这个dll,这个dll提供HSSFWorkbook类 HSSFSheet类HSSFRow类: 具体实现方式直接看代码吧: public static Stream DataSetToExcel(DataSet _SourceDt,string _sheetName) { HSSFWorkbook workbook

  • C#使用Datatable导出Excel

    本文实例为大家分享了C#使用Datatable导出Excel的具体代码,供大家参考,具体内容如下 using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using Newtonsoft.Json; namespace EasyFrame.Com

  • C#数据导入/导出Excel文件及winForm导出Execl总结

    一.asp.net中导出Execl的方法: 在asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出流写给浏览器.在Response输出时,\t分隔的数据,导出execl时,等价于分列,\n等价于换行. 1.将整个html全部输出execl 此法将html中所有的内容,如按钮,表格,图片等全部输出到Execl中. 复制代码 代码如下: Response.Clear(); Response.Buffer= t

随机推荐