Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I 'm pretty new to c# and try to look for a solution to create a little first app with excel.

What I try to do is to make a order list and later I want to export to PDF.

Step 1 scan a barcode and post to first column in data grid
Step 2 search the barcode in excel and post back the description into the datagrid column 3
Step 3 enter the Qty en jump back to txt box 1



C#
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void orderArtikelNummerTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {                
//Move to Qty
                orderQtyTextBox.Focus();
                e.Handled = true;
                orderQtyTextBox.Select(0, orderQtyTextBox.Text.Length);

            }
        }

        private void orderQtyTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
// check excell for valeu and post to column 3 !!!TODO!!!
//Post textboxes in grid and create new line
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(this.orderDataGridView, productNameTextBox.Text, orderQtyTextBox.Text);
                this.orderDataGridView.Rows.Add(row);
                
//Move to Artikelnummer
                productNameTextBox.Focus();
                e.Handled = true;
                productNameTextBox.Select(0, productNameTextBox.Text.Length);
            }
        }



example of excel:

(A1)12345 (B1) some description of the product

Any suggestions?
I search 't google codeproject etc.. but can 't find any readable solution

thnks
Posted
Updated 21-Jan-13 11:19am
v2

1 solution

private Microsoft.Office.Interop.Excel.Range GetSpecifiedRange(string matchStr, Microsoft.Office.Interop.Excel.Worksheet objWs)
        {
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Range currentFind = null;
            //Microsoft.Office.Interop.Excel.Range firstFind = null;
            currentFind = objWs.get_Range("A1", "AM100").Find(matchStr, missing,
                           Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues,
                           Microsoft.Office.Interop.Excel.XlLookAt.xlPart,
                           Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
                           Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, missing, missing);
            return currentFind;
        }
 
        private void orderArtikelNummerTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                //post data into 
               
               //Move to Qty
                orderQtyTextBox.Focus();
                e.Handled = true;
                orderQtyTextBox.Select(0, orderQtyTextBox.Text.Length);
            }
        }
        private void orderQtyTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                string File_name = "E:\\catalog.xls";
                Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook oWB;
                Microsoft.Office.Interop.Excel.Worksheet oSheet;
                try
                {
                    //Move to Artikelnummer
                    orderArtikelNummerTextBox.Focus();
                    e.Handled = true;
                    orderArtikelNummerTextBox.Select(0, orderArtikelNummerTextBox.Text.Length);
                  
                    //start search excell
                    object missing = System.Reflection.Missing.Value;
                    oWB = oXL.Workbooks.Open(File_name, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing);
                    oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[1];
                    Microsoft.Office.Interop.Excel.Range oRng = GetSpecifiedRange(orderArtikelNummerTextBox.SelectedText, oSheet);
                    if (oRng != null)
                    {
                        MessageBox.Show("Text found, position is Row:" + oRng.Row + " and column:" + oRng.Column);
                        
                        DataGridViewRow row = new DataGridViewRow();
                        row.CreateCells(this.orderDataGridView, orderArtikelNummerTextBox.Text, orderQtyTextBox.Text);
                        this.orderDataGridView.Rows.Add(row); 
                        
                        
                        //orderDataGridView.Rows[0].Cells[0].Value = orderArtikelNummerTextBox.Text;
                        //orderDataGridView.Rows[+1].Cells[0 + 2].Value = orderQtyTextBox.Text;
                                            }
                    else
                    {
                        MessageBox.Show("Text is not found"); 
                    }
                    oWB.Close(false, missing, missing);
                    oSheet = null;
                    oWB = null;
                    oXL.Quit();
                }
                catch (Exception ex)
                {
                    {
                        MessageBox.Show(" Exception caught." + ex);
                    }
                }
            }
        }


Okay found how to read excel and get row and column number

Any suggestion how to count with the column number, Lets say +1 and than ask to excell the new counted column?

kind regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900