将数据导出为ex​​cel;错误文件格式错误

时间:2014-01-22 05:44:06

标签: asp.net .net asp.net-mvc-3 c#-4.0 export-to-excel

工具:MVC 3,Linq到Sql,Asp.net,C#.net 状态:MVC和Linq对sql几乎是新手

背景:创建了一个显示数据库数据的webgrid(sql server 2012)。

问题:尝试将该数据导出到Excel工作表,它已成功完成,Excel显示所有数据但在所有excel抛出错误之前? enter image description here

单击是后显示数据

enter image description here

控制器:

    namespace EmployeeAttendance_app.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Precise Technology Consultants";
            var DataContext = new EmployeeAtdDataContext();
            //var EmployeeAtd = DataContext.GetAttendance_Sp();
            IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
            return View(EmployeeAtd);

        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult ToExcel()
        {
            var DataContext = new EmployeeAtdDataContext();
            IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
            System.Web.UI.WebControls.GridView gv = new System.Web.UI.WebControls.GridView();
            gv.DataSource = DataContext.GetAtdRecord_Sp(null).ToList();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=AttendanceSheet.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            return RedirectToAction("Index");
        }
    }
}

查看:

 @using EmployeeAttendance_app.Models

<div>

@{

    var grid = new WebGrid(ViewData.Model, defaultSort: "EmplID");

 }

@grid.GetHtml()

</div>

现在下面给出的代码是一个旧代码,我试图在我的代码中使用它,那么如何?

public ActionResult ExportData()
        {
            GridView gv = new GridView();
            gv.DataSource = db.Studentrecord.ToList();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=Marklist.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

            return RedirectToAction("StudentDetails");
        }

2 个答案:

答案 0 :(得分:0)

我刚刚看到这个适用于其他人,你只需要将你的linq合并到那里{s}查询{/ 3}}

 public ActionResult ExportToExcel()
{
    var products = new System.Data.DataTable("teste");
    products.Columns.Add("col1", typeof(int));
    products.Columns.Add("col2", typeof(string));

    products.Rows.Add(1, "product 1");
    products.Rows.Add(2, "product 2");
    products.Rows.Add(3, "product 3");
    products.Rows.Add(4, "product 4");
    products.Rows.Add(5, "product 5");
    products.Rows.Add(6, "product 6");
    products.Rows.Add(7, "product 7");


    var grid = new GridView();
    grid.DataSource = products;
    grid.DataBind();

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
    Response.ContentType = "application/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("MyView");
}

答案 1 :(得分:0)

您使用的代码仅使用DataTable和.xls entsion创建HTML文本文件。 Excel功能强大,足以理解内容,但警告内容和扩展名称不正确。