Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there anyway to export multiple stored procedures to single excel sheet.
I want to export multiple stored procedures to excel sheet(single) using epplus library, but i am only able to export two stored procedure in single sheet.
i will be glad if i get answers.
Thanks.


What I have tried:

 SqlCommand cmd = new SqlCommand("[usp_GetCampaignSchedule]", con1);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CampaignID", SqlDbType.Int).Value = 1102;
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            SqlCommand cmd1 = new SqlCommand("[usp_GetEmailStatitics]", con1);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.Add("@CampaignID", SqlDbType.Int).Value = 1102;
            SqlDataAdapter da1 = new SqlDataAdapter(cmd1);

           

            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
            DataSet ds = new DataSet();
            da.Fill(dt1);
            da1.Fill(dt2);
           ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);


            ExcelPackage package = new ExcelPackage();
            var worksheet = package.Workbook.Worksheets.Add("ExcelSheet");
         
            int z = 1;
            for (int x = 0; x < ds.Tables.Count; x++)
            {
                int r = z;
                int t = r + 1;
                for (int i = 1; i < ds.Tables[x].Columns.Count + 1; i++)
                {
                    worksheet.Cells[r, i].Value = ds.Tables[x].Columns
[i-1].ToString();
                }
                for (int i = 0; i < ds.Tables[x].Rows.Count; i++)
                {
                    for (int j = 0; j < ds.Tables[x].Columns.Count; j++)
                    {
                        worksheet.Cells[i + t, j + 1].Value = ds.Tables[x].Rows[i]
                        [j].ToString();
                    }
                }
                z = ds.Tables[x].Rows.Count + 4;
            }
            package.SaveAs(new FileInfo(@"C:\Users\Desktop\Work\CampaignID.xlsx"));
Posted
Updated 14-Nov-17 20:17pm
Comments
Karthik_Mahalingam 15-Nov-17 1:32am    
does the columns are same from all the sp's?
Vikrant_saini 15-Nov-17 1:47am    
No
Karthik_Mahalingam 15-Nov-17 2:04am    
then how will you combine the data from 3 sp's ?
you want to display the data one below the other ?
Vikrant_saini 15-Nov-17 2:09am    
Yes, i want to display the data table one below the other.
and we have 7 stored procedures tables.

1 solution

try

using (ExcelPackage package = new ExcelPackage())
           {
               ExcelWorksheet ws = package.Workbook.Worksheets.Add("Your Sheet Name");
               int rowNumber = 1;
               foreach (DataTable dt in ds.Tables)
               {
                   ws.Cells["A" + rowNumber].LoadFromDataTable(dt, true);
                   rowNumber += dt.Rows.Count + 2; // to create 2 empty rows
               }

               package.SaveAs(new FileInfo(@"D:\Projects\CPTemp\CPTemp\TextFile.xlsx"));
           }
 
Share this answer
 
Comments
Vikrant_saini 15-Nov-17 2:37am    
foreach (DataTable dt in ds.Tables)
In this line dt shows error as 'cannot be declared in this scope'.
As i already define it and if i want to add more table and DataAdapter.

DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataSet ds = new DataSet();
da.Fill(dt1);
da1.Fill(dt2);
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
Karthik_Mahalingam 15-Nov-17 2:41am    
  foreach (DataTable dtTemp in ds.Tables)
                {
                    ws.Cells["A" + rowNumber].LoadFromDataTable(dtTemp, true);
                    rowNumber += dtTemp.Rows.Count + 2; // to create 2 empty rows
                }
Vikrant_saini 15-Nov-17 2:47am    
I tried this but this does not give me correct output.
In my code, i am taking 6 sp's and as output only three tables i am getting in excel.
Karthik_Mahalingam 15-Nov-17 2:48am    
did you add add the 6 datatables to the dataset ?
Vikrant_saini 15-Nov-17 2:54am    
yes i did that.
Bhai i solved my problem and very thanks to you, as you gave me idea.
In my code, line that was meant for empty rows have changed to this-
z +=ds.Tables[x].Rows.Count + 4. It gives me correct output.
Thanks a lot. Will talk you on another discussion, I am a new .net learner.

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