Domanda

My code here runs a stored procedure that retrieves multiple reports for each customer. All these reports are viewed within the same sheet in excel. Can someone tell me how I can make my report viewed on multiple excel-sheets instead of 1 only??

 using (SqlCommand sqlCmd = new SqlCommand("databases.dbo.SP_GetAll_Reports",    connection))
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.AddWithValue("@customerName", SqlDbType.NVarChar).Value = custName;
            using (SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
            {
                Session["TaskTable"] = dt;
                sqlDa.Fill(dt);
                if (dt.Tables.Count > 0)
                {
                     StringWriter sw = new StringWriter();
                    foreach(DataTable table in dt.Tables)
                    {
                         HtmlTextWriter hw = new HtmlTextWriter(sw);
                        DataGrid dg = new DataGrid();
                        dg.DataSource = table;
                        dg.DataBind();
                        dg.RenderControl(hw);
                    }
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachchment;       filename=Report_Accounting.xls");
                    Response.Charset = "";
                    Response.ContentType = "application/vnd.xls";
                    Response.Write(sw.ToString());
                    Response.End();
                }
È stato utile?

Soluzione

First install EPPlus using Nuget Package manager

  1. right click you project
  2. select Nuget Package Manager
  3. Search for EPPlus
  4. Click install

then change your code to this

using OfficeOpenXml;


 using (SqlCommand sqlCmd = new SqlCommand("databases.dbo.SP_GetAll_Reports",    connection))
 {
     sqlCmd.CommandType = CommandType.StoredProcedure;
     sqlCmd.Parameters.AddWithValue("@customerName", SqlDbType.NVarChar).Value = custName;
     using (SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
     {
         Session["TaskTable"] = dt;
         sqlDa.Fill(dt);
         if (dt.Tables.Count > 0)
         {
             MemoryStream ms = new MemoryStream();
             int i=1;
             using (ExcelPackage package = new ExcelPackage(ms))
             {
                 foreach(DataTable table in dt.Tables)
                 {
                     ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet"+i++);
                     worksheet.Cell["A1"].LoadFromDataTable(table, true); 
                 }
                 Response.Clear(); 
                 package.SaveAs(Response.OutputStream);
                 Response.AddHeader("content-disposition", "attachchment;       filename=Report_Accounting.xls");
                 Response.Charset = "";
                 Response.ContentType = "application/vnd.xls";
                 Response.End();
             }
         }
     }
 }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top