locked
How to export the Inside panel Control In excel RRS feed

  • Question

  • User1827380138 posted

    This is my code For Generating Barcode images 

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="page-wrapper" >
             <div class="col-md-12">
      <!-- Form Elements -->
             <div class="panel panel-info">
             <div class="panel-heading">
                   <h3 style="text-shadow: 2px 2px #79aed8;">Barcode List </h3>  
              </div>
              <div class="panel-body">
              <div id="Barcode">
               <h1 align="center" 
                      style="text-decoration: underline; font-size: x-large; font-weight: bold">Barcode</h1>
              
                <div id="excel1" runat="server">
                  <asp:Panel ID="Panel1" runat="server">
                      <asp:Label ID="lblPname" runat="server" Text="" Font-Bold="True"></asp:Label> 
                      <br />
                    <asp:PlaceHolder ID="phBarcode" runat="server" Visible="true"></asp:PlaceHolder>
                      </asp:Panel>
                 </div>
                  <div class="table-responsive";>
                     <table class="table">
                        <tbody>
                        <tr>
                        <td>
                            <asp:Button ID="btnPPdf" runat="server" Text="Print" CssClass="btn-info" 
                                Width="100px" OnClientClick = "return PrintPanel();" />
                        
                             <asp:Button ID="btnExcel" runat="server" Text="Excel" CssClass="btn-success" 
                                Width="100px" onclick="btnExcel_Click"  />
                        
                             <asp:Button ID="btnBack" runat="server" Text="Back" CssClass="btn-danger" 
                                Height="35px" Width="100px" />
                        </td>
                        </tr>
                        </tbody> 
                     </table>
                 </div>
              </div>
    
               </div>
             </div>
          </div>
       </div>
    </asp:Content>
     string barCode;
            protected void Page_Load(object sender, EventArgs e)
            {
                //lblPname.Text = Request.QueryString["pname"];
                //barCode = Request.QueryString["subhead"];
                lblPname.Text = "Raj";
                barCode = "G-212012123";
                Barcode();
    
            }
            private void Barcode()
            {
               System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
               imgBarCode.Height = 70;
               imgBarCode.Width = 250;
            using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
            {
                using (Graphics graphics = Graphics.FromImage(bitMap))
                {
                    Font oFont = new Font("IDAutomationHC39M", 16);
                    PointF point = new PointF(2f, 2f);
                    SolidBrush blackBrush = new SolidBrush(Color.Black);
                    SolidBrush whiteBrush = new SolidBrush(Color.White);
                    graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                    graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();
    
                    Convert.ToBase64String(byteImage);
                    imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                    }
                    phBarcode.Controls.Add(imgBarCode);
                }
            }
    
            protected void btnExcel_Click(object sender, EventArgs e)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
                Response.Charset = "";
                Response.ContentType = "application/vnd.xls";
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                Panel1.RenderControl(htmlWrite);
                Response.Write(stringWrite.ToString());
                Response.End();
    
        }

    When i export In excel That Out was only source 

    This is the Excel output it was not display image and label

    <div id="ContentPlaceHolder1_Panel1">
                      <span id="ContentPlaceHolder1_lblPname" style="font-weight:bold;">Raj</span> 
                      <br />
                    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbgAAABQCAYAAABrqmDoAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAbOSURBVHhe7doBkqJIEAVQj8eBPI538SrchFFQGxXHbeqznfa83ZiYCHtMsz5UPoHeDf6TgAQkIAEJ/MIEdr9wTZYkAQlIQAISGADnJJCABCQggV+ZwAvgjsN+txv2x/ma++HQXV477ofd6efXP92hH4al1/rD0O32w1Tmq2Z/6IZddxj60/+3mrePml77qj+9/7h/fm1e86vThz5nn/Pfai70OlvrUibj+mdr/Vuvtx7erP87Nc89jZlG+nx1rGbn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J0EAAc4wAEOcHVmsk6CCQAOcIADHOCCQ1WpOgkADnCAAxzg6sxknQQTABzgAAc4wAWHqlJ1EgAc4AAHOMDVmck6CSYAOMABDnCACw5Vpeok8AK4Og3qRAISkIAEJLAmAcCtSc17JCABCUigfAKAq3iIjvvxtmF36Ct2N/SH7nJbc/f1d3cYYt0WX//dQfmkXkueTZqSwHYJtAE3bu79cAz19zw4c7VDLY5ljvvZYL8+w9snUjgO+9szwdNnNNbcpM/xmHfDvb2Xvhv7PSVbf/23E6l2r4vHPnqujhshuv+Te1QtCZwTaAJuAimD0LQh7wfn+FryyiBxzJc2dX84PaRvvOK61JjW2w+HrhG4jfp8dUym49dwLnzI+sdT6JN6vZ7z155bjtHD/knu/8TWVEMCjwl8G7jbVdbp2/rtBL/b8CtCvtzmab4AWPHRmbekrmBu06gduMWFtfe5GXB3/QaA32j9z2Xr9zrfs63n+yb7v7Up75fAiwS+D1x/etJy+zY4u1V31un0szXPYbb4JvjXWzRPt9hazo/rraqGq5enj99iaCb6vPS1cFU9HcPHW5drc626/qX11O71cR+0PtftN9j/a88S75PAuwS+DdxU8LKpTwOtO99Ka7w9dzccH/Gsdovykuj8eWHr0NjyqiDd56ddwaXX/0nH6rnXy5ec5j2V3f/vhpSfS2BtAquAe/q23vqbZBfUHm9RlnwGd0r6+q14u1uqmauCLfp8dbWdPVZ1178VcFscq8XrzcCVdnz/r51e3ieBNwmsAm6LVJd+SSE7NENdJ36h5G0rgQG/VZ+Lz0vbn+3dR1J4/VvcTt7qWD32er07st03s7dntn8ggf8zgTLAnRe99Nwsf/uvMd6l54/XX79uvvVz7S034M+3j5/+NPa5+HyzseZWwG2x/k/o9dUzaLY17n9v/6gESgH3UclpVgISkIAESicAuNKHR3MSkIAEJLA2gT9WgNyAEsKTSwAAAABJRU5ErkJggg==" style="height:70px;width:250px;" />
                      
    </div>
    Tuesday, December 31, 2019 2:03 PM

All replies

  • User-1038772411 posted

    Hello Dhoni,
    As we checked your issue, you got the plain html in to the excel file because of you have not use tabular format in your <asp:Panel ID="Panel1" runat="server">.

    So that you can use below html code for the panel1 & when export as excel then you can get output as you want.

    html code for the <asp:Panel ID="Panel1" runat="server">

    <asp:Panel ID="Panel1" runat="server">
        <table>
            <tr>
                <td><asp:Label ID="lblPname" runat="server" Text="" Font-Bold="True"></asp:Label> </td>                        
            </tr>
            <tr>
                <td> <asp:PlaceHolder ID="phBarcode" runat="server" Visible="true"></asp:PlaceHolder></td>
            </tr>
        </table>
    </asp:Panel>

    If you still facing any issue please let us know.

    Thanks.

    Wednesday, January 1, 2020 6:14 AM
  • User288213138 posted

    Hi Dhoni,

    As AddWeb Said, you should put the panel in table. you can also try it in code behind.

     protected void btnExcel_Click(object sender, EventArgs e)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
                Response.Charset = "";
                Response.ContentType = "application/vnd.xls";
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                Table table = new Table();
                TableRow row = new TableRow();
                row.Cells.Add(new TableCell());
                row.Cells[0].Controls.Add(Panel1);
                table.Rows.Add(row);
                table.RenderControl(htmlWrite);
                Response.Write(stringWrite.ToString());
                Response.End();
    
            }

    Best regards,

    Sam

    Wednesday, January 1, 2020 7:52 AM
  • User1827380138 posted

    But Barcode image is not display

    How to display Embedded base64 images

    Excel output

    Wednesday, January 1, 2020 1:04 PM
  • User-1038772411 posted

    Hello Dhoni,

    The working code for your requirement is below.

    .aspx page

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="page-wrapper" >
             <div class="col-md-12">
      <!-- Form Elements -->
             <div class="panel panel-info">
             <div class="panel-heading">
                   <h3 style="text-shadow: 2px 2px #79aed8;">Barcode List </h3>  
              </div>
              <div class="panel-body">
              <div id="Barcode">
               <h1 align="center" 
                      style="text-decoration: underline; font-size: x-large; font-weight: bold">Barcode</h1>
              
                <div id="excel1" runat="server">
                    
                    <asp:Panel ID="Panel1" runat="server">
                        <table>
                            <tr>
                                <td><asp:Label ID="lblPname" runat="server" Text="" Font-Bold="True"></asp:Label> </td>                        
                            </tr>
                            <%--<tr>
                                <td> <asp:PlaceHolder ID="phBarcode" runat="server" Visible="true"></asp:PlaceHolder></td>
                            </tr> Comment code because of if not commented then show image not available symbol into the excel. --%>                         
                        </table>
                    </asp:Panel>
                 </div>
                  <div class="table-responsive";>
                     <table class="table">
                        <tbody>
                        <tr>
                        <td>
                            <asp:Button ID="btnPPdf" runat="server" Text="Print" CssClass="btn-info" 
                                Width="100px" OnClientClick = "return PrintPanel();" />
                        
                             <asp:Button ID="btnExcel" runat="server" Text="Excel" CssClass="btn-success" 
                                Width="100px" onclick="btnExcel_Click"  />
                        
                             <asp:Button ID="btnBack" runat="server" Text="Back" CssClass="btn-danger" 
                                Height="35px" Width="100px" />
                        </td>
                        </tr>
                        </tbody> 
                     </table>
                 </div>
              </div>
    
               </div>
             </div>
          </div>
       </div>
    </asp:Content>
    

    .aspx.cs page code

            string barCode = "";
            string serverMapPath = "";
            protected void Page_Load(object sender, EventArgs e)
            {
                lblPname.Text = "Raj";
                barCode = "G-212012123";
                Barcode();
            }
    
    
    private void Barcode()
            {
                //System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
                //imgBarCode.Height = 70;
                //imgBarCode.Width = 250;
                using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
                {
                    using (Graphics graphics = Graphics.FromImage(bitMap))
                    {
                        Font oFont = new Font("IDAutomationHC39M", 16);
                        PointF point = new PointF(2f, 2f);
                        SolidBrush blackBrush = new SolidBrush(Color.Black);
                        SolidBrush whiteBrush = new SolidBrush(Color.White);
                        graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                        graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
                    }
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        byte[] byteImage = ms.ToArray();
                        if (!Directory.Exists(Server.MapPath("~/Content/images/")))
                        {                        
                            Directory.CreateDirectory(Server.MapPath("~/Content/images/"));
                        }
                        serverMapPath = Server.MapPath("~/Content/images/" + DateTime.Now.ToString("ddMMyyyy_hhmmss") +".jpg");
                        
    
                        using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(byteImage)))
                        {
                            image.Save(serverMapPath, ImageFormat.Jpeg);  // Or Png
                        }
                        //imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                    }
                    //phBarcode.Controls.Add(imgBarCode);
                }
            }
    
    
    
    
    protected void btnExcel_Click(object sender, EventArgs e)
            {            
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-excel";
                using (StringWriter sw = new StringWriter())
                {
                    HtmlTextWriter hw = new HtmlTextWriter(sw);
                    Panel1.RenderControl(hw);
                    Response.Output.Write(sw.ToString());
                    string tag = "<img src="+ serverMapPath + " />";
                    Response.Write(tag);
                    Response.Flush();
                    Response.End();
                }
             }
    

    If you still facing issue please let us know.

    Thanks.

    Thursday, January 2, 2020 5:46 AM
  • User1827380138 posted

    Same Problem Is here , 

    I export in excel image not display it was Embedded base64

    Please help me to how to display Embedded base64 image in excel

    Thursday, January 2, 2020 6:48 AM
  • User-1038772411 posted

    Hello Dhoni,

    The code attached above is working code. You can use in your new application and test.

    You can also check the image of exported excel file on url : https://imgur.com/9OQki0G

    If still you have problem or not get the image in excel file then let us know.

    As well as share the code that you used to generate excel for the image.

    Thanks.

    Thursday, January 2, 2020 7:10 AM