Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi have parent and child grid , i want to export both the grid data into single excelsheet.

What I have tried:

aspx code

ASP.NET
<asp:gridview id="gvSDPLogDetail" width="100%" runat="server" allowpaging="true" font-size="10px" datakeynames="SDPCompliance" emptydatatext="No Record Found" pagesize="20" 
="" cssclass="StandardCompactGridView" pagerstyle-cssclass="StandardGridViewPagination-left" pagersettings-position="TopAndBottom" headerstyle-cssclass="StandardGridViewHead" rowstyle-cssclass="StandardGridViewRow" footerstyle-cssclass="StandardGridViewFooterLight" autogeneratecolumns="False" pagerstyle-horizontalalign="Left" onrowdatabound="gvSDPLogDetail_RowDataBound">
                                              <headerstyle horizontalalign="Center">
                                              <columns>
                                                  <asp:boundfield datafield="requestID" headertext="RequestID" sortexpression="">
                                                  <asp:boundfield datafield="OrderNumber" headertext="Order#" sortexpression="">
                                                  <asp:boundfield datafield="Whsecode" headertext="WareHouse" sortexpression="">
                                                  <asp:boundfield datafield="ItemID" headertext="ItemID" sortexpression="">
                                                  <asp:boundfield datafield="LocationPicked" headertext="LocationPicked" sortexpression="">
                                                  <asp:boundfield datafield="LotNo" headertext="Lot#" sortexpression="">
                                                  <%--<asp:templatefield>
                                                      <headertemplate>
                                                          
                                                              <table class="table table-bordered table-sm"><thead style="background-color: rgba(139, 111, 78, 1); color: rgba(255, 255, 255, 1)">                                                                  <tr><th class="width-150">Location</th>                                                                  <th class="width-150">Lot#</th>                                                                  <th class="width-150">Qty</th>                                                              </tr></thead>                                                          </table>
                                                      
                                                  --%>

                                                  <asp:templatefield>
                                                     <headertemplate>
                                                         
                                                             <table class="table table-bordered table-sm"><tbody><tr style="background-color: rgba(139, 111, 78, 1); color: rgba(255, 255, 255, 1)">                                                                 <td style="width: 60px">                                                                     Location
                                                                 </td>                                                                 <td style="width: 60px">Lot#</td>                                                                 <td style="width: 60px">Qty</td>                                                             </tr>                                                         </tbody></table>
                                                     
                                                      <itemtemplate>
                                                          <asp:gridview id="gvItem" cssclass="table table-bordered table-sm" runat="server" autogeneratecolumns="false" showheader="false">
                                                              <columns>
                                                                  <asp:boundfield itemstyle-cssclass="w-20" datafield="location">
                                                                  <asp:boundfield itemstyle-cssclass="w-20" datafield="lotno">
                                                                  <asp:boundfield itemstyle-cssclass="w-20" datafield="qty">
                                                              
                                                           
                                                      
                                                  
                                                  
                                                  <asp:boundfield datafield="QtyPacked" headertext="QtyPacked" sortexpression="">
                                                  <asp:boundfield datafield="PackedOn" headertext="PackedOn" sortexpression="">
                                                  <asp:boundfield datafield="Packedby" headertext="PackedBy" sortexpression="">
                                                  <asp:boundfield datafield="Status" headertext="Status" sortexpression="">
                                                  <asp:boundfield datafield="SDPCompliance" headertext="SDPCompliance" sortexpression="">





.cs code for excel download

C#
void GetDataInExcel(DataSet ds)
        {
            var workbook = new ClosedXML.Excel.XLWorkbook();

            //foreach (DataTable dt in ds.Tables)
            {
                var worksheet = workbook.Worksheets.Add("DetailsData");
                worksheet.Cell(1, 1).InsertTable(ds.Tables[0]);
                worksheet.Columns().AdjustToContents();
               worksheet.Columns("13").Hide();
               // worksheet.Columns("13").Delete();
                
            }

            //Export the Excel file.
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename=Details.xlsx");

            try
            {
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    workbook.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }
            catch
            {

            }

        }
Posted
Updated 24-Aug-22 20:28pm
v2
Comments
OriginalGriff 25-Aug-22 2:28am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 15747062 25-Aug-22 2:33am    
HI,

I have parentgrid (gvSDPLogDetail) and child grid(gvitem),
i have stored parent grid into a viewstate["detaildata"] and converting it to dataset
sending this dataset to export to download excel.
after donloading i am unable to see child grid , only parent grid is being exported to excel.
can you take a look and help me here.
CHill60 25-Aug-22 11:45am    
Looks like you need to convert the child grid to a dataset and then call GetDataInExcel with that dataset and possibly the name of the sheet you want to create. Or pass it in the same dataset and uncomment that line with the for each - having done something about the worksheet naming.

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