不知道各位有沒有直接在WORD或EXCEL里直接設置過條碼,然后打印出來? 如果直接把內容設置為條碼字體,打印出來后是掃描槍是無法讀取的. 我們都知道要在內容前后加上"*"(這里和""只起引用,實際不用). 當然,這種只是39碼的起始字符. 但在實際應用途中,39碼往往不能夠滿足要求,需要打印成CODE128碼,但這時候就不能簡單的在打印內容前后加入"*"后設置為CODE128字體就可以了. 這需要通過算法添加不同的起始符. 下面結合我在實際的應用中,講一下這兩種方法(實際上是一種,這兩個函數的寫法均來自互聯網),
1. 是直接在SQL里取出相應的內容就已經添加.
2. 如果數據是在畫面生成,但不需經過數據庫獲取的話,就只能在報表里直接實現了.
把函數放到SQL里
ALTER FUNCTION [dbo].[StrToCode128B]( @Str NVARCHAR(200)='GetonJew')--128B碼:ChrW(204) RETURNS NVARCHAR(200) AS -- by GetonJew BEGIN DECLARE @checkB INT DECLARE @i INT ,@j INT DECLARE @str2 NVARCHAR(2) SET @i=1 SET @checkB = 1 --開始位的碼值為104 mod 103 =1 -- WHILE @i <= LEN(@Str) BEGIN SET @str2 = SUBSTRING(@Str,@i,1) SET @j = ASCII(@str2) --不過濾無效字符,比如漢字 IF @j<135 BEGIN SET @j=@j-32 END ELSE IF @j>134 BEGIN SET @j=@j-100 END SET @checkB = (@checkB + @i * @j) % 103 --計算校驗位 SET @i=@i+1 END IF @checkB<95 AND @checkB>0 --有的資料直接求103的模,解說不充分,因為有的校驗位超過127時,系統會"吃"掉它們(連帶休止符). BEGIN SET @checkB = @checkB + 32 END ELSE IF @checkB > 94 -- '字體設置時,字模被定義了2個值.觀察字體文件時能發現. BEGIN SET @checkB = @checkB + 100 END RETURN NCHAR(204) + @Str + CASE WHEN @checkB>0 THEN NCHAR(@checkB) ELSE NCHAR(32) END + NCHAR(206) END
在報表中的【自定義函數】新建一個函數保存為Code128,名字隨便定義,在代碼框中輸入如下代碼(Basic語法):
Function Code128 ( strIn As string ) As String Dim intLoop As Number Dim intPosition as Number Dim intTotalVal as Number Dim strOut as String Dim strSpChr as String Dim strEndChr as String Dim intEndNo as Number strOut = "" for intLoop = 0 to Len(strIn) - 1 intPosition = intLoop + 1 strSpChr = Mid(strIn, intPosition, 1) intTotalVal = intTotalVal + (Asc(strSpChr) - 32) * intPosition next intTotalVal = intTotalVal + 104 intTotalVal = intTotalVal mod 103 If intTotalVal >= 95 Then Select Case intTotalVal Case 95 strEndChr = "Ã" Case 96 strEndChr = "Ä" Case 97 strEndChr = "Å" Case 98 strEndChr = "Æ" Case 99 strEndChr = "Ç" Case 100 strEndChr = "È" Case 101 strEndChr = "É" Case 102 strEndChr = "Ê" End Select Else intTotalVal = intTotalVal + 32 strEndChr = Chr(intTotalVal) End If Code128 = "Ì" + strIn + strEndChr + "Î" End Function
下面是水晶報表設計,為顯示添加起始符后的數據,這里的批號就特意不以條碼字體顯示(Barlabel是添加了起始結束符號的LabelId)
下圖是調出來的結果.
看到了嗎? 每一個批號的結束符都是不一樣的.
下面是該SP的代碼.
ALTER procedure [dbo].[RPT_PrintLabel] @ponum varchar(50),@pronum varchar(50),@lot varchar(10),@label varchar(50),@prtnum varchar(5) as declare @sql varchar(5000) select @sql = 'select top '+@prtnum+ 'ID, PONum,VenNum,VenName,ProNum,Lot,ProDesc,Qty,Unit,BoxNum,LabelId,BarQty=dbo.StrToCode128B(Qty),BarLabel=dbo.StrToCode128B(LabelId) into #t from TEMPRT where PONum= ''' +@ponum+ ''' and ProNum= ''' +@pronum+''' and Lot= ''' +@lot+''' order by ID desc;select * from #t order by ID ASC ;drop table #t' print @sql exec(@sql)