Document Solutions for Excel, .NET 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:

    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 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:

    C#
    Copy Code
    // Initialize Workbook.
    var workbook = new Workbook();
    
    // Access first worksheet.
    IWorksheet worksheet = workbook.Worksheets[0];
    
    // Set worksheet layout and add data.
    worksheet.Range["A:A"].ColumnWidth = 2;
    worksheet.Range["B:C"].ColumnWidth = 15;
    worksheet.Range["D:G"].ColumnWidth = 25;
    worksheet.Range["4:14"].RowHeight = 57;
    worksheet.Range["B3"].Value = "Type";
    worksheet.Range["C3"].Value = "Data";
    worksheet.Range["B2"].Value = "Barcode";
    worksheet.Range["B2:G2"].Merge(true);
    worksheet.Range["D3:G3"].Value = new object[,]{
        {"Default", "Change color", "Change showLable", "Change lablePosition"}
    };
    worksheet.PageSetup.PrintTitleColumns = "$A:$C";
    worksheet.Range["B4:C14"].HorizontalAlignment = HorizontalAlignment.Center;
    worksheet.Range["B4:C14"].VerticalAlignment = VerticalAlignment.Center;
    worksheet.Range["B2:G3"].HorizontalAlignment = HorizontalAlignment.Center;
    worksheet.Range["B2:G3"].VerticalAlignment = VerticalAlignment.Center;
    worksheet.Range["B4:C14"].Value = new object[,]
    {
        {"QR code", "Policy:411"},
        {"Data Matrix", "Policy:411"},
        {"PDF417", 6935205311092},
        {"EAN-8", 4137962},
        {"EAN-13", 6920312296219},
        {"Code39", 3934712708295},
        {"Code93", 6945091701532},
        {"Code49", 6901668002433},
        {"Code128", 465465145645},
        {"Codabar", 9787560044231},
        {"gs1128", 235465143135}
    };
    string[] types = {
        "BC_QRCODE",
        "BC_DataMatrix",
        "BC_PDF417",
        "BC_EAN8",
        "BC_EAN13",
        "BC_CODE39",
        "BC_CODE93",
        "BC_CODE49",
        "BC_CODE128",
        "BC_CODABAR",
        "BC_GS1_128"
    };
    
    // Add barcodes using barcode formula.
    for (var i = 0; i < types.Length; i++)
    {
        string columnD = "D" + (i + 4);
        string columnE = "E" + (i + 4);
        worksheet.Range[columnD].Formula = "=" + types[i] + "(C" + (i + 4) + ")";
        worksheet.Range[columnE].Formula = "=" + types[i] + "(C" + (i + 4) + ",\"#fff\",\"#000\")";
    }
    
    for (var i = 3; i < types.Length; i++)
    {
        string columnD = "F" + (i + 4);
        string columnE = "G" + (i + 4);
        worksheet.Range[columnD].Formula = "=" + types[i] + "(C" + (i + 4) + ",,,0)";
        worksheet.Range[columnE].Formula = "=" + types[i] + "(C" + (i + 4) + ",,,,\"top\")";
    }
    
    // Convert resulting barcodes to pictures and set the image type to JPG.
    workbook.ConvertBarcodeToPicture(GrapeCity.Documents.Excel.Drawing.ImageType.JPG);
    
    // Save the workbook.
    workbook.Save("ConvertBarcodeToPicture.xlsx");

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