mcakir
mcakir

Reputation: 1

C# Excel from textbox to excel cell

I've a little problem about to copy and paste from textbox to excel cells. Basicly, When I get data from website to textbox, I can not put this values into excel cells regularly.

My data:

xxxxxxx-qqq-1111-13661 3********************* 01/02/2020 02/03/2016 0

xyzxyzx-qqq-2222-11067 3********************* 02/03/2016 12/09/2028 0

qazwsge-qqq-1234-01940 6********************* 03/04/2015 09/01/2002 0

qweqwfe-qqq-4567-01941 6********************* 04/05/2013 17/06/2025 0         

You can see 4 space between columns.

When I copy this data from textbox with manually or [Clipboard.SetText(textBox1.Text)] technique, Again I can paste any excel sheet manually. But, When I selected any cell, I did selected and just one clicked to cell, I can paste my data one by one regularly. (When I double clicked to cell, I can not paste data to cells one by one, just stay into one cell)

I tried this code but, it gave reaction like a double click.

        Clipboard.SetText(textBox1.Text);
        Excel.Range area = (Excel.Range)xlPage.Cells[2,1];
        area.Value2 = Clipboard.GetText();

My question is, How can I do the works like manual with C# codes? Is it possible for this manual technique?

Best regards,

Upvotes: 0

Views: 541

Answers (2)

mcakir
mcakir

Reputation: 1

Thanks for your answers,

I found the solution. You can try the PasteSpecial method.

Clipboard.SetText(textBox1.Text); Excel.Range area = (Excel.Range)xlPage.Cells[2,1]; area.PasteSpecial();

Upvotes: 0

Adisak Anusornsrirung
Adisak Anusornsrirung

Reputation: 690

In excel VBA you can use method TextToColumns to do what you want. See https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.namedrange.texttocolumns?view=vsto-2017 for C#.NET example.

Below is the sample code for your application.

Clipboard.SetText(textBox1.Text);
Excel.Range area = (Excel.Range)xlPage.Cells[2,1];
area.Value2 = Clipboard.GetText();

area.TextToColumns(area, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierDoubleQuote, true)

Upvotes: 0

Related Questions