跳至頁尾內容
使用IRONBARCODE
如何在Crystal Reports中使用VB.NET列印條碼 | IronBarcode

如何使用VB.NET在Crystal Reports中列印條碼

要在Crystal Reports中使用VB.NET列印條碼,請透過NuGet安裝IronBarcode,使用BarcodeWriter.CreateBarcode()生成條碼圖像,並將生成的二進制資料綁定到您的Crystal Report中的圖像欄位。 此方法消除了字體依賴性,並能在分鐘內生產可掃描、準備列印的條碼。

將條碼新增到Crystal Reports可以提升您的業務流程,使其更快、更準確——從跟蹤庫存到生成詳細報告。 如果您曾經嘗試在VB.NET中使用字體生成的條碼,您會知道使用複雜的公式、有限的格式和使人沮喪的列印機問題有多麻煩。

現代條碼生成SDK,如IronBarcode使得建立和列印條碼變得簡單。 在這個指南中,您將找到所需的一切:在Visual Studio中設置IronBarcode,生成條碼,並直接在您的Crystal Reports中顯示它們。 最終,您將能夠以最少的麻煩產生專業、可靠的條碼。

在Crystal Reports中列印條碼的方法是什麼?

有兩種主要方法可將條碼新增到Crystal Reports:基於字體的方法和SDK解決方案。

基於字體的方法需要在您的字體文件夾中安裝特殊條碼字體(True Type字體),並使用公式或使用者函式庫(UFL)來編碼資料。 雖然此方法可行,但通常涉及複雜的編碼公式、有限的條碼格式支持(如Code 39或資料矩陣)、以及不同的列印機和系統的字體渲染問題,這在Microsoft的文件關於字體渲染中已經討論。

基於SDK的解決方案提供了一種更可靠的替代方案。這些庫以程式方式生成條碼圖像,提供更好的可靠性、廣泛的格式支持和更輕鬆的實施。 IronBarcode體現了這種現代方法,允許開發人員生成條碼作為圖像,通過標準資料庫欄位或DataTable物件與Crystal Reports整合。 這種方法確保在所有平台上的一致渲染,並消除了在使用VB.NET列印條碼時的字體依賴問題。

在Crystal Reports中的基於字體的方法與基於SDK的方法的比較
因素 基於字體的方法 基於SDK的方法(IronBarcode)
設置的複雜性 高——需要字體安裝和編碼公式 低——單一NuGet包安裝
條碼格式支持 有限(Code 39, Code 128) 30多種格式,包括QR,資料矩陣,PDF417
跨列印機一致性 不一致——取決於安裝的字體 一致——基於圖像渲染
維護負擔 高——每台機器都必須管理字體 低——通過NuGet更新管理
掃描可靠性 可變 高——IronBarcode生成已驗證的圖像

如何為VB.NET安裝IronBarcode?

在VB.NET專案中設置IronBarcode只需最低配置。 首先,透過NuGet套件管理器安裝程式庫,執行以下指令:

Install-Package BarCode

或者,使用Visual Studio中的套件管理器UI搜索並安裝"BarCode"。 安裝後,在VB.NET程式碼中導入命名空間:

Imports IronBarCode
Imports IronBarCode
VB .NET

確認您的專案目標.NET Framework 4.6.2或更高版本,或任何版本的.NET Core,.NET 5,或.NET 6+。 IronBarcode適用於所有支持圖像欄位的Crystal Reports版本,這使其與現有項目具有廣泛的相容性。 有關詳細的安裝說明,請參閱IronBarcode入門指南

立即下載IronBarcode,開始在您的Crystal Reports中建立專業條碼。

IronBarcode支持哪些.NET版本?

IronBarcode目標支持廣泛的.NET執行環境:

  • .NET Framework 4.6.2及以上版本
  • .NET Core 3.1+
  • .NET 5,6,7,8,9
  • .NET Standard 2.0+

這種廣泛的相容性意味著您可以輕鬆地將IronBarcode整合到運行於.NET Framework上的傳統VB.NET應用程式以及目標使用.NET 8或更高版本的現代應用程式中。 請查看IronBarcode相容性文件以獲取完整的支持矩陣。

如何生成條碼並將其儲存在資料庫中?

使用IronBarcode建立條碼並將其儲存在為Crystal Reports儲存的方法中,涉及生成條碼圖像並使用SqlConnection或OleDbConnection將其保存到您的資料庫。 這是一個完整的例子:

選項1:使用SQL資料庫

Imports IronBarCode
Imports System.Data.SqlClient

Module BarcodeGenerator
    Sub CreateAndStoreBarcode()
        ' Generate a Code128 barcode
        Dim myBarcode = BarcodeWriter.CreateBarcode("PROD-12345", BarcodeWriterEncoding.Code128)
        ' Add readable text below the barcode
        myBarcode.AddBarcodeValueTextBelowBarcode()
        ' Resize to appropriate dimensions
        myBarcode.ResizeTo(400, 150)
        ' Connect to your database
        Using connection As New SqlConnection("YourConnectionString")
            Dim cmd As New SqlCommand(
                "INSERT INTO Products (ProductCode, BarcodeImage) VALUES (@Code, @Image)",
                connection)
            cmd.Parameters.AddWithValue("@Code", "PROD-12345")
            cmd.Parameters.AddWithValue("@Image", myBarcode.BinaryStream)
            connection.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Sub
End Module
Imports IronBarCode
Imports System.Data.SqlClient

Module BarcodeGenerator
    Sub CreateAndStoreBarcode()
        ' Generate a Code128 barcode
        Dim myBarcode = BarcodeWriter.CreateBarcode("PROD-12345", BarcodeWriterEncoding.Code128)
        ' Add readable text below the barcode
        myBarcode.AddBarcodeValueTextBelowBarcode()
        ' Resize to appropriate dimensions
        myBarcode.ResizeTo(400, 150)
        ' Connect to your database
        Using connection As New SqlConnection("YourConnectionString")
            Dim cmd As New SqlCommand(
                "INSERT INTO Products (ProductCode, BarcodeImage) VALUES (@Code, @Image)",
                connection)
            cmd.Parameters.AddWithValue("@Code", "PROD-12345")
            cmd.Parameters.AddWithValue("@Image", myBarcode.BinaryStream)
            connection.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Sub
End Module
VB .NET

此範例生成Code 128條碼並將其作為二進制資料儲存在SQL Server中。 AddBarcodeValueTextBelowBarcode()方法在條碼符號下方新增可讀文字,使它既可用於掃描又可用於目視檢查。 ResizeTo()方法調整尺寸以適應您的報告佈局。

選項2:使用DataTable

對於不需要持久儲存的情況,DataTable方法完全避免使用資料庫:

Imports IronBarCode
Imports System.Data

Function CreateBarcodeDataTable() As DataTable
    Dim dt As New DataTable()
    dt.Columns.Add("ProductCode", GetType(String))
    dt.Columns.Add("Barcode", GetType(Byte()))

    ' Generate a barcode for each product
    Dim row As DataRow = dt.NewRow()
    row("ProductCode") = "PROD-12345"

    Dim barcode = BarcodeWriter.CreateBarcode("PROD-12345", BarcodeWriterEncoding.Code128)
    barcode.AddBarcodeValueTextBelowBarcode()
    barcode.ResizeTo(400, 150)
    row("Barcode") = barcode.BinaryStream

    dt.Rows.Add(row)
    Return dt
End Function
Imports IronBarCode
Imports System.Data

Function CreateBarcodeDataTable() As DataTable
    Dim dt As New DataTable()
    dt.Columns.Add("ProductCode", GetType(String))
    dt.Columns.Add("Barcode", GetType(Byte()))

    ' Generate a barcode for each product
    Dim row As DataRow = dt.NewRow()
    row("ProductCode") = "PROD-12345"

    Dim barcode = BarcodeWriter.CreateBarcode("PROD-12345", BarcodeWriterEncoding.Code128)
    barcode.AddBarcodeValueTextBelowBarcode()
    barcode.ResizeTo(400, 150)
    row("Barcode") = barcode.BinaryStream

    dt.Rows.Add(row)
    Return dt
End Function
VB .NET

此DataTable方法適用於報告預覽、一次性列印作業或資料庫基礎架構不可用的情況。 您可以迴圈遍歷產品列表並為每個項目新增一行,按需生成條碼。 有關更多條碼格式,請探索支持的條碼型別或瀏覽IronBarcode教程

如何自定義條碼外觀?

除了簡單的生成之外,IronBarcode還提供了多種自定義選項,在列印報告中很有幫助:

  • 邊距 -- 使用myBarcode.SetMargins()為條碼新增白色空白區域,提高掃描可靠性
  • 顏色 -- 更改前景和背景顏色以適應品牌報告設計
  • 註釋 -- 使用AddBarcodeValueTextAboveBarcode()在條碼上方或下方新增自定義文字
  • 解析度 -- 使用myBarcode.SetDPI()設置DPI以獲得高質量的列印輸出

請參閱條碼自定義指南以獲取每個選項的程式碼範例。

如何在Crystal Reports中顯示條碼?

一旦條碼儲存在資料庫中,在Crystal Reports中顯示它們遵循標準的圖像欄位程式:

  1. 打開Crystal Reports並建立新報告或打開現有報告
  2. 在欄位瀏覽器中,右鍵點擊"資料庫欄位"並選擇"資料庫專家"

    如何在VB.NET中列印Crystal Reports中的條碼:圖1–資料庫專家窗口

  3. 連接到您的資料庫並選擇包括條碼圖像的表
  4. 將條碼圖像欄位拖放到您的報告設計面板上
  5. 根據需要調整欄位大小和位置

您的報告設計器應看起來如下:

如何在VB.NET中列印Crystal Reports中的條碼:圖2–報告設計佈局

若需在報告運行時間動態生成條碼,請修改您的DataTable或資料庫查詢,以即時生成條碼。 以下範例顯示如何將動態建立的DataTable綁定到CrystalReportViewer:

Imports IronBarCode
Imports System.Data

Function GetReportData() As DataTable
    Dim dt As New DataTable()
    dt.Columns.Add("ProductCode", GetType(String))
    dt.Columns.Add("Barcode", GetType(Byte()))

    Dim row As DataRow = dt.NewRow()
    row("ProductCode") = "CUSTOMER-12345"

    Dim barcode = BarcodeWriter.CreateBarcode("CUSTOMER-12345", BarcodeWriterEncoding.Code128)
    barcode.AddBarcodeValueTextBelowBarcode()
    barcode.ResizeTo(400, 150)
    row("Barcode") = barcode.BinaryStream

    dt.Rows.Add(row)
    Return dt
End Function
Imports IronBarCode
Imports System.Data

Function GetReportData() As DataTable
    Dim dt As New DataTable()
    dt.Columns.Add("ProductCode", GetType(String))
    dt.Columns.Add("Barcode", GetType(Byte()))

    Dim row As DataRow = dt.NewRow()
    row("ProductCode") = "CUSTOMER-12345"

    Dim barcode = BarcodeWriter.CreateBarcode("CUSTOMER-12345", BarcodeWriterEncoding.Code128)
    barcode.AddBarcodeValueTextBelowBarcode()
    barcode.ResizeTo(400, 150)
    row("Barcode") = barcode.BinaryStream

    dt.Rows.Add(row)
    Return dt
End Function
VB .NET

此方法在報告載入時動態生成條碼,無需在資料庫中預存圖像。 您可以使用Crystal Reports內建的匯出功能導出報告為PDF。 有關Crystal Reports資料源的更多資訊,請參閱SAP的Crystal Reports文件

輸出

您的Crystal Report將顯示帶有可讀文字和適當尺寸的條碼。 根據需要調整欄位大小或邊距,以適應掃描器、列印機或標籤。 條碼可以直接從您的VB.NET應用程式中包含在PDF、列印表單或標籤模板中。

如何在VB.NET中列印Crystal Reports中的條碼:圖3–使用Crystal Reports Viewer顯示生成的條碼

如何處理常見條碼列印問題?

在使用VB.NET實現條碼於Crystal Reports中時可能會出現幾個問題。 以下故障排除提示解決了最常見的問題:

Crystal Reports條碼列印的常見問題和解決方案
問題 原因 解決方案
條碼模糊 圖像解析度低 使用ResizeTo()設置較大的尺寸或設置更高的DPI
掃描失敗 安靜區域不足 使用SetMargins()新增邊距;為Code 39新增星號
資料庫連接錯誤 連接參數不正確 驗證OleDbConnection或SqlConnection字串和資料集綁定
資料庫中的資料型別錯誤 欄位未設置為二進制型別 使用imagevarbinary(max)欄型別
條碼無法顯示 權限或路徑問題 驗證Crystal Reports檢視器具有資料庫/DataTable存取權限
缺少DLL錯誤 安裝不完整 透過NuGet重新安裝;確認已在專案中引用IronBarcode DLL

有關其他故障排除支援,請存取IronBarcode故障排除指南IronBarcode支援論壇也可用於未在文件中涵蓋的特定問題和邊緣案例。

如何選擇合適的條碼格式?

選擇正確的條碼格式對於掃描硬體的相容性和資料容量很重要。 商業報告中最常見的格式是:

  • Code 128 —— 通用的線性條碼; 高資料密度; 大多數掃描器均可工作。 使用BarcodeWriterEncoding.Code128
  • Code 39 —— 較舊的標準; 廣泛支持; 編碼字母數字字元。 使用BarcodeWriterEncoding.Code39
  • QR Code —— 2D條碼; 儲存大量資料,包括URL。 使用BarcodeWriterEncoding.QRCode
  • Data Matrix —— 緊湊的2D條碼; 在製造和物流中常見。 使用BarcodeWriterEncoding.DataMatrix
  • PDF417 —— 高容量2D條碼; 用於運輸標籤和ID。 使用BarcodeWriterEncoding.PDF417

請參閱完整的支持條碼格式列表,以選擇適合您的使用場景的編碼。 要從Crystal Report輸出圖像中讀取條碼,請參閱條碼閱讀教程

您的下一步是什麼?

在使用VB.NET和IronBarcode實現條碼於Crystal Reports中提供了一個可靠的報告增強解決方案。 這種基於SDK的方法消除了字體依賴性,同時提供了廣泛的自定義選項。 無論是使用資料庫還是DataTable,您都可以實現在您的Crystal Reports應用程式中使用包括Data Matrix、Code 39和QR碼在內的專業條碼。

要繼續構建您的條碼工作流程:

今天就開始在您的專案中實施條碼,透過探索IronBarcode的免費試用來探索其為Crystal Reports上使用VB.NET列印條碼的完整功能。 SAP Crystal Reports SDK記錄在SAP開發者入口以獲取額外的整合模式。

常見問題

使用IronBarcode在Crystal Reports與VB.NET中有何好處?

在Crystal Reports中使用IronBarcode和VB.NET允許您無縫整合條碼,為庫存跟蹤和生成詳細報告等任務增強業務流程。相比於基於字體的解決方案,它簡化了條碼生成。

IronBarcode如何改進Crystal Reports中的條碼生成?

IronBarcode通過提供簡單明了的SDK改善條碼生成,消除了對複雜公式的需求並減少了在Crystal Reports中與格式有限和列印機相容性相關的問題。

IronBarcode能夠在Crystal Reports中處理不同的條碼格式嗎?

是的,IronBarcode支持多種條碼格式,使其適用於Crystal Reports中的各種應用,確保相容性和可靠性。

將IronBarcode整合到VB.NET專案中是否困難?

不,將IronBarcode整合到VB.NET專案中被設計為方便使用者使用,擁有清晰的文件和支援,讓該過程順暢而高效。

為什麼應選擇IronBarcode而非基於字體的條碼解決方案?

IronBarcode提供比基於字體的條碼更強大和靈活的解決方案,避免了複雜公式和列印機問題,並提供廣泛的格式支持和可靠性。

IronBarcode是否支持直接從Crystal Reports列印條碼?

是的,IronBarcode允許從Crystal Reports直接列印條碼,確保高品質的輸出並降低與其他方法相關的複雜性。

在VB.NET中,基於字體的條碼有哪些常見問題?

基於字體的條碼通常涉及複雜的公式、格式選項有限和列印機相容性問題,而IronBarcode借助其全面的SDK有助於克服這些問題。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話