This Is My code
- <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">
- <tabel>
- <tr>
- <asp:Label ID="lblPname" runat="server" Text="" Font-Bold="True"></asp:Label>
- </tr>
- <tr>
- <asp:PlaceHolder ID="phBarcode" runat="server" Visible="true"></asp:PlaceHolder>
- </tr>
- </tabel>
- </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>
- string barCode;
- 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();
-
- 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);
- 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();
- }
My Excel output is
Please help me to solve this issue