[Spreadsheet] Add Barcode in Spreadsheet

1 Answer 81 Views
Barcode Spreadsheet
WEI TZE
Top achievements
Rank 2
Iron
Iron
WEI TZE asked on 04 May 2023, 09:57 AM | edited on 04 May 2023, 10:15 AM

Background:

  • Given a spreadsheet template, to generate a new spreadsheet by filling the information and barcode.
  • E.g. the screenshot (template) below, the barcode to be inserted at the cell location.

What I have done:

  • I manage to locate the target cell index (by searching the keyword $PART_NAME_LABEL$).

Question:

  • How to insert the barcode (image) at the target cell index location?
  • Any other suggestions for barcode generation? (Instead of inserting the barcode image, e.g. using barcode font)

I have referred to the sample in this page but it doesn't work.

https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/shapes-and-images

 

XlsxFormatProvider formatProvider = new XlsxFormatProvider();
            Workbook workbook = formatProvider.Import(File.ReadAllBytes(@"C:\temp\temp.xlsx"));

            var worksheet = workbook.Sheets[0] as Worksheet;

            FindOptions f = new FindOptions();
            f.FindWhat = "$PART_NAME_LABEL$";

            IEnumerable<FindResult> result = worksheet.FindAll(f);

            foreach(var i in result)
            {
                worksheet.Cells[i.FoundCell.CellIndex].SetValue("");
                //Add barcode here
                RadBarcode radBarcode1 = new RadBarcode();
                Telerik.WinControls.UI.Barcode.Symbology.Code39Extended code39Extended1 = new Telerik.WinControls.UI.Barcode.Symbology.Code39Extended();
                radBarcode1.Symbology = code39Extended1;
                radBarcode1.Value = "123456";
                radBarcode1.LoadElementTree();

                var ms = new MemoryStream();
                Image barcode = radBarcode1.ExportToImage();
                barcode.Save(ms, ImageFormat.Png);

                FloatingImage image = new FloatingImage(worksheet, i.FoundCell.CellIndex, 35, 10);
                image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(ms, "png");
            }

 

 

 

1 Answer, 1 is accepted

Sort by
1
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 May 2023, 01:19 PM

Hi, WEI TZE,

Indeed, inserting the barcode as an image is the appropriate way to add it to a spreadsheet cell. I have prepared a sample code snippet demonstrating how to add a barcode image to a cell in RadSpreadsheet: 

        public Form1()
        {
            InitializeComponent();

            RadBarcodeView radBarcode1 = new RadBarcodeView();
            Code39Extended code93Extended1 = new Code39Extended();
            radBarcode1.Symbology = code93Extended1;
            code93Extended1.AutoChecksum = true;
            code93Extended1.LineAlign = System.Drawing.StringAlignment.Far;
            code93Extended1.Module = 1;
            code93Extended1.ShowText = false;
            code93Extended1.TextAlign = System.Drawing.StringAlignment.Center;
            radBarcode1.Value = "123456";
            radBarcode1.LoadElementTree();

            var ms = new MemoryStream();
            Image barcode = radBarcode1.ExportToImage();
            barcode.Save(ms, ImageFormat.Png);
            Telerik.Windows.Documents.Spreadsheet.Model.Worksheet worksheet = this.radSpreadsheet1.Workbook.ActiveWorksheet;
            FloatingImage image = new Telerik.Windows.Documents.Spreadsheet.Model.Shapes.FloatingImage(worksheet,
                new Telerik.Windows.Documents.Spreadsheet.Model.CellIndex(7, 1), 35, 10);

            image.Width = 330;
            image.Height = 80;
            image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(ms, "png");

            worksheet.Images.Add(image);
        }

Please, do not forget to insert the image into the collection of shapes of the worksheet. Note that the worksheet needs to be the same as the one passed in the FloatingImage constructor, otherwise an exception is thrown.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

WEI TZE
Top achievements
Rank 2
Iron
Iron
commented on 08 May 2023, 01:47 PM

 

Thanks Dess. But I am having issue with:

            worksheet.Images.Add(image);

Dess | Tech Support Engineer, Principal
Telerik team
commented on 08 May 2023, 01:50 PM

WEI TZE, 

Please make sure that you are using the latest version of the Telerik UI for WinForms suite (R1 2023 SP1) since the code in the referred help article is valid with the latest version.

WEI TZE
Top achievements
Rank 2
Iron
Iron
commented on 08 May 2023, 01:54 PM

Thanks Dess
Tags
Barcode Spreadsheet
Asked by
WEI TZE
Top achievements
Rank 2
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or