.net实现生成及打印条形码功能,下面简单总结了自己的实现方法。
1.引用dll文件
将FLX.WebControls.dll文件拷贝到顶目的bin文件夹目录中;
2.配置web.config文件
在<system.web></system.web>之间添加如下代码
<httpHandlers>
<add verb="*" path="FlxBarCode.axd" type="FLX.WebControls.FlxBarCodeHandler,FLX.WebControls" />
</httpHandlers>
3.新建aspx文件,代码如下
前台文件:
Code
<HTML>
<HEAD>
<title>打印条形码</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="Css/baseStyle.css" type="text/css" rel="stylesheet">
<style media="print">.noprint { DISPLAY: none }
</style>
<script language="javascript" src="scripts/print.js" charset="gb2312"></script>
<STYLE> DIV.page { PAGE-BREAK-BEFORE: always } </STYLE>
</HEAD>
<body>
<form id="Form2" method="post" runat="server">
<span id="message" class="noprint"></span>
<br>
<span id="span" class="noprint"></span>
<br>
<br>
<table border="1" cellspacing="0" cellpadding="0" width="70" bordercolor="#000000">
<tr>
<td>
<table>
<tr>
<td width="100%" valign="top" align="center"><asp:Label id="LblName" runat="server" Font-Size="14px" Font-Bold="True"></asp:Label></td>
</tr>
<tr>
<td width="100%" valign="top">
<p align="right" style="PADDING-RIGHT:5px">
<asp:Image id="ImageCode" runat="server"></asp:Image>
</p>
<P><FONT face="宋体"></FONT></P>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</HTML>
<HTML>
<HEAD>
<title>打印条形码</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="Css/baseStyle.css" type="text/css" rel="stylesheet">
<style media="print">.noprint { DISPLAY: none }
</style>
<script language="javascript" src="scripts/print.js" charset="gb2312"></script>
<STYLE> DIV.page { PAGE-BREAK-BEFORE: always } </STYLE>
</HEAD>
<body>
<form id="Form2" method="post" runat="server">
<span id="message" class="noprint"></span>
<br>
<span id="span" class="noprint"></span>
<br>
<br>
<table border="1" cellspacing="0" cellpadding="0" width="70" bordercolor="#000000">
<tr>
<td>
<table>
<tr>
<td width="100%" valign="top" align="center"><asp:Label id="LblName" runat="server" Font-Size="14px" Font-Bold="True"></asp:Label></td>
</tr>
<tr>
<td width="100%" valign="top">
<p align="right" style="PADDING-RIGHT:5px">
<asp:Image id="ImageCode" runat="server"></asp:Image>
</p>
<P><FONT face="宋体"></FONT></P>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</HTML>
后台文件:
Code
protected System.Web.UI.WebControls.Image ImageCode;
protected System.Web.UI.WebControls.Label LblName;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.LblName.Text =Request.QueryString["Name"].ToString();
this.ImageCode.ImageUrl =Request.QueryString["Url"].ToString();
}
protected System.Web.UI.WebControls.Image ImageCode;
protected System.Web.UI.WebControls.Label LblName;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.LblName.Text =Request.QueryString["Name"].ToString();
this.ImageCode.ImageUrl =Request.QueryString["Url"].ToString();
}
引用JS代码:
Code
String.prototype.getQuery = function(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = this.substr(this.indexOf("\?")+1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
//创建WebBrowser控件
var createWebBrowser = function(){
document.getElementById("span").innerHTML = '<object id="WebBrowser" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
}
//创建按钮
var createButton = function(vID,vValue,eventHandler){
var btnButton = document.createElement("INPUT");
btnButton.type ="Button";
btnButton.id = vID;
btnButton.value = vValue;
btnButton.onclick = eventHandler;
document.getElementById("span").appendChild(btnButton);
}
//创建标签
var createSpan = function(vID,vValue,eventHandler){
var htmSpan = document.createElement("span");
htmSpan.id = vID;
htmSpan.innerHTML = vValue;
if(eventHandler != null)
htmSpan.onclick = eventHandler;
document.getElementById("span").appendChild(htmSpan);
}
//创建标签
var createTextBox = function(vID,vValue,eventHandler,width){
var btnButton = document.createElement("INPUT");
btnButton.type ="text";
btnButton.id = vID;
btnButton.value = vValue;
btnButton.width = width;
if(eventHandler != null)
btnButton.onclick = eventHandler;
document.getElementById("span").appendChild(btnButton);
}
//按钮事件
var print = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(6,1);
}
var directPrint = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(6,6);
}
var PrintSetUp = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(8,1);
}
var PrintReview = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(7,1);
}
var PrintFirst = function(){
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex=1";
}
var PrintLast = function(){
var last = parseInt(window.location.toString().getQuery('LastPage'));
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+last;
}
var PrintPre = function(){
var index = parseInt(window.location.toString().getQuery('PageIndex'));
if(index>1)
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+(index-1);
else alert("到头了");
}
var PrintNext = function(){
var index = parseInt(window.location.toString().getQuery('PageIndex'));
var last = parseInt(window.location.toString().getQuery('LastPage'));
if((index+1)<=last)
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+(index+1);
else alert("到头了");
}
var ShowPageIndex= function(){
}
var GoPage= function(){
var pageSize = document.getElementById('txtPageSize').value.replace(/\s+/,"");
if(/^\d+$/.test(pageSize))
{
var href = window.location.href.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
.replace(/\?LastPage=(\d)*$/g,"").replace(/\?LastPage=(\d)*&/g,"?").replace(/&LastPage=(\d)*&/g,"&").replace(/&LastPage=(\d)*$/g,"")
.replace(/\?PageSize=(\d)*$/g,"").replace(/\?PageSize=(\d)*&/g,"?").replace(/&PageSize=(\d)*&/g,"&").replace(/&PageSize=(\d)*$/g,"")
.replace(/\?pagesize=(\d)*$/g,"").replace(/\?pagesize=(\d)*&/g,"?").replace(/&pagesize=(\d)*&/g,"&").replace(/&pagesize=(\d)*$/g,"")
+"&PageSize="+pageSize;
window.location.href = href;
}
else
{
alert("请输入数字");
document.getElementById('txtPageSize').focus();
}
}
//设置网页打印的页眉页脚为空
function pagesetup_null(){
var hkey_root,hkey_path,hkey_key;
hkey_root="HKEY_CURRENT_USER"
hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
try{
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key="header";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
hkey_key="footer";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
}catch(e){}
}
document.getElementById('span').innerHTML = "";
createWebBrowser();
createButton("btn0","打印",print);
createButton("btn1","直接打印",directPrint);
createButton("btn2","页面设置",PrintSetUp);
createButton("btn3","打印预览",PrintReview);
if(window.location.toString().getQuery('PageSize') != null || window.location.toString().getQuery('pagesize') != null)
{
createButton("btn4","第一页",PrintFirst);
createButton("btn5","上一页",PrintPre);
createButton("btn6","下一页",PrintNext);
createButton("btn7","末 页",PrintLast);
var index = parseInt(window.location.toString().getQuery('PageIndex'));
var last = parseInt(window.location.toString().getQuery('LastPage'));
createSpan("span0"," 当前第 "+index+"/"+last+" 页 ",null);
if(index==1){document.getElementById("btn5").disabled = true;document.getElementById("btn4").disabled = true;}
if(index==last){document.getElementById("btn6").disabled = true;document.getElementById("btn7").disabled = true;}
if(window.location.toString().getQuery('UserSetSize') != null || window.location.toString().getQuery('usersetsize') != null)
{
var usersetsize = "";
if(window.location.toString().getQuery('UserSetSize') != null)
usersetsize = window.location.toString().getQuery('UserSetSize');
else if(window.location.toString().getQuery('usersetsize') != null)
usersetsize = window.location.toString().getQuery('usersetsize');
if(usersetsize.toUpperCase()=='Y')
{
createSpan("span1","每页显示",null);
var pagesize = 0;
if(window.location.toString().getQuery('PageSize') != null)
pagesize = parseInt(window.location.toString().getQuery('PageSize'));
else if
String.prototype.getQuery = function(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = this.substr(this.indexOf("\?")+1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
//创建WebBrowser控件
var createWebBrowser = function(){
document.getElementById("span").innerHTML = '<object id="WebBrowser" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
}
//创建按钮
var createButton = function(vID,vValue,eventHandler){
var btnButton = document.createElement("INPUT");
btnButton.type ="Button";
btnButton.id = vID;
btnButton.value = vValue;
btnButton.onclick = eventHandler;
document.getElementById("span").appendChild(btnButton);
}
//创建标签
var createSpan = function(vID,vValue,eventHandler){
var htmSpan = document.createElement("span");
htmSpan.id = vID;
htmSpan.innerHTML = vValue;
if(eventHandler != null)
htmSpan.onclick = eventHandler;
document.getElementById("span").appendChild(htmSpan);
}
//创建标签
var createTextBox = function(vID,vValue,eventHandler,width){
var btnButton = document.createElement("INPUT");
btnButton.type ="text";
btnButton.id = vID;
btnButton.value = vValue;
btnButton.width = width;
if(eventHandler != null)
btnButton.onclick = eventHandler;
document.getElementById("span").appendChild(btnButton);
}
//按钮事件
var print = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(6,1);
}
var directPrint = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(6,6);
}
var PrintSetUp = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(8,1);
}
var PrintReview = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(7,1);
}
var PrintFirst = function(){
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex=1";
}
var PrintLast = function(){
var last = parseInt(window.location.toString().getQuery('LastPage'));
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+last;
}
var PrintPre = function(){
var index = parseInt(window.location.toString().getQuery('PageIndex'));
if(index>1)
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+(index-1);
else alert("到头了");
}
var PrintNext = function(){
var index = parseInt(window.location.toString().getQuery('PageIndex'));
var last = parseInt(window.location.toString().getQuery('LastPage'));
if((index+1)<=last)
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+(index+1);
else alert("到头了");
}
var ShowPageIndex= function(){
}
var GoPage= function(){
var pageSize = document.getElementById('txtPageSize').value.replace(/\s+/,"");
if(/^\d+$/.test(pageSize))
{
var href = window.location.href.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
.replace(/\?LastPage=(\d)*$/g,"").replace(/\?LastPage=(\d)*&/g,"?").replace(/&LastPage=(\d)*&/g,"&").replace(/&LastPage=(\d)*$/g,"")
.replace(/\?PageSize=(\d)*$/g,"").replace(/\?PageSize=(\d)*&/g,"?").replace(/&PageSize=(\d)*&/g,"&").replace(/&PageSize=(\d)*$/g,"")
.replace(/\?pagesize=(\d)*$/g,"").replace(/\?pagesize=(\d)*&/g,"?").replace(/&pagesize=(\d)*&/g,"&").replace(/&pagesize=(\d)*$/g,"")
+"&PageSize="+pageSize;
window.location.href = href;
}
else
{
alert("请输入数字");
document.getElementById('txtPageSize').focus();
}
}
//设置网页打印的页眉页脚为空
function pagesetup_null(){
var hkey_root,hkey_path,hkey_key;
hkey_root="HKEY_CURRENT_USER"
hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
try{
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key="header";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
hkey_key="footer";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
}catch(e){}
}
window.onload
= function(){document.getElementById('span').innerHTML = "";
createWebBrowser();
createButton("btn0","打印",print);
createButton("btn1","直接打印",directPrint);
createButton("btn2","页面设置",PrintSetUp);
createButton("btn3","打印预览",PrintReview);
if(window.location.toString().getQuery('PageSize') != null || window.location.toString().getQuery('pagesize') != null)
{
createButton("btn4","第一页",PrintFirst);
createButton("btn5","上一页",PrintPre);
createButton("btn6","下一页",PrintNext);
createButton("btn7","末 页",PrintLast);
var index = parseInt(window.location.toString().getQuery('PageIndex'));
var last = parseInt(window.location.toString().getQuery('LastPage'));
createSpan("span0"," 当前第 "+index+"/"+last+" 页 ",null);
if(index==1){document.getElementById("btn5").disabled = true;document.getElementById("btn4").disabled = true;}
if(index==last){document.getElementById("btn6").disabled = true;document.getElementById("btn7").disabled = true;}
if(window.location.toString().getQuery('UserSetSize') != null || window.location.toString().getQuery('usersetsize') != null)
{
var usersetsize = "";
if(window.location.toString().getQuery('UserSetSize') != null)
usersetsize = window.location.toString().getQuery('UserSetSize');
else if(window.location.toString().getQuery('usersetsize') != null)
usersetsize = window.location.toString().getQuery('usersetsize');
if(usersetsize.toUpperCase()=='Y')
{
createSpan("span1","每页显示",null);
var pagesize = 0;
if(window.location.toString().getQuery('PageSize') != null)
pagesize = parseInt(window.location.toString().getQuery('PageSize'));
else if