Document Solutions for Excel, Java Edition | Document Solutions
Features / Barcodes
In This Topic
    Barcodes
    In This Topic

    DsExcel provides API to add barcodes in worksheets. These are very helpful in scanning information easily and quickly with utmost precision. They also facilitate users to take informed business decisions and improve data analysis.

    The following types of barcodes are supported in DsExcel Java:

    Since Excel does not support barcode formulas, DsExcel provides convertBarcodeToPicture method in IWorkbook interface and Workbook class that enables a user to convert the calculated barcodes to pictures in all worksheets and save the file as an XLSX file. convertBarcodeToPicture method also allows the user to specify the image type using convertBarcodeToPicture(ImageType imageType = ImageType.JPG) overload. ImageType enumeration of com.grapecity.documents.excel.drawing namespace sets the image type. The default image type is SVG if the image type parameter is not specified. The conversion does not support EMF and WMF image types, and the method will throw an unsupported exception.

    Refer to the following example code to convert the resulting barcodes to pictures:

    Java
    Copy Code
    // Initialize Workbook.
    Workbook workbook = new Workbook();
            
    // Access first worksheet.
    IWorksheet worksheet = workbook.getWorksheets().get(0);
            
    // Set worksheet layout and add data.
    worksheet.getRange("A:A").setColumnWidth(2);
    worksheet.getRange("B:C").setColumnWidth(15);
    worksheet.getRange("D:F").setColumnWidth(25);
    worksheet.getRange("4:13").setRowHeight(57);
    worksheet.getRange("B3").setValue("Type");
    worksheet.getRange("C3").setValue("Data");
    worksheet.getRange("B2").setValue("Barcode");
    worksheet.getRange("B2:F2").setMergeCells(true);
    worksheet.getRange("D3:F3").setValue(new Object[][]
            {
                    {"Default", "Change showLabel", "Change labelPosition"}
            });
    worksheet.getPageSetup().setPrintTitleColumns("$A:$C");
    worksheet.getRange("B4:C13").setHorizontalAlignment(HorizontalAlignment.Center);
    worksheet.getRange("B4:C13").setVerticalAlignment(VerticalAlignment.Center);
    worksheet.getRange("B2:F3").setHorizontalAlignment(HorizontalAlignment.Center);
    worksheet.getRange("B2:F3").setVerticalAlignment(VerticalAlignment.Center);
    worksheet.getRange("B4:C13").setValue(new Object[][]
            {
                    {"QR code", "Policy:411"},
                    {"Data Matrix", "Policy:411"},
                    {"PDF417", "6935205311092"},
                    {"EAN-8", "4137962"},
                    {"EAN-13", "6920312296219"},
                    {"Code39", "3934712708295"},
                    {"Code93", "6945091701532"},
                    {"Code128", "465465145645"},
                    {"Codabar", "9787560044231"},
                    {"gs1128", "010123456789012815051231"}
            });
    String[] types = {"BC_QRCODE", "BC_DataMatrix", "BC_PDF417", "BC_EAN8", "BC_EAN13", "BC_CODE39", "BC_CODE93", "BC_CODE128", "BC_CODABAR", "BC_GS1_128"};
    worksheet.getPageSetup().setPrintGridlines(true);
    
    // Add barcodes using barcode formula.
    for (int i = 0; i < types.length; i++)
    {
        String columnD = "D" + (i + 4);
        worksheet.getRange(columnD).setFormula("=" + types[i] + "(C" + (i + 4) + ")");
    }
    
    for (int i = 3; i < types.length; i++)
    {
        String columnE = "E" + (i + 4);
        String columnF = "F" + (i + 4);
        worksheet.getRange(columnE).setFormula("=" + types[i] + "(C" + (i + 4) + ",,,0)");
        worksheet.getRange(columnF).setFormula("=" + types[i] + "(C" + (i + 4) + ",,,,\"top\")");
    }
    
    // Convert resulting barcodes to pictures and set the image type to JPG.
    workbook.convertBarcodeToPicture(ImageType.JPG);
        
    // Save the workbook.
    workbook.save("ConvertBarcodetoPicture.xlsx");

    You can also perform SpreadJS JSON I/O of barcodes. For more information, refer to Import and Export SpreadJS Files. Similarly, Barcodes can be exported to image, html and PDF documents. For more information about PDF exporting, refer to Export Barcodes.

    Limitations

    DsExcel does not support setting the color or background color of the barcodes.