Reputation: 121
I have data in an Excel spreadsheet with values like this: 31% and 69%
After parsing Clipboard I get value as .31 and .69. I am expecting to get 31 and 69 or 31% and 69%
I have a C# program that parses this data off the Clipboard.
const string ClipboardFormat = "XML Spreadsheet";
if (Clipboard.ContainsData(ClipboardFormat))
{
object clipData = Clipboard.GetData(ClipboardFormat);
MemoryStream ms = clipData as MemoryStream;
XmlDocument xml = new XmlDocument();
xml.Load(ms);
XmlNodeList table = xml.GetElementsByTagName("Table");
foreach (XmlNode row in table[0].ChildNodes)
{
foreach (XmlNode cell in row.ChildNodes)
{
GridView.SetRowCellValue(iRowIndex, GridView.Columns[iColIndex], cell.InnerText);
iColIndex++;
}
iRowIndex++;
}
cell.InnerText always gives me value as .31 and .69 where as I am expecting values 31 and 69 or 31% and 69%. I want to use "XML Spreadsheet" format as It helps me get right Date format [Feb-14 gives me 02/01/2014] which I am looking for.
Please suggest me how I can resolve this using "XML Spreadsheet" DataFormat.
Upvotes: 1
Views: 2840
Reputation:
If you have a cell containg the value "61%", then in fact, this is the VALUE 0.61, but FORMATTED as percent.
Excel copies this to the clipboard as follows:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s69">
<NumberFormat ss:Format="0%"/>
</Style>
</Styles>
<Worksheet ss:Name="Tabelle1">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1"
ss:DefaultColumnWidth="60" ss:DefaultRowHeight="14.5">
<Row>
<Cell ss:StyleID="s69"><Data ss:Type="Number">0.61</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
Unfortunately, you need to do some effort in order to extract the values properly. In the <CELL> tag, please note this Attribute:
ss:StyleID="s69"
And then look up into the <Styles> section, there you find this style and the <NUMBERFORMAT> tag:
<NumberFormat ss:Format="0%"/>
So in fact, "0% is the FORMAT which will be applied to the VALUE 0.61.
It is a little bit complicated to extract the format, but in fact, all the information is in the XML text.
Upvotes: 0
Reputation: 149
As you should know, Excel stores percent data as a part of 1, so when you see "50%" in Excel cell, there is "0.50" really stored in the xls file. I can think of 3 options:
If you are using DevExpress Gridview (as I can see - you do), you can use following code to display data as a percent, instead of share of 1:
//myPercentColumn - a column in which you want to put your percentile data...
myPercentColumn.DisplayFormat.FormatType = FormatType.Numeric;
myPercentColumn.DisplayFormat.FormatString = "p0";
Change Excel's cell format to "Text" - worst option, as you wouldn't be able to use that cell value in Excel formulas.
Parse value in your C# code, like multiplying value by 100 (or convert it to string after multiplying, and add "%").
Upvotes: 1