vb/java/c# 生成code128 条码/QR Code二维码 导出excel

公羊渝
2023-12-01

java/c#生成条形码/二维码图片,导出excel 会用到POI/NPOI,BarcodeLib、ZXing插件.
code128 如果不使用插件,也可使用下面vb/c#代码生成含有校验的条码文本,之后导出excel时候设置字体"code 128"(cs_barcode_Font.FontName = “code 128”?


[code 128 校验 vb]
新建一个Excel然后另存为启用宏的Excel工作簿(后缀为.xlsm),如果保存时候提示 “文档检查器”警告,需要在Excel选项的 信任中心 设置,去掉 “文档特性设置”里面的勾。
Excel -》开发工具-》Visual Basic  增加以下代码:
Function code128b(Tar As Range)   '128B码:ChrW(204).
Dim s , i , i%, ss ,i, j%, curR%, checkB%
curR = Tar.Row
s = Tar.Value
checkB = 1  '开始位的码值为104 mod 103 =1
For i = 1 To Len(s)
    ss = Mid(s, i, 1)
    j = Asc(ss) '不过滤无效字符,比如汉字.
    If j < 135 Then
        j = j - 32
    ElseIf j > 134 Then
        j = j - 100
    End If
    checkB = (checkB + i * j) Mod 103   '计算校验位
Next
If checkB < 95 And checkB > 0 Then  '有的资料直接求103的模,解说不充分,因为有的校验位超过127时,系统会"吃"掉它们(连带休止符).
    checkB = checkB + 32
ElseIf checkB > 94 Then '字体设置时,字模被定义了2个值.观察字体文件时能发现.
    checkB = checkB + 100
End If
code128b = ChrW(204) & s & IIf(checkB, ChrW(checkB), Chr(32)) & ChrW(206)
End Function


[code 128 校验 c#]
public string GetCode128A(string inputData)
{
string result = “”;
int checksum = 103;
int j = 1;
for (int ii = 0; ii < inputData.Length; ii++)
{
if (inputData[ii] >= 32)
{
checksum += (inputData[ii] - 32) * (ii + 1);
}
else
{
checksum += (inputData[ii] + 64) * (ii + 1);
}
}
checksum = checksum % 103;
if (checksum < 95)
{
checksum += 32;
}
else
{
checksum += 100;
}
result = Convert.ToChar(203) + inputData.ToString() + Convert.ToChar(checksum) + Convert.ToChar(206);
return result;
}

	public string GetCode128B(string inputData)
    {
        string result = "";
        int checksum = 104;
        int j = 1;
        for (int ii = 0; ii < inputData.Length; ii++)
        {
            if (inputData[ii] >= 32)
            {
                checksum += (inputData[ii] - 32) * (ii + 1);
            }
            else
            {
                checksum += (inputData[ii] + 64) * (ii + 1);
            }
        }
        checksum = checksum % 103;
        if (checksum < 95)
        {
            checksum += 32;
        }
        else
        {
            checksum += 100;
        }
        result = Convert.ToChar(204) + inputData.ToString() + Convert.ToChar(checksum) + Convert.ToChar(206);
        return result;
    }
	
	public static string GetCode128C(string inputData)
    {
        string result = "";
        int checksum = 105;
        int j = 1;
        for (int ii = 0; ii < inputData.Length; ii++)
        {
            if (ii % 2 == 0)
            {
                checksum += Convert.ToInt32(inputData.Substring(ii, 2)) * j;
                if (Convert.ToInt32(inputData.Substring(ii, 2)) < 95)
                {
                    result += Convert.ToChar(Convert.ToInt32(inputData.Substring(ii, 2)) + 32);
                }
                else
                {
                    result += Convert.ToChar(Convert.ToInt32(inputData.Substring(ii, 2)) + 100);
                }
                j++;
            }
            ii++;
        }
        checksum = checksum % 103;
        if (checksum < 95)
        {
            checksum += 32;
        }
        else
        {
            checksum += 100;
        }
        result = Convert.ToChar(205) + result + Convert.ToChar(checksum) + Convert.ToChar(206);
        return result;
    }

援引资料:
https://blog.csdn.net/zhengjianqi/article/details/24309265
https://www.cnblogs.com/jerrmy/p/4356516.html
https://www.cnblogs.com/xxaxx/p/4408798.html

 类似资料: