C# 制作39条形码

public void aspnet_barcode()
    {
        HttpContext c = new HttpContext(Request, Response);
        //获取生成条形码的高度
        if (c.Request.QueryString["height"] != null)
        {
            _height = Convert.ToInt32(c.Request.QueryString["height"].ToString());
        }
        //获取生成条形码的编号
        if (c.Request.QueryString["barcode"] != null)
        {
        }
        //调用自定义BarCode方法,打印出条形码
        barcode = BarCode("*"+Session["receipt"].ToString ()+"*", 1);
        //barcode = BarCode(Label1.Text, 1);
        int p_l = barcode.Length;
        int p_h = _height ;
        //输出生成条形码的图片
        c.Response.ContentType = "image/gif";
        Bitmap myBitmap = new Bitmap(p_l, p_h);
        //创建一个画布
        Graphics myGrap = Graphics.FromImage(myBitmap);
        //清空背景色
        myGrap.Clear(Color.White);
        for (int i = 0; i < p_l; i++)
        {
            //创建画笔对象
            Pen myPen = new Pen(Color.White, 1);
            if (barcode.Substring(i, 1) == "|")
            {
                myPen.Color = Color.Black;
            }
            //绘制一条连接由坐标对指定的两点连线
            myGrap.DrawLine(myPen, i, 0, i, _height);
        }
        //myGrap.DrawString(Session["receipt"].ToString(), new Font("Courier New", 10), new SolidBrush(Color.Black), -4, _height);
        //将此图像以指定格式保存到数据流中
        myBitmap.Save(c.Response.OutputStream, ImageFormat.Gif);
        string savePath = Server.MapPath("Receipt/" + Session["receipt"] + ".jpg");

        if (!System.IO.File.Exists(savePath))
        {
            myBitmap.Save(savePath);
            c.Response.End();
          
        }
        
    }
    public static string BarCode(object str, int type_code)
    {
        #region
        string strTmp = str.ToString();
        string code = strTmp;
        strTmp = strTmp.ToLower();
        strTmp = strTmp.Replace("0", "_|_|__||_||_|"); ;
        strTmp = strTmp.Replace("1", "_||_|__|_|_||");
        strTmp = strTmp.Replace("2", "_|_||__|_|_||");
        strTmp = strTmp.Replace("3", "_||_||__|_|_|");
        strTmp = strTmp.Replace("4", "_|_|__||_|_||");
        strTmp = strTmp.Replace("5", "_||_|__||_|_|");
        strTmp = strTmp.Replace("7", "_|_|__|_||_||");
        strTmp = strTmp.Replace("6", "_|_||__||_|_|");
        strTmp = strTmp.Replace("8", "_||_|__|_||_|");
        strTmp = strTmp.Replace("9", "_|_||__|_||_|");
        strTmp = strTmp.Replace ("*","_|__|_||_||_|");
        if (type_code == 1)
        {
            return strTmp + "<BR>" + code;
        }
        else
        {
            return strTmp;
        }
        #endregion
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要批量生成39码(Code 39条形码并保存到Excel中,您可以使用C#中的BarcodeLib库和EPPlus库来实现。以下是一个示例代码: 首先,确保在项目中引用了BarcodeLib和EPPlus库。 ```csharp using BarcodeLib; using OfficeOpenXml; ``` 然后,可以使用以下代码来生成39条形码并保存到Excel中: ```csharp // 创建一个新的Excel文件 using (ExcelPackage package = new ExcelPackage()) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Barcodes"); // 生成39条形码并保存到Excel中 Barcode barcode = new Barcode(); int startingRow = 1; for (int i = 1; i <= 10; i++) // 生成10个条形码,可以根据需求进行调整 { string barcodeText = "CODE" + i.ToString(); // 条形码文本 Image barcodeImage = barcode.Encode(TYPE.CODE39, barcodeText, Color.Black, Color.White, 300, 100); // 生成39条形码图片 // 将图片插入到Excel单元格中 ExcelPicture excelPicture = worksheet.Drawings.AddPicture("Barcode" + i.ToString(), barcodeImage); excelPicture.SetPosition(startingRow, 1); // 调整单元格大小以适应图片 worksheet.Row(startingRow).Height = barcodeImage.Height; startingRow++; // 下一行 } // 保存Excel文件 FileInfo excelFile = new FileInfo("C:\\Path\\to\\your\\ExcelFile.xlsx"); package.SaveAs(excelFile); } ``` 上述代码将在Excel文件的名为"Barcodes"的工作表中生成10个39条形码,并将它们保存到指定的Excel文件中。您可以根据需要更改生成条形码数量和Excel文件的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值