0

I have a problem I currently cannot insert an image created by Zen Barcode framework in PictureBox into Excel.
So far I only manage to save it as a jpeg file but I want to insert the barcode and the QR code generated into Microsoft Excel when I click the button convert to Excel.
The only issue I facing now is that after the barcode and the QR code generated but I cannot get it exported out to Microsoft Excel but I can get what is being keyed into the 'textbox' to excel.

This is my application so far

public partial class Form1: Form
{
    public object SaveFileDialog1 { get; private set; }

    // Bitmap qrCodeImage;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
        pictureBox.Image = barcode.Draw(Barcodetxt.Text, 50);
    }

    private void btnQR_Click(object sender, EventArgs e)
    {
        Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr;
        pictureBox1.Image = qrcode.Draw(QRCodetxt.Text, 50);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnexcel_Click(object sender, EventArgs e)
    {
        string file = "D:/newdoc.xls";
        Workbook workbook = new Workbook();
        Worksheet worksheet = new Worksheet("Label Pritting ");
        worksheet.Cells[1, 3] = new Cell(Barcodetxt.Text);
        worksheet.Cells[2, 5] = new Cell(QRCodetxt.Text);
        Bitmap bm = default(Bitmap);

        bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);

        System.Windows.Forms.Clipboard.SetDataObject(bm, false);

        //worksheet.Cells[3, 8] = new Cell(pictureBox.Image);
        //worksheet.Cells[1, 2] = new Cell(pictureBox.Image);
        // worksheet.Cells[1, 3] = new Cell(pictureBox1.Image);
        workbook.Worksheets.Add(worksheet); workbook.Save(file);

        // open xls file
        Workbook book = Workbook.Load(file);
        Worksheet sheet = book.Worksheets[0];

        // traverse cells
        //foreach (Pair, Cell > cell in sheet.Cells)
        {
            //  dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
        }

        // traverse rows by Index
        for (int rowIndex = sheet.Cells.FirstRowIndex; rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
        {
            Row row = sheet.Cells.GetRow(rowIndex);
            for (int colIndex = row.FirstColIndex; colIndex <= row.LastColIndex; colIndex++)
            {
                Cell cell = row.GetCell(colIndex);
            }
        }
    }

    private void pictureBox_Click(object sender, EventArgs e)
    {

    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }

    private void Saveqrcodebtn_Click(object sender, EventArgs e)
    {
        string saveFileName;
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.DefaultExt = "jpg";
        saveDialog.Filter = "jpeg|*.jpg";
        // saveDialog.FileName = saveFileName;
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        if (saveDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                saveFileName = saveDialog.FileName;
                pictureBox.Image.Save(saveFileName);
                pictureBox1.Image.Save(saveFileName);

                MessageBox.Show(saveFileName + " Save Complete", "Complete", MessageBoxButtons.OK);
            }
            catch
            {
                MessageBox.Show("Error file is open/in use");
            }
            pictureBox1.Image = null;
        }
    }

    private void Barcodebtn_Click(object sender, EventArgs e)
    {
        string saveFileName;
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.DefaultExt = "jpg";
        saveDialog.Filter = "jpeg|*.jpg";
        // saveDialog.FileName = saveFileName;
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        if (saveDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                saveFileName = saveDialog.FileName;
                pictureBox.Image.Save(saveFileName);
                pictureBox1.Image.Save(saveFileName);

                MessageBox.Show(saveFileName + " Save Complete", "Complete", MessageBoxButtons.OK);
            }
            catch
            {
                MessageBox.Show("Error file is open/in use");
            }
            pictureBox1.Image = null;
        }
    }
}
w.c
  • 13
  • 8
  • Which part is the problem: combining bar code into an image or insertng some image into excel? – TaW May 08 '18 at 07:55
  • The problem is that i cannot seem to get the barcode and the QR code generated in the picture box exported out to excel – w.c May 10 '18 at 00:52
  • Hm, I woder why you save the images to the same name or even save them at all. But the real question is : Where do you even try to insert them to excel?? Or why you put an empty image into the clipboard? - For an example [see here](https://stackoverflow.com/questions/37606187/add-picture-in-excel-on-particular-cell-with-c-sharp) – TaW May 10 '18 at 04:08

0 Answers0