从数据网格中的excell和post B列读取列A. [英] read column A from excell and post column B in datagrid

查看:53
本文介绍了从数据网格中的excell和post B列读取列A.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对c#很陌生,并尝试寻找一个解决方案,用excel创建一个小小的第一个应用。



我尝试做的是订购清单后来我要导出为PDF。



步骤1扫描条形码并发布到数据网格中的第一列

第2步搜索excel中的条形码并将描述发回到datagrid列3

步骤3输入数量并跳回txt框1







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



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);
            }
        }







excel示例:



(A1)12345(B1)该产品的一些描述



有什么建议吗?

我搜索谷歌代码项目等..但无法找到任何可读的解决方案



thnks




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

推荐答案

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);
                    }
                }
            }
        }





好​​的找到了如何阅读excel和获取行号和列号



任何建议如何计算列号,让我们说+1并且要求对新计数列进行计数?



亲切的问候



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


这篇关于从数据网格中的excell和post B列读取列A.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆