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

2024-04-17 00:08

本文主要是介绍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:" + width + ";background:#FFFFFF;'></span>");
            strTemp = strTemp.Replace("|", "<span style='height:" + height + ";width:" + 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="预览并打印" οnclick="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)
    {

    }
}

 

 

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

这篇关于ASP.NET关于条形码的生成问题兼网页打印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/910279

相关文章

MySQL启动报错:InnoDB表空间丢失问题及解决方法

《MySQL启动报错:InnoDB表空间丢失问题及解决方法》在启动MySQL时,遇到了InnoDB:Tablespace5975wasnotfound,该错误表明MySQL在启动过程中无法找到指定的s... 目录mysql 启动报错:InnoDB 表空间丢失问题及解决方法错误分析解决方案1. 启用 inno

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

Java使用MethodHandle来替代反射,提高性能问题

《Java使用MethodHandle来替代反射,提高性能问题》:本文主要介绍Java使用MethodHandle来替代反射,提高性能问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录一、认识MethodHandle1、简介2、使用方式3、与反射的区别二、示例1、基本使用2、(重要)

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

电脑蓝牙连不上怎么办? 5 招教你轻松修复Mac蓝牙连接问题的技巧

《电脑蓝牙连不上怎么办?5招教你轻松修复Mac蓝牙连接问题的技巧》蓝牙连接问题是一些Mac用户经常遇到的常见问题之一,在本文章中,我们将提供一些有用的提示和技巧,帮助您解决可能出现的蓝牙连接问... 蓝牙作为一种流行的无线技术,已经成为我们连接各种设备的重要工具。在 MAC 上,你可以根据自己的需求,轻松地

Java 中的跨域问题解决方法

《Java中的跨域问题解决方法》跨域问题本质上是浏览器的一种安全机制,与Java本身无关,但Java后端开发者需要理解其来源以便正确解决,下面给大家介绍Java中的跨域问题解决方法,感兴趣的朋友一起... 目录1、Java 中跨域问题的来源1.1. 浏览器同源策略(Same-Origin Policy)1.

如何清理MySQL中的binlog问题

《如何清理MySQL中的binlog问题》:本文主要介绍清理MySQL中的binlog问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目http://www.chinasem.cn录清理mysql中的binlog1.查看binlog过期时间2. 修改binlog过期

如何解决yum无法安装epel-release的问题

《如何解决yum无法安装epel-release的问题》:本文主要介绍如何解决yum无法安装epel-release的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录yum无法安装epel-release尝试了第一种方法第二种方法(我就是用这种方法解决的)总结yum

MybatisX快速生成增删改查的方法示例

《MybatisX快速生成增删改查的方法示例》MybatisX是基于IDEA的MyBatis/MyBatis-Plus开发插件,本文主要介绍了MybatisX快速生成增删改查的方法示例,文中通过示例代... 目录1 安装2 基本功能2.1 XML跳转2.2 代码生成2.2.1 生成.xml中的sql语句头2

IDEA下"File is read-only"可能原因分析及"找不到或无法加载主类"的问题

《IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题》:本文主要介绍IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题,具有很好的参... 目录1.File is read-only”可能原因2.“找不到或无法加载主类”问题的解决总结1.File