Barcode in VB.NET and Excel - Data Matrix | QR Code | CODE 128

Contoh cara membuat lembar kerja Excel baru dari program VB.NET dan menempatkan barcode 1D (CODE 128, EAN-13, ITF-14) atau barcode 2D (QR Code, Data Matrix, PDF417) pada lembar kerja yang.

Panduan ini diuji dengan Excel 2010 dan Excel 2007. Excel versi sebelumnya mungkin memerlukan referensi komponen yang berbeda dan ruang nama yang berbeda yang akan ditambahkan ke dalam proyek VB.NET. Informasi tambahan tentang otomatisasi Excel dari VB.NET dapat ditemukan di KB 301982 dan 302094 KB.
Contoh kode (lihat di bawah) menggunakan StrokeScribe ActiveX untuk membuat barcode. Silahkan install StrokeScribe sebelum mencoba sampel ini.

1. Open an existing (or create a new) VB.NET project in which you want to create barcodes. In our example, we will create a new console VB.NET application in Visual Studio 2010 as shown on the picture:

2. Add a reference to Excel Object Library into your project. From the Visual Studio menu, execute Project->Add Reference:

3. In the COM tab, select Microsoft Excel 12.0 Object Library for Office 2007 or Microsoft Excel 14.0 Object Library for Office 2010:

4. Execute the Project->Add Reference menu command again and add a reference to StrokeScribe ActiveX:

5. Copy this sample code into the VB.NET project:
Imports Microsoft.Office.Interop
Imports STROKESCRIBELib
Module Module1
    Sub Main()
        Dim oXL As Excel.Application
        Dim oWB As Excel.Workbook
        Dim oSheet As Excel.Worksheet
        oXL = CreateObject("Excel.Application")
        oXL.Visible = True
        oWB = oXL.Workbooks.Add
        oSheet = oWB.ActiveSheet
        Dim x = oXL.InchesToPoints(1)
        Dim y = oXL.InchesToPoints(1)
        Dim w = oXL.InchesToPoints(3)
        Dim h = oXL.InchesToPoints(2)
        Dim sh As Excel.Shape
        sh = oSheet.Shapes.AddOLEObject("STROKESCRIBE.StrokeScribeCtrl.1", , , , , , , _
                        x, y, w, h)
     
        Dim ss As StrokeScribe
        ss = sh.OLEFormat.Object.Object
        ss.Alphabet = enumAlphabet.CODE128
        ss.Text = "1234ABCdef"
        If ss.Error Then MsgBox(ss.ErrorDescription)
    End Sub
End Module 
6. Build and run your project.


To create a QR Code in Excel, set Alphabet = enumAlphabet.QRCODE.
To manually control the QR Code error correction, change the value of the QrECL property to L, Q, or H. Setting QrECL=H will restore up to 30% of stored data in case of barcode corruption, but the error correction block size will take up to 60 percent of barcode capacity. Setting QrECL=L will free more space for user data, but only 7% of data will be restored if the barcode is corrupted.
QR Code generated by a VB.NET application in Excel
QrMinVersion forces the encoder to use the specified and higher matrix versions to encode user-supplied data. The sample picture on the right shows the QR Code with version 10 matrix.
The ECI property indicates the code page of text encoded in the barcode. Use this property only if the receiving party's hardware and software are ECI-compatible.
When UTF8=true, non-ASCII characters are encoded in UTF-8. Set this property when a non-ASCII content needs to be read on smartphones.
Because QR Code is always square, you need to adjust the height of shape object which contains the barcode. Otherwise, the barcode picture will have whitespace at the left and at the right.
Dim w = oXL.InchesToPoints(3)
Dim h = oXL.InchesToPoints(3)
And make these changes in VB.NET code to produce a QR Code :
ss.Alphabet = enumAlphabet.QRCODE
ss.QrECL = enumQR_ECL.M 'M = default error correction level of QR Code
ss.QrMinVersion = 1 'Increase, if you want a large QR Code.
ss.ECI = 0
ss.UTF8 = false 'set to true if you need to encode national characters for smartphones
ss.Text = "1234ABCdef" 

Sumber :
Strokescribe

0 Response to "Barcode in VB.NET and Excel - Data Matrix | QR Code | CODE 128"

Posting Komentar