使用C#下载excel文件 [英] Download excel file using C#
本文介绍了使用C#下载excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从数据库中获取数据,在excel上写入并下载文件。我使用下面给出的代码,当数据显示在警报时无法下载文件。
我尝试过:
控制器代码如下:
[HttpPost]
public ActionResult ShipExportToExcel( string [] ShipID)
{
var products = new System.Data.DataTable( Person 跨度>);
products.Columns.Add( 名称, typeof ( string ));
products.Columns.Add( Address, typeof ( string ));
products.Columns.Add( Mobile Number, typeof ( string ));
foreach ( var i in ShipID)
{
int ID = Convert.ToInt32(i);
Person obj_Person = new Person();
obj_Person = new BL_Person()。GetPersonByID(ID);
if (obj_Person!= null )
{
products。 Rows.Add(obj_Person .Name,obj_Person.ShipAddress,obj_Person .Cell);
}
}
var grid = new GridView();
grid.DataSource = products;
grid.DataBind();
Response.ClearContent();
Response.Buffer = true ;
Response.AddHeader( content-disposition, attachment; filename = ExportByExcel.xls);
// Response.ContentType =application / ms-excel;
Response.ContentType = application / vnd.ms-excel;
Response.Charset = ;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return 查看();
}
查看代码.........................
$(' #btnSave')。click(function(){
var rowCount = $(' 表> tbody> tr' )。长度;
如果(rowCount > < span class =code-digit> 0 ){
var data = {
ShipID:shipmentItems
}
$ .ajax({
url: @ Url.Action( ShipExportToExcel , ExportToExcel ,new {area = Pers on }),
type: POST,
data:JSON.stringify(data),
contentType: application / json; charset = utf-8,
成功:函数(d){
msgSuccess(d);
},
错误:函数(请求,错误){
msgAlerts(错误);
}
});
} 其他 {
msgAlerts( 请先输入记录);
}
解决方案
(' #btnSave')。click(function(){
var rowCount =
(' 表> tbody> tr')。length;
如果(rowCount > 0 ){
var data = {
ShipID:shipmentItems
}
.ajax({
url: @ Url.Action( ShipExportToExcel , ExportToExcel ,new {area = Person }),
type: POST,
data:JSON.stringify(data),
contentType: application / json; charset = utf-8,
成功:函数(d){
msgSuccess(d);
},
错误:函数(请求,错误){
msgAlerts(错误);
}
});
} 其他 {
msgAlerts( 请先输入记录);
}
I want to get data from database, write on excel and download file. I use the code given below and file cannot be download while data show in alert.
What I have tried:
Controller Code is given below
[HttpPost]
public ActionResult ShipExportToExcel(string[] ShipID)
{
var products = new System.Data.DataTable("Person");
products.Columns.Add("Name ", typeof(string));
products.Columns.Add("Address", typeof(string));
products.Columns.Add("Mobile Number", typeof(string));
foreach (var i in ShipID)
{
int ID = Convert.ToInt32(i);
Person obj_Person = new Person();
obj_Person = new BL_Person().GetPersonByID(ID );
if (obj_Person!= null)
{
products.Rows.Add(obj_Person .Name, obj_Person.ShipAddress, obj_Person .Cell);
}
}
var grid = new GridView();
grid.DataSource = products;
grid.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=ExportByExcel.xls");
//Response.ContentType = "application/ms-excel";
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return View();
}
view code.........................
$('#btnSave').click(function () {
var rowCount = $('Table >tbody >tr').length;
if (rowCount > 0) {
var data = {
ShipID: shipmentItems
}
$.ajax({
url: "@Url.Action("ShipExportToExcel", "ExportToExcel", new { area = "Person" })",
type: "POST",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
success: function (d) {
msgSuccess(d);
},
error: function (request, error) {
msgAlerts(error);
}
});
} else {
msgAlerts("Please Enter the Record First");
}
解决方案
('#btnSave').click(function () { var rowCount =
('Table >tbody >tr').length; if (rowCount > 0) { var data = { ShipID: shipmentItems }
.ajax({ url: "@Url.Action("ShipExportToExcel", "ExportToExcel", new { area = "Person" })", type: "POST", data: JSON.stringify(data), contentType: "application/json; charset=utf-8", success: function (d) { msgSuccess(d); }, error: function (request, error) { msgAlerts(error); } }); } else { msgAlerts("Please Enter the Record First"); }
这篇关于使用C#下载excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文