您的位置:首页 > 编程语言 > C#

使用 nopi导出大量数据保存execal的方法 c#开发

2014-10-08 00:00 531 查看
摘要: nopi,大数据量,道粗exeal,easyui,c#开发

本文代码是在easyui框架下用c#开发

由于在easyui中没有插件或者控件什么的可以直接导出execal,所有要采取外部插件。我使用了nopi插件。

在使用的时候遇见的问题:

1:点击导出execal 用ajax发起,浏览器没有任何反应,也就是弹不出来对话框提示下载的。

2:几千条的数据时可以用,上万的数据的时候就需要 改写代码了。

(废话太多直接上代码)

在easyui中导出按钮是动态创建的

如下图



后台easyui代码 如下

{
text: '导出Execal编辑', iconCls: 'icon-print', handler: function () {
$("<iframe  style='display:none;' ' id='Frame2'></iframe>").prependTo('body');
$("#Frame2").attr("src","Printf.ashx?flag=2&id="+$('#yhm').combogrid('getValue')+"&action="+$('input[name="copys"]:checked').val() );
}
}

此处是动态创建一个frame 是display:none 的,引出另外一个界面

Printf.ashx代码如下

DataTable dt = SqlHelper.ExecuteTable(sql);
//common.getExcel(dt);
MemoryStream ms = common.RenderToExcel(dt);
common.RenderToBrowser(ms,HttpContext.Current, "EmptyWork");

第一行 先得到一个datatable,

第二行 得到一个 MemoryStream

第三行 导出execal文件

RenderToExcel方法如下

#region 导出excal
public static MemoryStream RenderToExcel(DataTable table)
{
MemoryStream ms = new MemoryStream();
using (table)
{
//using (IWorkbook workbook = new HSSFWorkbook())
IWorkbook workbook = new HSSFWorkbook();
{
//using (ISheet sheet = workbook.CreateSheet())
ISheet sheet = workbook.CreateSheet();
{
IRow headerRow = sheet.CreateRow(0);
// handling header.
foreach (DataColumn column in table.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
// handling value.
int rowIndex = 1;
foreach (DataRow row in table.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in table.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}
rowIndex++;
}
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
}
}
}
return ms;
}
#endregion

RenderToBrowser方法如下

#region 导出excal事件
public static void RenderToBrowser(MemoryStream ms, HttpContext context, string fileName)
{
if (context.Request.Browser.Browser == "IE")
fileName = HttpUtility.UrlEncode(fileName);
context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");
context.Response.BinaryWrite(ms.ToArray());
}
#endregion


用此方法可解决数据量大的时候导出无反应的情况

————————————————————————————————

如果有一样的那就是我抄别人的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: