我真的是C#和ASP.Net的新手,我可以说我正在学习在旅途中。我需要做的是运行存储过程,并使用响应创建一个Excel文件。到目前为止,这是我的控制器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ConfigurationSettings = System.Configuration.ConfigurationManager;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NewSalesReport.Controllers
{
public class NewSalesReportController : Controller
{
class Program
{
static void Main()
{
string inline = ConfigurationSettings.AppSettings["ConectionString"];
using (SqlConnection toConect = new SqlConnection(inline))
{
using (SqlCommand cmd = new SqlCommand("DaReport", toConect))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.DateTime));
cmd.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
adp.Fill(ds);
/*if (ds != null)
{
//Console.Write("It is not Empty");
} */
}
}
}
}
private void SetDataExcel(object result)
{
GridView grid = new GridView();
grid.DataSource = result;
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
grid.RenderControl(htmltextwrtter);
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=reportExcel.xls");
Response.ContentType = "application/ms-excel";
Response.Output.Write(strwritter.ToString());
Response.Flush();
Response.End();
strwritter.Dispose();
htmltextwrtter.Dispose();
}
public ActionResult GetAprovedForPay(DateTime? fi = null, DateTime? ff = null)
{
var result = _repo.GetAprovedForPay(fi, ff);
SetDataExcel(result);
return null;
}
}
}
}
有人在帮助我一些建议,他告诉我我需要将第一个表作为参数传递:ds.Tables[0]
,但他没有解释如何做。
另外,当我运行编译器时,显示错误,提示我需要
字段,方法或属性
Controller.Response
的引用对象。
有人可以帮我解决这两个问题吗?如我所说,我对.Net这个漏洞很陌生。
先感谢
答案 0 :(得分:1)
发生非静态字段,方法或属性的错误时需要对象引用,因为您正在从返回类型为Controller.Response
的方法中调用void
而不是ActionResult
。
首先,删除内部类Program
并将Main()
方法重命名为其他名称(必要时也删除static
)。听起来该代码包括以前在console / WinForms应用程序中使用的Main()
方法,您应该对其进行更改以避免混淆。
其次,删除Response
的使用并使用FileResult
返回文件以供下载,如下例所示:
public FileResult SetDataExcel(object obj)
{
GridView grid = new GridView();
grid.AutoGeneratedColumns = true; // automatically generate all columns from data source
grid.DataSource = result;
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
grid.RenderControl(htmltextwrtter);
string path = Server.MapPath("~/path/to/filename.xls");
using (var sw = new StreamWriter(path))
{
sw.Write(strwriter.ToString());
}
var savedFile = new FileStream(path, FileMode.Open);
return File(savedFile, "application/ms-excel");
}
最后,您可以使用重定向来调用SetDataExcel
并返回文件:
public ActionResult GetAprovedForPay(DateTime? fi = null, DateTime? ff = null)
{
var result = _repo.GetAprovedForPay(fi, ff);
return RedirectToAction("SetDataExcel", new { obj = result });
}
注意:关于GridView
的数据绑定,如果AutoGeneratedColumns
设置为true,则可以使用ds.Tables[0]
作为数据源(请参阅参考文献{{3 }}):
string inline = ConfigurationSettings.AppSettings["ConectionString"];
using (SqlConnection toConect = new SqlConnection(inline))
{
using (SqlCommand cmd = new SqlCommand("DaReport", toConect))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.DateTime));
cmd.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
adp.Fill(ds);
grid.DataSource = ds.Tables[0];
grid.DataBind();
}
}
}