I'm using Microsoft.Office.Interop.Excel to write data into excel cell. The cell has a certain initial height and I'm setting both "merge cell = true" and "wrap text = true".
When the cell content is large, the cell height is not increased. I tried both of the following:
_range.entrirerow.autofit()
_range.columns.autofit()
This doesn't work anything for me, please suggest some solution.
Try _range.Rows.Autofit();
Please note:
COM objects don't get released with double periods. It created tempory variables, which you can't access.
So rather use:
Range rows = _range.Rows;
rows.Autfit();
if (rows != null)
{
Marshal.FinalReleaseComObject(rows);
}
Related
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();
Taking a look at this answer Getting Cells with Formulas in Excel file, there is an elegant solution to get all cells in an Excel worksheet which contain formulas. But what if I want all the cells that do NOT contain formulas? (I do not want blank cells either - I just want the plain cell with a value and that is not a formula).
Is there an elegant solution which does not include checking each and every cell in C# VSTO environment?
If I understand your question, you want the constants, which is sort of the opposite of the formulas. There is a special cell type for this as well:
Range nonFormulas = ws.Cells.SpecialCells(XlCellType.xlCellTypeConstants);
foreach (Range r in nonFormulas)
{
// Do some stuff
}
I think you know this already, but the formulas are just:
ws.Cells.SpecialCells(XlCellType.xlCellTypeFormulas);
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.
I create cell with text. After that I set WrapText property and column width.
var cell = worksheet.Cell("A1");
cell.Style.Alignment.WrapText = true;
cell.SetValue("This is very long text");
worksheet.Column(1).Width = 10;
worksheet.Rows().AdjustToContents();
The text has been moved by words, but row height is not changed. How to adjust row height to cell content?
There are many ways to achieve this.
Don't use wrap or shrink properties on cell values rather include this line just before saving your excel
ws.Columns().AdjustToContents();
Another way is to make use of Allignment property
IXLRange titleRange = ws.Range("B2:AA2");
titleRange.Cells().Style
.Alignment.SetWrapText(true); // Its single statement
Hope it helps!!
It works when you remove the worksheet.Rows().AdjustToContents();.
Autofitting sometimes needs more of a trial and error approach ...
The following code worked for me.
IXLRange contents = ws.Range("A1:A50");
contents.Style.Alignment.WrapText = true;
You can also AdjustToContents on Specific range of cells.
worksheet.Columns(2, 20).AdjustToContents();
I think this is a bug of ClosedXML.
enter link description here
ws.Row(1).AdjustToContents();
ws.Row(1).ClearHeight();
In my case it works.
Version: 0.95.4
I have a worksheet that spans multiple pages, both vertically and horizontally. It has certain rows and columns that are set to repeat on each page. I would like to know where these page breaks are going to be; I essentially want to get the information from Page Break Preview into my C# code so that I can fix things like cells that are merged across pages, and adding a signature box that is one page wide.
I tried using the Worksheet.HPageBreaks property, but the HPageBreaks.Count property is always null, even when I know that the worksheet has multiple pages.
To the best of my own knowledge, Excel does not store that data internally, but rather calculates it closer to real time based on PageSettings, PrinterSettings, manually stored "PageBreak" markers, and cell sizes and content.
The best solution I could suggest would be attempting to calculate these things yourself.
I also freely admit that I could be completely wrong about this, but from my own experiences, this is likely the case. I will update as//if//when my own research finds something new.
Update: Link
This is barely relevant, but on the same topic. Perhaps it contains some examples or data that you can manipulate for your own ends.
http://social.msdn.microsoft.com/Forums/sv/exceldev/thread/0d1fd9f2-9111-42c4-ad36-3082f4311202
Suppose you want to place a custom footer on the last row of a page break.
On a new blank worksheet in landscape, the last row of the page is row 34.
Try it!
Open Excel
New Worksheet
Type "hi" in cell A1
You'll see the automatic page breaks (dotted lines) appear.
If the row heights don't change, each page has 34 rows.
So place the footer every 34th row. Simple!
But get's hairier if there are rows with varying heights. Now you must roll up your sleeves and cook your own algorithm! The goal is to find an offset that will always land you where you intent to.
We know 1 row = 15 RowHeight points (on 100% dpi). The approach is to find the line break count inside a given cell to determine how many rows to offset.
// cell w/ multiple lines of text
Excel.Range rng = ws.get_Range("A3");
int lineBreakCnt = rng.Text.Count(x => x == '\n');
// if range is merged, it will not auto-adjust RowHeight. So:
int rowHeight = 15; // 15 points = 1
int rowHeight = lineBreakCnt > 1 ? rowHeight * lineBreakCnt + (rowHeight / 2) : rowHeight * lineBreakCnt + rowHeight;
rng.RowHeight = rowHeight;
//Calc footer row
int footerRow = 34;
//depending how much cell contents push down, adjust footer row
if (lineBreakCnt == 1)
{
footerRow -= (lineBreakCnt - 2) + 2;
}
else
if (lineBreakCnt > 1)
{
footerRow -= (lineBreakCnt -2) + 1;
}
ws.get_Range("A" + footerRow).Formula = "Cool footer w/ formatted text here";
Test it out by plugging in multiple lines of text in A3.
If this helps you, please give it a like for support.