Retrieve excel cell text that is not wide enough - c#

I want to read excel display value, not value that excel internally saves. Problem is that when this value is wider that the cell it makes cell to display ####### which is OK in excel. But I want to read this value from excel API. When I access property range. Text I also get value ########. Is there a way to get value which will be displayed when cell is wide enough?
i.e some third property, not Value2 nor Text.
My colleague said that there are three values in excel, one internal value how excel represents cell value, one what is currently displayed and one what will be displayed when there is enough space, is he right?
If this is not possible, I have another solution which might work. I used AutoFit() method to make cell wide enough and that I read value. But the problem is that I don't want to change width of columns. Is there a way to remember width, then change width so I can read cell, and that return it again to previous width?
Or maybe I should somehow apply formatting option to value of cell to get what will be displayed?
How to solve this?

In VBA I would do this by getting the Value and the number Format, and then useing the VBA Format Function to format it, but I am not sure how to do this using c#.
As a matter of curiosity, why would you want to get the formatted value anyway?

Related

EPPLUS: how to check if all cell are valid?

I'm using Excel's data validation lists, checking if the cell value is into a dynamic range (the cell values of another column).
The problem is that if I add a value into the source range (eg. "A"), I can input that value on the cell with data validation. But then, if I change the source (eg from "A" to "B") the cell with data validation is still "A".
In Excel there's a button that sets a red circle around invalid data, but client does not always check if everything is good.
Can I perform that check with EPPLUS?
The documentation doesn't suggest that this check can be performed by EPPlus:
https://www.epplussoftware.com/Developers/Features mentions "Create, read, modify, delete Data validations" and links to sample code in their wiki, which is all about designing the sheet and inspecting its design, no content validation.
I've searched the source code and looked at ExcelDataValidation.cs and its various sub-types. Their "Validate" methods only check if the validation is properly defined according to Excel's rules.
To achieve what you're asking for, you'll have to use EPPlus to get the validation from the cell, interpret it depending on its type and evaluate the cell content. There is an example that touches some of the cases and could give you an idea where this is heading.

Precision of DataGridView doubles when copying to clipboard

I am displaying an array of doubles using a DataGridView.
(There is no limitation on the dimensions of the array)
I intentionally don't display every decimal place of each double to the user, instead I set the format of the cell to something like:
dataGridView.Columns[i].DefaultCellStyle.Format = "f3";
So, a value 1.112233 would be displayed as "1.112".
However, when copying a selected cell to the clipboard and pasting into Excel I only ever get the displayed precision. I would like to know if there is a straightforward way to specify that I want the cell Value to be copied to the clipboard not the FormattedValue?
Please note that I am not limited to copying single cells, I would like the user to be able to copy multiple cells, ie
dataGridView.MultiSelect = true;
dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
When debugging I have verified that the Value property on the cell does indeed contain the original double.
I have specified the the ValueType is a double.
I attempted to override the Ctrl + C command with a key down event and output the selected cells to the clipboard manually. However, the order of the cells in the dataGridView.SelectedCells does not necessarily match the order in which they were selected. For example, when two neighbouring cells are selected their order seems to have been arbitrarily reversed. ie select a value in column 0 and then a value in column 1, the order in the dataGridView.SelectedCells was column 1 and then column 0.
It feels too convoluted and excessively inefficient to have to manually sort these back into a sensible order before then manually copying to the clipboard.
Is there a simple way to do this or do I just have to put the effort into the manual copy?

Number stored as Text

I'm currently updating values in an Excel template and I keep getting my currency fields to have the green triangle in the top corner and the "Number Stored as Text" message. How do I get Excel to recognize the cell is a number and I want it to treat it that way since I already have the cell formatted as currency? (just like what happens if I'm on the cell in Excel, hit F2, and then hit enter)
Here's a simplified version of what I'm currently doing:
UInt32Value moneyFormat = report.createCellFormat(report.Stylesheet, fontIndex, backgroundIndex, borderIndex, 168);
report.UpdateValue("Workbook", "I29", "1234.56", moneyFormat, true, String.Empty);
And here's an image of what my cells look like
You might find the answer in this post:
Open XML SDK 2.0 - how to update a cell in a spreadsheet?
Probably the DataType of the cell is not set correct.

How to get a column total in a Winforms Pivot Grid control

I need to display the percentage of each cell, of a pivot grid, column against its column total.
So far, I have been checking possible ways to get the columns totals being displayed by the PivotGrid (as I already know how to insert a computed column using expressions) but so far I haven't found a place where these values are available.
I have also considered to handle the CustomSummary or CustomUnboundFieldData events of the PivotGrid but I don't see clearly how can I get the column total from there.
Another approach I have think of is to try to use the PivotCellBaseEventArgs.CreateDrillDownDataSource to get the underlying records of the cell, but then I will not be able to get the column total, as the records returned by that method are only for a specific cell, not for the whole column.
I'll appreciate if you can point out what would be the best approach for dealing with the cell percentage against column total requirement, including any of the previous or maybe another approach I haven't think of.
Also I will be glad if you show me a way of getting the column total from the pivot grid control, if there's any way.
I finally find the way to do this so I am posting it in case anyone else need it...
There is a property on a PivotGridField that allows you to control the way the data is "summarized" (i.e. aggregated). Usually this is set to "as is" or Default but you can change this to support what is called Percentage Modes.
So I just change the PivotSummaryDisplayType, of the field that I wanted to display as percentage, to PivotSummaryDisplayType.PercentOfColumnGrandTotal and that's it.

How do you programmatically break up one Excel document into several while still maintaining each cell's style and format?

m trying to break one large Excel spreadsheet into several. I've made good progress, but I'm running into some problems. Specifically, the values that get copied over don't retain their format (for instance, 40322 instead of 5/24/2010 and -101 instead of (101.00) ). I've tried using the style (see below) but that doesn't even get me the font, let alone the number format. Any help or a poke in the right direction would be appreciated.
There are 2 loops, one for row, one for column.
destinationSheet.Cells[i, j].Style = sourceSheet.Cells[i, j].Style;
Instead of looping for each cell, you can copy/paste the entire range of cells using the pastespecial method.
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.namedrange.pastespecial(VS.80).aspx

Categories

Resources