dhoni kholi

dhoni kholi

  • NA
  • 198
  • 42.8k

How to Display embedded base64 image in Excel

Jan 1 2020 11:37 PM
This Is My code 
 
  1. <div id="Barcode">  
  2.        <h1 align="center"   
  3.               style="text-decoration: underline; font-size: x-large; font-weight: bold">Barcode</h1>  
  4.         
  5.         <div id="excel1" runat="server">  
  6.           <asp:Panel ID="Panel1" runat="server">  
  7.           <tabel>  
  8.           <tr>  
  9.               <asp:Label ID="lblPname" runat="server" Text="" Font-Bold="True"></asp:Label>   
  10.               </tr>  
  11.             <tr>  
  12.               <asp:PlaceHolder ID="phBarcode" runat="server" Visible="true"></asp:PlaceHolder>  
  13.             </tr>  
  14.             </tabel>  
  15.               </asp:Panel>  
  16.          </div>  
  17.           <div class="table-responsive";>  
  18.              <table class="table">  
  19.                 <tbody>  
  20.                 <tr>  
  21.                 <td>  
  22.                     <asp:Button ID="btnPPdf" runat="server" Text="Print" CssClass="btn-info"   
  23.                         Width="100px" OnClientClick = "return PrintPanel();" />  
  24.                   
  25.                      <asp:Button ID="btnExcel" runat="server" Text="Excel" CssClass="btn-success"   
  26.                         Width="100px" onclick="btnExcel_Click"  />  
  27.                   
  28.                      <asp:Button ID="btnBack" runat="server" Text="Back" CssClass="btn-danger"   
  29.                         Height="35px" Width="100px" />  
  30.                 </td>  
  31.                 </tr>  
  32.                 </tbody>   
  33.              </table>  
  34.          </div>  
  35.       </div>  
  1. string barCode;  
  2.        protected void Page_Load(object sender, EventArgs e)  
  3.        {  
  4.            
  5.            lblPname.Text = "Raj";  
  6.            barCode = "G-212012123";  
  7.            Barcode();  
  8.   
  9.        }  
  10.        private void Barcode()  
  11.        {  
  12.           System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();  
  13.           imgBarCode.Height = 70;  
  14.           imgBarCode.Width = 250;  
  15.        using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))  
  16.        {  
  17.            using (Graphics graphics = Graphics.FromImage(bitMap))  
  18.            {  
  19.                Font oFont = new Font("IDAutomationHC39M", 16);  
  20.                PointF point = new PointF(2f, 2f);  
  21.                SolidBrush blackBrush = new SolidBrush(Color.Black);  
  22.                SolidBrush whiteBrush = new SolidBrush(Color.White);  
  23.                graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);  
  24.                graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);  
  25.            }  
  26.            using (MemoryStream ms = new MemoryStream())  
  27.            {  
  28.                bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);  
  29.                byte[] byteImage = ms.ToArray();  
  30.   
  31.                Convert.ToBase64String(byteImage);  
  32.                imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);  
  33.                }  
  34.                phBarcode.Controls.Add(imgBarCode);  
  35.            }  
  36.        }  
  37.   
  38.        protected void btnExcel_Click(object sender, EventArgs e)  
  39.        {  
  40.            Response.Clear();  
  41.            Response.AddHeader("content-disposition""attachment;filename=FileName.xls");  
  42.            Response.Charset = "";  
  43.            Response.ContentType = "application/vnd.xls";  
  44.            System.IO.StringWriter stringWrite = new System.IO.StringWriter();  
  45.            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);   
  46.            Table table = new Table();  
  47.            TableRow row = new TableRow();  
  48.            row.Cells.Add(new TableCell());  
  49.            row.Cells[0].Controls.Add(Panel1);  
  50.            table.Rows.Add(row);  
  51.            table.RenderControl(htmlWrite);  
  52.            Response.Write(stringWrite.ToString());  
  53.            Response.End();  
  54.        }  
My Excel output is 
 
 
 
Please help me to solve this issue