gpt4 book ai didi

c#-4.0 - 我想使用 WebGrid 将自由格式的 native 查询输出发送到 excel

转载 作者:行者123 更新时间:2023-12-04 08:44:49 24 4
gpt4 key购买 nike

首先,我需要解释我的问题,我有一个 native 查询应用程序,其中有人输入“Select .... this and that”,输出当前显示在分页的网格中,我被要求添加一个按钮将数据直接从无类型数据流导出到 excel。我发现的当前代码使用网格,但由于某种原因仍然不提示我下载 .xls 文件。

     [Authorize(Roles = "Portal administrator")]
public void ExportExcel(NativeQueryVM model, int? page, string sort)
{
List<Dictionary<string, object>> result = null;
String vartimedate = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");
try
{
var user = Membership.GetUser();
var grid = new System.Web.UI.WebControls.GridView();
if (page == null && sort == null)
{
if (model.QueryText.ToUpper().StartsWith("SELECT"))
{
UserQueryInput queryinput = new UserQueryInput();
queryinput.DatabaseId = model.selectedDatabase;
queryinput.QueryText = model.QueryText;
result = lookupProvider.GetSetNativeQuery((Guid)user.ProviderUserKey, user.UserName, queryinput, "Set");
model.QueryResultData = result;
ViewBag.SubmitType = "Select";
CreateDynamicResult(model.QueryResultData);
if (model == null || model.QueryResultData.Count == 0)
{
ViewBag.ResultMessage = "No Results Found";
}
else
{

WebGrid wd = new WebGrid(source: ViewBag.DynamicResult, canPage: false, canSort: false );

string griddata = wd.GetHtml().ToString();
string attachment = "attachment; filename=NativeQuery" + vartimedate + ".xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.Write(griddata);
Response.End();
}
}

}
else
{ //This part should come when page or sort is not null for Select. Should not be executed other than SELECT
result = lookupProvider.GetSetNativeQuery((Guid)user.ProviderUserKey, user.UserName, null, "Get");
model.QueryResultData = result;
ViewBag.SubmitType = "Select";
CreateDynamicResult(model.QueryResultData);
if (model == null || model.QueryResultData.Count == 0)
{
ViewBag.ResultMessage = "No Results Found";
}
else
{

}
}

}
catch (Exception ex)
{
logger.Log(new LogMessage()
.ForMethod("SubmitQuery")
.WithContext(Environment.MachineName)
.WithException(ex)
.WithDescription("An error occurred while submiting query to get result"));
}
}

最佳答案

大家好,回答了这个问题,我发现答案和问题都存在于我在问题中输入的代码之外。 Jquery 正在拦截我的文件流的输出,另一方面,如果按钮是使用 runat=server 创建的,则模型超出范围,解决方案的主要部分涉及将模型保存到 session 并将其拉回。

谢谢大家的帮助...

关于c#-4.0 - 我想使用 WebGrid 将自由格式的 native 查询输出发送到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com