Get cell style from excel spreadsheet with C# - c#

Im trying to copy some cells from one workbook-sheet to another workbook-sheet and preserve the style and formatting using C#.
I can get the attributes one by one like this:
string fontName = ((Excel.Range) workSheet.Cells[3, 2]).Font.Name.ToString();
But Im looking for at way to get it all at once. Thanks

If you copy the cell(s) via the clipboard using the Range.Copy Destination:= method then both the content and formatting will be copied. If you just want to copy the formatting use .Copy and then .PasteSpecial formats

Related

Tickmark using epplus

My problem is, when I am trying to generate my Excel file
I have some cells contains the tickmark symbol.
using (ExcelRange Rng = wsSheet.Cells[col])
{
Rng.Value = Beneficiary.IDentityOrFamilyBookCheck;// this value is ü
Rng.Style.Font.SetFromFont("Wingdings", 16);
Rng.Style.Font.Charset = 178;
}
in fact with the specified font and value it must appear as a check
but it is not
This is how it actually appears
This is how it must appear
Your Charset is wrong. Try Rng.Style.Font.Charset = 2; It is always good to do some reverse engineering. In this case create an Excel file with the Font and symbol you want and read the Style within your program.
I was able to discover the solution in my own way. Please note that Behnam's way is excellent also and can clarify the issue, but I did it in a different way.
I created an excel file containing the required symbol.
I changed the .xsls extension to .zip.
Iow I became able to show my excel file XML content and discover the actual Rng.Style.Font.Charset.

copy data from Excel cells without formatting c#

I have winform project in c# that I want to copy data from Excel cells to my datagirdview. But the excell cells is formatted with 2 digits after the comma.I want to get all value.
What i mean :
The excell cell value = 884,79258
Formatted value = 884,79
I am getting the data with Clipboard.GetText() method. Bu it gives me formatted value(884,79).
But i want the get all value which is 884,79258.
These are my codes
String[] lines = Clipboard.GetText().Split('\n');
string result = lines[0];
Thanx.
Edit:
Thanx to dr.nulll. The link that he shared is worked.
I am sharing the link below for those who have similar request.
Copying decimal values from Excel to C# causes to copy only displayed values
Don’t go via the clipboard. Either use Excel via interop or a 3rd party library like NPOI. If you must use the clipboard then the value copied reflects the current formatting of the cell, so you need it set to show 4 digits after the decimal point.

NPOI C# Cell Format Warning(Number Stored As Text)

I'm trying to generate a excel file(.xlsx) using C# in visual studio 2019, using the NPOI library. I ran into the issue where the data inserted is format incorrectly.
I have a field which can have both numbers and text, when it is displayed in the excel the cell gives warning "Number Stored As Text" only on number fields like in the image attached.
I tried following methods to make cell a string cell.
By giving Style to the cell.
ICellStyle cellStyle = book.CreateCellStyle();
cellStyle.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("text");
By settng cell Type
ICell cell = row.CreateCell(cellIndex);
cell.CellStyle = cellStyle;
cell.SetCellType(CellType.String);
cell.SetCellValue(itemData.PropertyName.ToString());
Still I get same warning in generated excel file
Any help is appreciated.
This is correct and valid behaviour. The Excel message is informational, not an error. It is telling you that you HAVE managed to store a text string consisting of only numeric characters. Your options in Excel are to convert it to a number or ignore the "error".
If you wanted the value stored as a number then you have to NOT make the cell type string, doing something like this:
if (itemData.PropertyName.ToString().Any(c => !char.IsNumber(c)))
cell.SetCellType(CellType.String);
else
cell.SetCellType(CellType.Numeric);
This might be of use: NPOI Cell Formatting Link

Working with a multi-line Excel Cell into a C# Program

My problem seems to be a simple case, but I can't seem to find any answers on it. I'm trying to write a program that will allow me to read Excel files using C# and the Interop method. These Excel files can contain information that has been entered with the use of "Alt-Enter" to create multiple lines within the cell, each line denoting a different value.
Currently my program spits out the contents of the whole cell, but how do I separate out the multiple values in the single cell, so that I am able to work on the individual values? Sorry if this has a simple answer, I'm a noob at this!
You can use String.Split function:
string[] values = ((string) multiLineCell.Value).Split('\n');

Query excel spreadsheet for cell format.... (percentage)

I am using the .Net 4.0 and excel 2003
How can i use an oledb connection to retrieve the cell format of an excel spreadsheet... I specifically want to find out if a cell column (or cell itself) is in a numeric percentage format.
I cannot seem to find this information in the GetOleDbSchemaTable method.
EX: My web app reads numbers from an excel spreadsheet. This works fine; However, if the numbers are in a percentage format, excel displays it as (fraction*100) but the actual value is a fractional decimal (1/3 = .3333..) - Excel displays as 33.33% - (Notice the decimal point).
Therefore, i need a way of distinguishing between what is a percentage & what is not to allow my webapp to work properly...
Any ideas?
Thanks in advance.....
You might be able to get out this information with OleDbConnection.GetSchema, but I'm not sure what information you'll get for an Excel sheet with that. Documentation here.
Search the NumberFormat property of the Cell (Range) in question for a % sign.
Also, if you can get the format type, then you're looking for a format that starts with 'P', like 'P1'.
EDIT: The only way I can see is either using XML or Automation. For automation you need to use the Interop Assembly. Namespace: Microsoft.Office.Interop.Excel, Interface: DisplayFormat, Property: NumberFormat.
Can you just read the first row of data as a string and parse it looking for '%' in the string.

Categories