1

Currently I have to specify the cells the values have to be added to. How do I like increment in some fashion as to say no matter how many times the user hits the add to file button it should just increment the previous pattern. eg. I have :

        xlWorkSheet.Cells[1, 1] = comboBox2.Text;
        xlWorkSheet.Cells[1, 2] = textBox5.Text;
        xlWorkSheet.Cells[1, 3] = textBox2.Text;
        xlWorkSheet.Cells[1, 4] = comboBox3.Text;
        xlWorkSheet.Cells[1, 5] = textBox3.Text;
        xlWorkSheet.Cells[1, 6] = comboBox1.Text;

Upon clicking the "add to xls file" button, how would I make it now follow this pattern and save to file without having to write this into the code:

        xlWorkSheet.Cells[2, 1] = comboBox2.Text;
        xlWorkSheet.Cells[2, 2] = textBox5.Text;
        xlWorkSheet.Cells[2, 3] = textBox2.Text;
        xlWorkSheet.Cells[2, 4] = comboBox3.Text;
        xlWorkSheet.Cells[2, 5] = textBox3.Text;
        xlWorkSheet.Cells[2, 6] = comboBox1.Text;

It's not like I can copy and paste every single time, that would be way too tedious. All answers and comments are welcome. Thanks in advance

4
  • 1
    have you tried making a static variable that would reset on new excel file load and increment when the button "add to xls file" was pressed? static int i = 1; public void button_click(object sender, EventArgs e){ xlWorkSheet.Cells[i, 1] = comboBox2.Text; i++; } Nov 10, 2016 at 17:47
  • @vipersassassin Am not sure I get what you are saying
    – Javy26
    Nov 10, 2016 at 17:49
  • looks like @mybirthname has it in an answer similar to what I would have said. I would add a reset on changing of the excel file though. Nov 10, 2016 at 17:50
  • @vipersassassin problem with this is that each time I increment using yor solution the cells shift the data. So let's say I start at cell A1(which would be combox2 values). The following data goes to B2(textBox5) then C3(textBox2) and so forth.
    – Javy26
    Nov 10, 2016 at 18:00

3 Answers 3

2

You can use property.

public int IndexProp {get; set;}

public void AddToExcelBtn_Click(object sender, EventArgs e) 
{
    //... some other code

    Index +=1;
    xlWorkSheet.Cells[IndexProp , 1] = comboBox2.Text;
    xlWorkSheet.Cells[IndexProp , 2] = textBox5.Text;
    xlWorkSheet.Cells[IndexProp , 3] = textBox2.Text;
    xlWorkSheet.Cells[IndexProp , 4] = comboBox3.Text;
    xlWorkSheet.Cells[IndexProp , 5] = textBox3.Text;
    xlWorkSheet.Cells[IndexProp , 6] = comboBox1.Text;
}
13
  • I try it but am getting this: Exception from HRESULT: 0x800A03EC
    – Javy26
    Nov 10, 2016 at 17:53
  • @Jevon Use debug and what is the problem, I don't know what is the exception meaning Nov 10, 2016 at 17:55
  • An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
    – Javy26
    Nov 10, 2016 at 18:02
  • 1
    If you want the default value of the property to be 1 -> Write it like this public int IndexProp {get; set;} = 1; I suppose you are using C# 6.0 Nov 10, 2016 at 19:59
  • 1
    @Jevon Index is the property yes this increase the index. Nov 10, 2016 at 20:28
1

This is based on previous answer, but is simpler to see and would reset your index on file open. If there is a button per combo or text box, then further identifiers need to be added in.

public static int indexProp = 0;

public void OpenExcelFile(string path){
    //Open new file here
    indexProp = 0; //reset value as needed
}

public void AddToExcelBtn_Click(object sender, EventArgs e) 
{
//... some other code

indexProp +=1;
xlWorkSheet.Cells[indexProp , 1] = comboBox2.Text;
xlWorkSheet.Cells[indexProp , 2] = textBox5.Text;
xlWorkSheet.Cells[indexProp , 3] = textBox2.Text;
xlWorkSheet.Cells[indexProp , 4] = comboBox3.Text;
xlWorkSheet.Cells[indexProp , 5] = textBox3.Text;
xlWorkSheet.Cells[indexProp , 6] = comboBox1.Text;

}

4
  • what if I want to open the file and save to it once and on future data being added I just write to it without opening
    – Javy26
    Nov 10, 2016 at 18:22
  • when you want to write to a file, you have to open it. if you would like you can wrap an open and set visibility to false then determine the location of your last write and use the following location as your indexProp starting point. Nov 10, 2016 at 18:26
  • If it resets my index, wouldn't it just over write the original contents of the file?
    – Javy26
    Nov 10, 2016 at 19:19
  • Not if you read it first to determine where the last write was made to. while(xlWorkSheet.Cells[indexProp, 1] != String.Empty && indexProp < maxValue){ indexProp++; } Nov 10, 2016 at 19:46
1

You don't have to open the workbook to write to it. There are technologies available like OpenXML and ADO.NET that can treat it like a database and manipulate information. OpenXML has a steep learning curve, so I would try ADO.NET if it will work on your network.

Interop opens the files and you would have to use a cell or external text file to retrieve the number.

On a different note, I assume you are talking VBA behind the scenes for adding data. If you append data, all you need to do is start at the bottom.

lastRow = valWkSht.Range("B65535").End(xlUp).Row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.