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;
}
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:用C#做網絡爬蟲的步驟教學
欄 目:C#教程
本文標題:C#語言MVC框架Aspose.Cells控件導出Excel表數據
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4982.html
您可能感興趣的文章
- 01-10C#編程自學之開篇介紹
- 01-10C#語言中的修飾符匯總
- 01-10ASP.NET MVC命名空間時引起錯誤的解決方法
- 01-10輕松學習C#的foreach迭代語句
- 01-10輕松學習C#的抽象類
- 01-10C#開源的AOP框架--KingAOP基礎
- 01-10結合.net框架在C#派生類中觸發(fā)基類事件及實現接口事件
- 01-10C# DateTime.ToString根據不同語言生成相應的時間格式
- 01-10詳解C#借助.NET框架中的XmlTextReader類讀取XML的方法
- 01-10適用于WebForm Mvc的Pager分頁組件C#實現


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現txt定位指定行完整實例
- 01-10WinForm實現仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數量限制代碼修改


