Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody.
I'd like to copy the value from the cell that lies next to my wanted cell.I would then like to write my copied value in a text document.

My individual steps:
1.Open Excel file
2.Find the cells
3.Create new text file
4.Write my value from xlxs file


Does anyone have any advice?
Thank you all for any help

25.11. I've improved it a bit but it's still not it
I now have these errors:


Error CS0161 'Class1.ReadCe­ll(int, int)': not all code paths return a value

What I have tried:

Form1:
C#
using System;
using System.IO;
using System.Windows.Forms;


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

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFile();
        }

        public void OpenFile()
        {
            Class1 excel = new Class1(textBox1.Text, 1);
            string path = textBox1.Text + ".txt";
            TextWriter tw = new StreamWriter(path, true);

            tw.Write(excel.ReadCell(0, 0));
            tw.Close();
        }
    }
}


class1:
C#
<pre>using Microsoft.Office.Interop.Excel;
using _Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApp2
{
    class Class1
    {
        string path = "";
        _Application excel = new _Excel.Application();
        Workbook wb;
        Worksheet ws;
        private object excelWorksheet;

        public Class1(string path, int Sheet)
        {
            this.path = path;
            wb = excel.Workbooks.Open(path);
            ws = (Worksheet)wb.Worksheets[Sheet];
        }
        public string ReadCell(int i, int j)
        {
            for ( i = 0; i < 26; i++)
            {
                for ( j = 1; j < 100; j++)
                {
                    int first = 65 + i;
                    string coordinates = ((char)first).ToString() + j;

                    if (ws.Range[coordinates].Value == "Z_KomSilnice_L (24200)/7 ")
                    {
                        return ws.Range[coordinates].Value;
                    }
                    else
                        return "";
                }
            }
        }
     }
Posted
Updated 24-Nov-20 23:19pm
v4
Comments
Richard MacCutchan 2-Nov-20 9:32am    
Step through your code with the debugger and see exactly what items are being found.
dejf111 3-Nov-20 4:20am    
But I'd like public Excel to throw a bug
Richard MacCutchan 3-Nov-20 9:02am    
Why not just do what I suggested and actually see what happens as each instruction executes? It will reveal so much more information, and you will learn more than one useful thing as you do it.
dejf111 4-Nov-20 3:30am    
So I tried and I got into trouble that the program couldn't find the file
Richard MacCutchan 5-Nov-20 3:36am    
Well that is easy to fix.

I have copied your code and with a couple of minor changes it works:
C#
using System;
using System.IO;
using System.Windows.Forms;


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

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFile();
        }

        public void OpenFile()
        {
            Class1 excel = new Class1(@"C:\Users\info\Documents\Zkouska.xlsx", 1);
            string path = @"C:\Users\info\Documents\new.txt";
            TextWriter tw = new StreamWriter(path, true);
            String text = excel.ReadCell(0, 0); // capture the returned value
            tw.Write(text);
            tw.Close();  // close the output stream
        }
    }
}


And the Class1.cs file
C#
using Microsoft.Office.Interop.Excel;
using _Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApp2
{
    class Class1
    {
        string path = "";
        _Application excel = new _Excel.Application();
        Workbook wb;
        Worksheet ws;

        public Class1(string path, int Sheet)
        {
            this.path = path;
            wb = excel.Workbooks.Open(path);
            ws = (Worksheet)wb.Worksheets[Sheet];
        }
        public string ReadCell(int i, int j)
        {
            i++;
            j++;
            if ((ws.Cells[i, j].Value2 == "Z_KomSilnice_L (24200)/7 "))
            {
                return ws.Cells[i, j++].Value2; // get the field value
            }
            else
                return "";
        }
    }
}
 
Share this answer
 
v2
Comments
CHill60 20-Nov-20 10:09am    
5'd. You've put a lot of effort into this.
Richard MacCutchan 20-Nov-20 10:19am    
You are very kind, but I had much of the code already. And creating a project from OP's code was not exactly difficult. It was just one of those itches I needed to scratch.
dejf111 23-Nov-20 3:04am    
I don't know where the error is but for me it still writes the same error message, but thank you for your help. I will try to export xlxs file to string and then search the string
Richard MacCutchan 23-Nov-20 4:00am    
Have you changed the code to be exactly as above? As I said, this code does exactly what it is supposed to do.
dejf111 23-Nov-20 4:12am    
I changed everything according to you but the result is the same
Your ReadCell method is incorrect, you have the following:
C#
public string ReadCell(int i, int j)
{
    for ( i = 0; i < 26; i++)
    {
        for ( j = 1; j < 100; j++)
        {
            int first = 65 + i;
            string coordinates = ((char)first).ToString() + j;

            if (ws.Range[coordinates].Value == "Z_KomSilnice_L (24200)/7 ")
            {
                return ws.Range[coordinates].Value;
            }
            else
                return "";
        }
    }
}

First, you are missing a return at the end for the case when both loops complete without finding the string you want.

Second, you are ignoring the values passed in which denote the cell to test.

And third, your first range value in the loop will be "A1", but if that does not contain the search string then the method will return the blank string, and not test any further.

You should change your code to:
C#
public string ReadCell(int i, int j)
{
    for ( i = 0; i < 26; i++)
    {
        for ( j = 1; j < 100; j++)
        {
            int first = 65 + i;
            string coordinates = ((char)first).ToString() + j;

            if (ws.Range[coordinates].Value == "Z_KomSilnice_L (24200)/7 ")
            {
                return ws.Range[coordinates].Value;
            }
        }
    }
    return ""; // string not found in any cell A1:Z99
}

However, that is a very wasteful piece of code. You should use the values of i and j that are passed in to select the cell to test.

I am still at a loss to understand why the code that I tested successfully does not work on your system.
 
Share this answer
 
Comments
dejf111 25-Nov-20 5:28am    
'if (ws.Range[coordinates].Value == "Z_KomSilnice_L (24200)/7 ")' Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Operator '==' cannot be applied to operands of type 'double' and 'string''
Richard MacCutchan 25-Nov-20 6:21am    
The Value of that cell is a double and you cannot compare a double type to a string type. Any more than you can compare a boolean to a string as already discussed above. You need to check if the type of the cell is a string, or use the ToString method on the returned Value.
dejf111 25-Nov-20 6:41am    
if ((ws.Cells[i].Value.ToString() == "Z_KomSilnice_L (24200)/7 "))
Richard MacCutchan 25-Nov-20 6:44am    
Yes, but you need the column value as well. And why are you using double parentheses around the expression? What is wrong with:
if (ws.Cells[i, j].Value.ToString() == "Z_KomSilnice_L (24200)/7 ")
dejf111 25-Nov-20 6:48am    
now another mistake :D System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

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