C# Excel from textbox to excel cell - c#

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,

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)

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

Related

Microsoft.Office.Interop.Excel - How to add a range from one cell to another cell?

As shown in the picture, say I have created a worksheet. How to select range cell2 to cell8 ?
I tried worksheet.Range[lastRow.Cells[2], lastRow.Cells[8]], but it doesn't give me the expected result. Instead, it returns the grey area. What's the correct way of selecting it?
Any assistance would be appreciated.

C# WPF - Cut and then copy an entire Excel row

I have to edit an Excel sheet with a WPF in C# and already ordered the rows by one column. But now I need to cut a few rows and write them to another position in the sheet.
Is ther anyway to completely cut out an entire row and insert this row to another position in the sheet?
Looks like this has been answered
https://social.msdn.microsoft.com/Forums/vstudio/en-US/78059590-c294-47b4-a656-aec8ca51779f/c-excel-cut-or-copy-an-excel-row-and-move-to-another-row?forum=exceldev

Excel how to fit text in cell, based on cell witdh

I want to set the witdh of a cell in my ExcelSheet.
Set the witdh:
worksheet.Columns["K"].ColumnWidth = 114.80;
When the text is larger then the ColumnWith the text is not visible.
I want to split the text to a new row in the same cell based on the ColumnWith.
I tried to add \r\n to the string in the Excel but no result.
EDIT after answers
This works perfectly:
worksheet.Columns["K"].ColumnWidth = 114;
Excel.Range rangeK = worksheet.get_Range("K1");
rangeK.EntireColumn.WrapText = true;
What you are looking for is this:
worksheet.Range("K1:K100").WrapText = True;
That code, for example, will set the cells from K1 to K100 to wrap the contents inside their cells.
You have a couple options. The first option (which is what I would suggest) is to autoresize the columns.
worksheet.Columns.AutoFit();
The next option is to word wrap all text, which I have not done but this link might be of use to you. I hope this helps.

Copying Data with Skipped Columns From Excel and Pasting into DataGridView

I've managed to copy from excel and paste it into a datagridview in C#. But when I hold down ctrl, select columns that aren't adjacent to each other, copy and paste into datagridview - all the columns in between were also pasted. Is there a way around this without actually opening the Workbook?
Current implementation simply retrieves Clipboard.GetDataObject().GetData, formats that into a string and delimit by tab.
Unfortunately excel just copies everything not just your selected columns for outside world. You can view what is copied to the clipboard using a clipboard viewer (I've used Free Clipboard Viewer). I've created a worksheet with following
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
and selected and copied columns 2, 4 and 6. clipboard viewer shows me what I've copied not three columns but all columns from 2 to 6 (actually Sheet!C2:C6).
You can always open excel using interop or 3rd party component and get what you need and paste it manually which I had to do for converting a grid to excel like grid. I manually parsed comma separated values and set the cells individually.
edokan is right when he says that the middle columns are also copied. Is there a setting in Excel which can prevent that? NO.
However here is the simplest way to do achieve what you want.
Before copying Delete unnecessary columns. Then copy and paste. Once done you have two options
Close the Excel file without saving
Press CTRL + Z to undo.
If you want a Code solution then yes there is a possibility. Create a VSTO Add-In. What this Add-In will do is copy the relevant columns to clipboard. Not the same as CTRL + C

how to move entire existing excel range to one row down using interop in C#

how to move entire existing excel range to one row down using interop in C#?
I need to add a header in 1st row of existing excel which has only data.I want to move the entire rangel one setup down programatically in C#.
I saw moving a cell stuff,but moving the entrire range was not clear.
could you please help me with it.
I agree if you are running on a server, CloseXML is a better solution, but if you want to go the interop way:
Here is the code to add a row and shift the existing data down by one row.
// Inserts a new row at the beginning of the sheet
Microsoft.Office.Interop.Excel.Range a1 = sheet.get_Range( "A1", Type.Missing );
a1.EntireRow.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown,
Type.Missing );
After you have inserted your new row at the top, you can add the headers to the cells.
I assume you know how to open and access the sheets.

Categories

Resources