如何使用VB.NET在Crystal Reports中列印條碼
在 Crystal Reports 中新增條碼可以增強您的業務流程,使其更快、更準確,從追蹤庫存到產生詳細報告。 如果您曾經嘗試在 VB.NET 中使用基於字體的條碼,您就會知道,由於複雜的公式、有限的格式和印表機問題,使用起來有多麼棘手,令人沮喪。
幸運的是,像IronBarcode這樣的現代條碼產生器 SDK 使得建立和列印條碼變得非常簡單。 在本指南中,我們將帶您完成所有步驟:在 Visual Studio 中設定 IronBarcode、產生條碼以及直接在 Crystal Reports 中顯示它們。 到最後,你將能夠輕鬆製作出專業、可靠的條碼。
Crystal Reports中列印條碼的方法有哪些?
在 Crystal Reports 新增條碼主要有兩種方法:基於字型的方法和 SDK 解決方案。
基於字型的方法需要在字型資料夾中安裝特殊的條碼字型(True Type 字型),並使用公式或使用者函數庫 (UFL) 對資料進行編碼。 雖然這種方法有效,但它通常涉及複雜的編碼公式、有限的條碼格式支援(例如 Code 39 或資料矩陣),以及在不同印表機和系統上可能出現的字體渲染問題,正如微軟關於字體渲染的文檔中所討論的那樣。
基於 SDK 的解決方案提供了更強大的替代方案。這些函式庫以程式設計方式產生條碼影像,具有更高的可靠性、更廣泛的格式支援和更簡便的實現方式。 IronBarcode正是這種現代方法的典範,它允許開發人員產生條碼影像,並透過標準資料庫欄位或 DataTable 物件與 Crystal Reports 無縫整合。 此方法可確保在所有平台上呈現一致的效果,並消除使用 VB.NET 在 Crystal Report 中列印條碼時出現的字體依賴性問題。
如何在VB.NET設定IronBarcode?
在 VB.NET 專案中設定 IronBarcode 只需要極少的配置。 首先,透過 NuGet 套件管理器安裝庫,執行以下命令:
Install-Package BarCode
或者,您可以使用 Visual Studio 中的套件管理器 UI 搜尋並安裝"IronBarcode"。 安裝完成後,在VB.NET程式碼中匯入命名空間:
Imports IronBarCodeImports IronBarCode請確保您的專案是為 .NET Framework 4.0 或更高版本,或任何版本的 .NET Core/.NET 5+。 IronBarcode 可與所有支援影像欄位的 Crystal Reports 版本搭配使用,因此與現有專案具有廣泛的相容性。 有關詳細的設定說明,請參閱 IronBarcode 文件。
立即下載 IronBarcode,開始在 Crystal Reports 中建立專業條碼。
如何產生條碼並將其儲存在資料庫中?
使用 IronBarcode 建立條碼並將其儲存到 Crystal Reports 中,需要產生條碼影像,並使用新的 OleDbConnection 或 SqlConnection 將其儲存到資料庫中。 以下是一個完整的範例:
方案一:使用資料庫
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 and adjust font size
myBarcode.ResizeTo(400, 150)
' Connect to your database (example uses connection string)
Using connection As New SqlConnection("YourConnectionString")
' SQL command to post barcode data
Dim cmd As New SqlCommand("INSERT INTO Products (ProductCode, BarcodeImage) VALUES (@Code, @Image)", connection)
' Add parameters
cmd.Parameters.AddWithValue("@Code", "CUSTOMER-12345")
cmd.Parameters.AddWithValue("@Image", myBarcode.BinaryStream)
' Execute the command
connection.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
End ModuleImports 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 and adjust font size
myBarcode.ResizeTo(400, 150)
' Connect to your database (example uses connection string)
Using connection As New SqlConnection("YourConnectionString")
' SQL command to post barcode data
Dim cmd As New SqlCommand("INSERT INTO Products (ProductCode, BarcodeImage) VALUES (@Code, @Image)", connection)
' Add parameters
cmd.Parameters.AddWithValue("@Code", "CUSTOMER-12345")
cmd.Parameters.AddWithValue("@Image", myBarcode.BinaryStream)
' Execute the command
connection.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
End Module使用 SqlConnection(或新的 OleDbConnection)建立新連線連接到資料庫,並發布條碼資料。
選項 2:使用資料表
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()))
' Add table row for each product
Dim row As DataRow = dt.NewRow()
row("ProductCode") = "PROD-12345"
' Create barcode on-the-fly and convert to binary
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 FunctionImports 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()))
' Add table row for each product
Dim row As DataRow = dt.NewRow()
row("ProductCode") = "PROD-12345"
' Create barcode on-the-fly and convert to binary
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此代碼產生值為"PROD-12345"的Code 128 條碼。 AddBarcodeValueTextBelowBarcode() 方法會在條碼下方加入易於閱讀的文字。 ResizeTo() 方法會調整尺寸以適應報表版面。 如需了解更多條碼格式,請瀏覽支援的條碼類型。
如何在 Crystal Reports 中顯示條碼?
條碼儲存到資料庫後,在 Crystal Reports 中顯示它們遵循標準影像欄位程式:
- 開啟 Crystal Reports,建立一個新報表或開啟一個現有報表。
在欄位資源管理器中,以滑鼠右鍵按一下"資料庫欄位",然後選擇"資料庫專家"。
- 連接到資料庫並選擇包含條碼圖像的表。
- 將條碼影像欄位拖曳到報表設計介面上。
- 根據需要調整欄位大小和位置
您的報表設計器應該與此類似:
如何在 VB.NET 的 Crystal Reports 中列印條碼:圖 2 - 報表設計器佈局
若要在報表執行時動態產生條碼,請修改您的資料表或資料庫查詢,以便即時產生條碼:
Function GetReportData() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("ProductCode", GetType(String))
dt.Columns.Add("Barcode", GetType(Byte()))
' Add table row
Dim row As DataRow = dt.NewRow()
row("ProductCode") = "CUSTOMER-12345"
' Create barcode on-the-fly and convert to binary
Dim barcode = BarcodeWriter.CreateBarcode("CUSTOMER-12345", BarcodeWriterEncoding.Code128)
row("Barcode") = barcode.BinaryStream
dt.Rows.Add(row)
Return dt
End FunctionFunction GetReportData() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("ProductCode", GetType(String))
dt.Columns.Add("Barcode", GetType(Byte()))
' Add table row
Dim row As DataRow = dt.NewRow()
row("ProductCode") = "CUSTOMER-12345"
' Create barcode on-the-fly and convert to binary
Dim barcode = BarcodeWriter.CreateBarcode("CUSTOMER-12345", BarcodeWriterEncoding.Code128)
row("Barcode") = barcode.BinaryStream
dt.Rows.Add(row)
Return dt
End Function這種方法在報表載入時動態產生條碼,無需預先將影像儲存在資料庫中。 您也可以使用 Crystal Reports 的匯出功能將報表轉換為 PDF 格式。 有關 Crystal Reports 資料來源的更多信息,請參閱SAP 的 Crystal Reports 文件。
輸出
您的 Crystal Report 報表應以清晰易讀的文字和適當的字型大小顯示條碼。根據掃描器、印表機或標籤的需要,調整字型、欄位大小或邊距。 現在可以直接從 VB 程式/專案中將條碼新增至 PDF、列印表格或範本中。
常見問題及解決方法有哪些?
在使用VB.NET在Crystal Reports中實作條碼時,可能會出現以下幾個問題:
*條碼模糊:*使用 myBarcode.ResizeTo() 增大尺寸以提高解析度。 掃描問題:確保條碼周圍有足夠的空白,或在 Code 39 條碼上加上星號標記。 資料庫連線問題:檢查您的新 OleDbConnection 或新 OleDbDataAdapter 參數。 確保資料集綁定正確。 資料類型問題:始終使用 image 或 varbinary(max) 欄位來儲存條碼二進位資料。 條碼不顯示:請驗證 Crystal Reports 檢視器是否具有存取資料庫、資料表或檔案資源的適當權限。 系統問題:**確保所有必要的 DLL 都已安裝,並且您的專案引用了正確的 IronBarcode DLL。 *字型或印表機問題:確認 TrueType 字型已安裝在字型資料夾中,並已針對您的印表機進行設定。
如需更多故障排除支持,請造訪IronBarcode 故障排除指南。
結論
使用 VB.NET 和 IronBarcode 在 Crystal Reports 中實作條碼,可為報表增強功能提供可靠且直接的解決方案。 這種基於 SDK 的方法消除了對字體的依賴,同時提供了廣泛的自訂選項。 無論使用資料庫或資料表,您都可以在 Crystal Reports 應用程式中實作專業的條碼,包括資料矩陣、Code 39 和其他格式。
立即下載IronBarcode 的免費試用版,開始在您的專案中實施條碼,探索其使用 VB.NET 在 Crystal Reports 中列印條碼的全部功能。
常見問題解答
在 VB.NET 的 Crystal Reports 中使用 IronBarcode 有哪些好處?
在 VB.NET 的 Crystal Reports 中使用 IronBarcode,可無縫整合條碼,從而增強業務流程,例如追蹤庫存和產生詳細報表。與基於字體的解決方案相比,它簡化了條碼的生成過程。
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 幫助克服了這些問題。






