Microsoft Excel on save changes decimal point to 8 - c#

when i open on save excel the value displayed in excel is 596207726.09 but actual code behind value is 596207726.09000003.
why this change happaned?
But 596207726.09 in excel changed to 596207726.09000003 in code.
How 000003 getting added in code and how to solve the issue in c# code so it can take exact decimal point as given in Excel
For Example
596207726.09 - 596207726.09
596207726.092 - 596207726.092

Related

ExcelDataReader.AsDataSet() converts single fraction double value into multiple fractions

I'm facing a problem when reading the excel-sheet data using ExcelDataReader in c#.
I am reading data from excel-sheet(.xlsm)
One of the cell has a list of values to choose.
Eg.
5.1
5.2
5.1a
When I choose the value either 5.2 or 5.1a and read, I get the same exact value in the dataset
But when I choose 5.1 and read, I get 5.0999999999999996 in the dataset
Here is the code which I used to read the data in c#,
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
DataSet findingsData = excelReader.AsDataSet();
Note :
For a workaround, I put a space after the value 5.1 in the cell. Then it read the value exactly same as expected(5.1 instead of 5.0999999999999996).
But I'm wondering, when it read the value 5.2 exactly same without applying any space, why doesn't work for 5.1?
Any suggestions are welcome to resolve this issue...
Thanks,
Karthik
Take a look at this question: Why can't decimal numbers be represented exactly in binary?
My maths isn't quite up to figuring it out precisely (comments welcome) but I suspect that 5.1 doesn't convert to the C# double precisely, but 5.2 does.
The reason it works when you add the space is that Excel will assume that the field is text, the same way 5.1a is, but when it receives something that looks like a number it assumes it is a number. (You can see this behaviour in a default blank spreadsheet as it will be right aligned if it is a number and left aligned when you add a space or any other text).
I expect that if you explicitly format all the cells as text in your source spreadsheet then the value will be read as you expect

Issue in using extremely huge Excel File with Formula in C#

In my current project, Aspose has been used to work with Excel file (XLXS). This excel file has 4 worksheets. First two sheets are empty except they have first row which contain column names. These tab got data through code and other two contains tons of complex formula based on these inputs. Just imagine first two tab as inputs, third tab as complex calculation and last tab as output. Average size of file ranges from 26MB to 48MB. Below piece of code does most of the work. After this method, the file has been saved in some physical location too. output date saved in DB. This process working fine so far with above range, but when size exceeded beyound 100MB, it started throwing Out of Memory exception. Hardly once or twice, it able to complete the process in around 80 - 100 mins.
public void CaclulateM(DataSet dataModel)
{
var workbook = this.ExcelModel.Workbook;
var ranges = ExcelModel.GetExcelModelRanges;
base.ImportInputsTo(workbook, ranges, dataModel);
workbook.CalculateFormula(false);
base.ExportOutputsTo(workbook, ranges, dataModel);
}
I tried out some of the solution provided by Aspose, but failed.I tried other dlls too including Interop, ExcelLibrary, NPOI, but same result.
https://forum.aspose.com/t/aspose-cell-dll-issue-for-xslb-file/164440
Please help or let me know if you need any other input to suggest anything. I cannot provide you the excel file due to confidentiality.

Paste data from clipboard to excel in right format

In performance analyzer I copy data from table in clipboard.
And then paste it in excel file.
The result is:
But when I paste it in text editor, I simple looks like:
Function Name Inclusive Samples Exclusive Samples Inclusive Samples
% Exclusive Samples %
[clr.dll] 26 26 39.39 39.39
Bee.Client.Common.BeeRight.CheckRightsForBeeUser() 10 0 15.15 0.00
Bee.Client.Common.BeeRight.get_Invoke() 6 0 9.09 0.00
Bee.Client.Common.BeeRight.Method(string,string) 13 0 19.70 0.00
Bee.Client.Common.Custom.FmCustom..ctor() 9 0 13.64 0.00
So can you tell me, how can I archive this effect?
Thanks!
Update
I'll try to explain.
I have DataGridView in my winform application. I wrote some function, which copy data from table into clipboard (the result looks like the text in my example). If I paste this text from clipboard to excel, the result will be excel file with data from clipboard, but there will be no formatting at all and this excel will be hard to read.
I wonder, how they prepare data from table (pic 1) such a way, that when I paste it to excel, it has formatting (pic 2), and when I paste it in text editor, we saw raw text..
The cause, why the direct work between your datagrid and Excel is good, is the implementation of the DataGridView component and its reaction to Copy operation, and the behavior of the application, you want to paste the content into. It can use some special codes, which are ignored by Notepad.
EDIT
So, now I understand your interest pretty well. I don't know how it works in C#, but in Java it looks so.
Every time you have any information in the clipboard there are a lot of variants, how other applications can use this content.
Suppose I want to get the content from the clipboard. I do it so:
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
but now I should determine how the information should look for my application, and here your question begins.
If I have a picture in the clipboard, I have only 1 possible representation of it:
[mimetype=image/x-java-image;representationclass=java.awt.Image]
If I have some text from Notepad, there are already 27 variants:
[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=application/x-java-serialized-object;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.io.Reader]
[mimetype=text/plain;representationclass=java.lang.String]
[mimetype=text/plain;representationclass=java.nio.CharBuffer]
and so on...
If I have some cells from an Excel sheet, there are 56 variants:
[mimetype=application/x-java-text-encoding;representationclass=[B]
[mimetype=text/html;representationclass=java.io.Reader]
[mimetype=text/html;representationclass=java.lang.String]
[mimetype=text/html;representationclass=java.nio.CharBuffer]
[mimetype=text/html;representationclass=[C]
and so on...
there is even an Image-variant for Excel-cells!
[mimetype=image/x-java-image;representationclass=java.awt.Image]
That is why it is possible to copy some cells from Excel and paste them into Paint as bitmap! It is not possible for Notepad of course, because its developers did not want to work with this presentation.
Now we can see, the clipboard is not so primitive how it can seem to be. Each time an application can analyze the content and take the best variant of it.
Now you can try to find some infos for C# development. I'm sure, you'll get it!

c# Excel losing formulae after save

Excel formula get lost after saving, were replaced by last calculation result.
Question: Is there a special save option in order to keep formulas ?
rangeTarget = workSheetTeam.get_Range(workSheetTeam.Cells[40, BG], workSheetTeam.Cells[40, BG]);
rangeTarget.Formula = "=MAX(BF8:BF25)";

Paste from Excel into C# app, retaining full precision

I have data in an Excel spreadsheet with values like this:
0.69491375
0.31220394
The cells are formatted as Percentage, and set to display two decimal places. So they appear in Excel as:
69.49%
31.22%
I have a C# program that parses this data off the Clipboard.
var dataObj = Clipboard.GetDataObject();
var format = DataFormats.CommaSeparatedValue;
if (dataObj != null && dataObj.GetDataPresent(format))
{
var csvData = dataObj.GetData(format);
// do something
}
The problem is that csvData contains the display values from Excel, i.e. '69.49%' and '31.22%'. It does not contain the full precision of the extra decimal places.
I have tried using the various different DataFormats values, but the data only ever contains the display value from Excel, e.g.:
DataFormats.Dif
DataFormats.Rtf
DataFormats.UnicodeText
etc.
As a test, I installed LibreOffice Calc and copy/pasted the same cells from Excel into Calc. Calc retains the full precision of the raw data.
So clearly Excel puts this data somewhere that other programs can access. How can I access it from my C# application?
Edit - Next steps.
I've downloaded the LibreOffice Calc source code and will have a poke around to see if I can find out how they get the full context of the copied data from Excel.
I also did a GetFormats() call on the data object returned from the clipboard and got a list of 24 different data formats, some of which are not in the DataFormats enum. These include formats like Biff12, Biff8, Biff5, Format129 among other formats that are unfamiliar to me, so I'll investigate these and respond if I make any discoveries...
Also not a complete answer either, but some further insights into the problem:
When you copy a single Excel cell then what will end up in the clipboard is a complete Excel workbook which contains a single spreadsheet which in turn contains a single cell:
var dataObject = Clipboard.GetDataObject();
var mstream = (MemoryStream)dataObject.GetData("XML Spreadsheet");
// Note: For some reason we need to ignore the last byte otherwise
// an exception will occur...
mstream.SetLength(mstream.Length - 1);
var xml = XElement.Load(mstream);
Now, when you dump the content of the XElement to the console you can see that you indeed get a complete Excel Workbook. Also the "XML Spreadsheet" format contains the internal representation of the numbers stored in the cell. So I guess you could use Linq-To-Xml or similar to fetch the data you need:
XNamespace ssNs = "urn:schemas-microsoft-com:office:spreadsheet";
var numbers = xml.Descendants(ssNs + "Data").
Where(e => (string)e.Attribute(ssNs + "Type") == "Number").
Select(e => (double)e);
I've also tried to read the Biff formats using the Excel Data Reader however the resulting DataSets always came out empty...
The BIFF formats are an open specification by Microsoft. (Note, that I say specification not standard). Give a read to this to get an idea of what is going on.
Then those BIFF you see correspond to the some Excel formats. BIFF5 is XLS from Excel 5.0 and 95, BIFF8 is XLS from Excel 97 to 2003, BIFF12 is XLSB from Excel 2003, note that Excel 2007 can also produce them (I guess Excel 2010 too). There is some documentation here and also here (From OpenOffice) that may help you make sense of the binary there...
Anyways, there is some work has been done in past to parse this documents in C++, Java, VB and for your taste in C#. For example this BIFF12 Reader, the project NExcel, and ExcelLibrary to cite a few.
In particular NExcel will let you pass an stream which you can create from the clipboard data and then query NExcel to get the data. If you are going to take the source code then I think ExcelLibrary is much more readable.
You can get the stream like this:
var dataobject = System.Windows.Forms.Clipboard.GetDataObject();
var stream = (System.IO.Stream)dataobject.GetData(format);
And read form the stream with NExcel would be something like this:
var wb = getWorkbook(stream);
var sheet = wb.Sheets[0];
var somedata = sheet.getCell(0, 0).Contents;
I guess the actual Office libraries from Microsoft would work too.
I know this is not the whole tale, please share how is it going. Will try it if I get a chance.

Categories