从ASP.NET网站发送查询结果到Excel [英] Send query results to Excel from ASP.NET website

查看:99
本文介绍了从ASP.NET网站发送查询结果到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们让用户创建在我们的网站即席查询。我们希望让用户选择他们的标准,然后点击提交,并有结果会自动传输到Excel中。我的应用已经填充一个DataTable,然后使用数据表创建一个制表符分隔字符串。问题是如果这能够脱颖而出。

We let users create ad-hoc queries in our website. We would like to have the user select their criteria, then click submit and have the results streamed automatically to Excel. I have the application populating a DataTable, then using the datatable to create a tab delimited string. The problem is getting that to excel.

什么是流数据到Excel中的最佳方式? preferrably,我们就不必让用户点击提交按钮后,关闭一个空的窗口。

What is the best way to stream data to Excel? Preferrably, we wouldn't have to make users close an empty window after clicking the submit button.

推荐答案

改变页面的文件类型脱颖而出,只有流建立一个表网页所需的HTML。从code <一个href=\"http://www.eggheadcafe.com/tutorials/aspnet/6e1ae1a8-8285-4b2a-a89b-fafc7668a782/aspnet-download-as-wor.aspx\">here

Change the page's file type to excel, and only stream the HTML necessary to build a table to the page. code from here

//for demo purpose, lets create a small datatable & populate it with dummy data
System.Data.DataTable workTable = new System.Data.DataTable();

//The tablename specified here will be set as the worksheet name of the generated Excel file. 
workTable.TableName = "Customers";
workTable.Columns.Add("Id");
workTable.Columns.Add("Name");
System.Data.DataRow workRow;

for (int i = 0; i <= 9; i++)
{
workRow = workTable.NewRow();
workRow[0] = i;
workRow[1] = "CustName" + i.ToString();
workTable.Rows.Add(workRow);
}

//...and lets put DataTable2ExcelString to work
string strBody = DataTable2ExcelString(workTable);

Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
Response.AppendHeader("Content-disposition", "attachment; filename=my.xls");
Response.Write(strBody);

这篇关于从ASP.NET网站发送查询结果到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆