Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to merge three tables in sql and i export that records to exce through asp.net mvc 4.0.
how to i do that.how to write stored procedure for that
Posted

1.use join to the three tables to get data as a single table.

2. fetch that into controller using linq or Datatable


3. push to client using this code
C#
//Write it back to the client
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.BinaryWrite(pck.GetAsByteArray());
    Response.End();
 
Share this answer
 
Hi...
See this one,may its help ful to u.
C#
datatable dt;
string filename = Server.MapPath("~/Download.xlsx");
StreamWriter sw = new StreamWriter(filename, false);

//DataTable dt = new DataTable();

int iColCount = dt.Columns.Count;
for (int i = 1; i < iColCount; i++)
{
    sw.Write(dt.Columns[i]);
    if (i < iColCount - 1)
    {
        sw.Write(",");
    }
}
sw.Write(sw.NewLine);

foreach (DataRow dr in dt.Rows)
{
    for (int i = 0; i < iColCount; i++)
    {
        if (!Convert.IsDBNull(dr[i]))
        {
            sw.Write(dr[i].ToString());
        }
        if (i < iColCount - 1)
        {
            sw.Write(",");
        }
    }

    sw.Write(sw.NewLine);
}
sw.Close();

//Response.Clear();
Response.ClearContent();
Response.ContentType = "application/xlsx";
Response.AddHeader("Content-Disposition", "attachment; filename=Download.xlsx");

Response.WriteFile(filename);
Response.Flush();
//Response.Write();
Response.End();

Thank u.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900