ASP.NET关于条形码的生成问题兼网页打印

条形码是迄今为止最经济、实用的一种自动识别技术。条形码技术具有以下几个方面的优点

  A.输入速度快:与键盘输入相比,条形码输入的速度是键盘输入的5倍,并且能实现“即时数据输入”。

  B.可靠性高:键盘输入数据出错率为三百分之一,利用光学字符识别技术出错率为万分之一,而采用条形码技术误码率低于百万分之一。

  C.采集信息量大:利用传统的一维条形码一次可采集几十位字符的信息,二维条形码更可以携带数千个字符的信息,并有一定的自动纠错能力。

  D.灵活实用:条形码标识既可以作为一种识别手段单独使用,也可以和有关识别设备组成一个系统实现自动化识别,还可以和其他控制设备联接起来实现自动化管理。

  另外,条形码标签易于制作,对设备和材料没有特殊要求,识别设备操作容易,不需要特殊培训,且设备也相对便宜。

最近做了个项目需要条形码和打印条形码

这里我收集了两种制作条形码的方法。

第一就是用字符替换生成,这个简单,不需要很多编程。

代码如下:

/// <summary>
        /// 条形码生成
        /// </summary>
        /// <param name="strTemp">要生成条形码的文本</param>
        /// <param name="height">每个_和|的高度</param>
        /// <param name="width">每个_和|的宽度</param>
        /// <param name="showstrTemp">是否显示文本</param>
        /// <example>Response.Write(CreateBarCode("6911989251236", 50, 1, true));</example>
        /// <returns></returns>
        public string CreateBarCode(string text, int height, int width, bool showText)
        {
            string strTemp = text.ToLower();

            //替换各个字符
            strTemp = strTemp.Replace("0", "_|_|__||_||_|"); ;
            strTemp = strTemp.Replace("1", "_||_|__|_|_||");
            strTemp = strTemp.Replace("2", "_|_||__|_|_||");
            strTemp = strTemp.Replace("3", "_||_||__|_|_|");
            strTemp = strTemp.Replace("4", "_|_|__||_|_||");
            strTemp = strTemp.Replace("5", "_||_|__||_|_|");
            strTemp = strTemp.Replace("7", "_|_|__|_||_||");
            strTemp = strTemp.Replace("6", "_|_||__||_|_|");
            strTemp = strTemp.Replace("8", "_||_|__|_||_|");
            strTemp = strTemp.Replace("9", "_|_||__|_||_|");
            strTemp = strTemp.Replace("a", "_||_|_|__|_||");
            strTemp = strTemp.Replace("b", "_|_||_|__|_||");
            strTemp = strTemp.Replace("c", "_||_||_|__|_|");
            strTemp = strTemp.Replace("d", "_|_|_||__|_||");
            strTemp = strTemp.Replace("e", "_||_|_||__|_|");
            strTemp = strTemp.Replace("f", "_|_||_||__|_|");
            strTemp = strTemp.Replace("g", "_|_|_|__||_||");
            strTemp = strTemp.Replace("h", "_||_|_|__||_|");
            strTemp = strTemp.Replace("i", "_|_||_|__||_|");
            strTemp = strTemp.Replace("j", "_|_|_||__||_|");
            strTemp = strTemp.Replace("k", "_||_|_|_|__||");
            strTemp = strTemp.Replace("l", "_|_||_|_|__||");
            strTemp = strTemp.Replace("m", "_||_||_|_|__|");
            strTemp = strTemp.Replace("n", "_|_|_||_|__||");
            strTemp = strTemp.Replace("o", "_||_|_||_|__|");
            strTemp = strTemp.Replace("p", "_|_||_||_|__|");
            strTemp = strTemp.Replace("r", "_||_|_|_||__|");
            strTemp = strTemp.Replace("q", "_|_|_|_||__||");
            strTemp = strTemp.Replace("s", "_|_||_|_||__|");
            strTemp = strTemp.Replace("t", "_|_|_||_||__|");
            strTemp = strTemp.Replace("u", "_||__|_|_|_||");
            strTemp = strTemp.Replace("v", "_|__||_|_|_||");
            strTemp = strTemp.Replace("w", "_||__||_|_|_|");
            strTemp = strTemp.Replace("x", "_|__|_||_|_||");
            strTemp = strTemp.Replace("y", "_||__|_||_|_|");
            strTemp = strTemp.Replace("z", "_|__||_||_|_|");
            strTemp = strTemp.Replace("-", "_|__|_|_||_||");
            strTemp = strTemp.Replace("*", "_|__|_||_||_|");
            strTemp = strTemp.Replace("/", "_|__|__|_|__|");
            strTemp = strTemp.Replace("%", "_|_|__|__|__|");
            strTemp = strTemp.Replace("+", "_|__|_|__|__|");
            strTemp = strTemp.Replace(".", "_||__|_|_||_|");

            //替换字符中的_和|
            strTemp = strTemp.Replace("_", "<span style='height:" + height + ";" + width + ";background:#FFFFFF;'></span>");
            strTemp = strTemp.Replace("|", "<span style='height:" + height + ";" + width + ";background:#000000;'></span>");

            if (showText)
            {
                return strTemp + "<br/>" + text;
            }
            else
            {
                return strTemp;
            }
        }

效果如下:

第二需要用到三方控件,GenCode128.dll

在一个网页codeimage。aspx通过传入的条形码号生成相应的条形码

再在需要的页面用已个IMAGE控件来呈现出来。

先列举codeimage。aspx。CS的代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GenCode128;

public partial class common_codeimage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

         string   num = Request["num"].ToString();
           // string num = "1231231232";
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.Drawing.Image myimg = Code128Rendering.MakeBarcodeImage(num, 2, true);
        myimg.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());
        Response.End();
   
    }
}

这里需要注意两点,第一,写上using GenCode128;同时在BIN文件要应用这个DLL。

接着就很简单了,在需要打印页面上通过事件传值给这个条形码生成页面,原理和验证吗一样的。

  img.ImageUrl = "~/aaa/codeimage.aspx?num=" + "Odf"+Convert.ToString(DataBinder.Eval(e.Item.DataItem, "编号"));

这个就可以看到条形码了 。

最后简单说说打印的部分

这个页面是打印页面,通过SESSION传值生成的多个条形码。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="rdrytmdy6_list.aspx.cs" Inherits="入滇人员管理_rdrydy6_list" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>省外入滇企业执证人员条码打印</title>
            <script language="Javascript">
function preview()
{
    bdhtml=window.document.body.innerHTML;
    sprnstr="<!--startprint-->";
    eprnstr="<!--endprint-->";
    prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
    prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
    window.document.body.innerHTML=prnhtml;
    window.print();
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:Button ID="btndaochu" runat="server" Text="导出数据" OnClick="btndaochu_Click" />
          <input type="button" name="print" value="预览并打印" onclick="preview()">
        <br />
        <br />
        <br />


        <div runat="server" id="aa">
         <!--startprint-->
     <div>

        <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
            <ItemTemplate>
                姓名:<asp:Label ID="lblname" runat="server" Width="133px" Text=<%#Eval("姓名") %>></asp:Label><br />
                工作单位:<asp:Label ID="lblDeptworkname" runat="server" Text=<%#Eval("工作单位") %> Width="431px"></asp:Label><br />
                证书类别:<asp:Label ID="lbltype" runat="server" Text=<%#Eval("执证类别") %> Width="339px"></asp:Label><br />
                证书编号:<asp:Label ID="lblid" runat="server" Text=<%#Eval("执证号码") %> Width="426px"></asp:Label><br />
                <asp:Image ID="imgid" runat="server" /><br />
               &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                   <asp:Label ID="lbl" runat="server"  Width="193px"></asp:Label><br />
            </ItemTemplate>
            <SeparatorTemplate>
                <br />
            </SeparatorTemplate>
        </asp:DataList>
         </div>
     <!--endprint-->

    </div>
    </div>
    </form>
</body>
</html>

 打印出来的部分就是  <!--startprint-->
和 <!--endprint-->之间的。

顺便附下CS代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class 入滇人员管理_rdrydy6_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Session["table"] != null)
            {
                DataSet ds = (DataSet)Session["table"];
                DataList1.DataSource = ds.Tables[0].DefaultView;
                DataList1.DataBind();
            }
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image img = (Image)e.Item.FindControl("imgid");
            img.ImageUrl = "~/aaa/codeimage.aspx?num=" + "Odf"+Convert.ToString(DataBinder.Eval(e.Item.DataItem, "编号"));
            Label lbl = (Label)e.Item.FindControl("lbl");
            lbl.Text = "Odf" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "编号"));
        }
    }
    protected void btndaochu_Click(object sender, EventArgs e)
    {
        ExportData("application/ms-word", "Word.doc");
    }
    private void ExportData(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);


        //ExpertControl(this, DocumentType.Word);
       
        this.aa.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
}

希望有高手给点新的见解和指出不足,谢谢

原文地址:https://www.cnblogs.com/tongdengquan/p/6090621.html