本文转自:https://stackoverflow.com/questions/8614910/paste-from-excel-into-c-sharp-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 Biff12Biff8Biff5Format129 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.

https://stackoverflow.com/questions/13647945/how-use-clipboard-to-move-data-from-net-application-to-excel?noredirect=1

When I copy from Excel, the clipboard holds 24 formats!

System.Windows.Clipboard.GetDataObject().GetFormats().Dump();
  • EnhancedMetafile
  • System.Drawing.Imaging.Metafile
  • MetaFilePict
  • Bitmap
  • System.Drawing.Bitmap
  • System.Windows.Media.Imaging.BitmapSource
  • Biff12
  • Biff8
  • Biff5
  • SymbolicLink
  • DataInterchangeFormat
  • XML Spreadsheet
  • HTML Format
  • Text
  • UnicodeText
  • System.String
  • CSV
  • Rich Text Format
  • Embed Source
  • Object Descriptor
  • Link Source
  • Link Source Descriptor
  • Link
  • Format129

https://stackoverflow.com/questions/42481912/copying-decimal-values-from-excel-to-c-sharp-causes-to-copy-only-displayed-value

Try this:

IDataObject dataObject = Clipboard.GetDataObject();
System.IO.MemoryStream stream = dataObject.GetData("XML Spreadsheet") as System.IO.MemoryStream;
if(stream != null)
{
stream.SetLength(stream.Length - 1);
XElement xml = XElement.Load(stream);
XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet"; double actualValue;
var data = xml.Descendants(ns + "Data").Where(x => (string)x.Attribute(ns + "Type") == "Number");
if(data != null && data.Any())
{
actualValue = (double)data.First();
MessageBox.Show(actualValue.ToString());
} stream.Dispose();
}

[转]Paste from Excel into C# app, retaining full precision的更多相关文章

  1. Word Excel 操作总结

    1.与office无关使用 Aspose.Cells.dll,Aspose.Words.dll 2.使用Microsoft.Office.Interop.Excel Microsoft.Office. ...

  2. asp.net利用剪切板导出excel

    public enum ClipboardFormats : uint { CF_TEXT = 1, CF_BITMAP = 2, CF_METAFILEPICT = 3, CF_SYLK = 4, ...

  3. 对.NET中导出数据到EXCEL的几种方法探讨

    最近在做一个报表系统的时候,需要把DATASET中的数据导到EXCEL当中,于是在网上找了一遍,发现了好几种方法,本来以为应该差不多,但后来经过一一试用后,发现在性能上真的差别很大,现在就介绍一下,同 ...

  4. 使用VBA将Excel指定单元格数据、字符串或者图表对象插入到Word模板指定书签处

    准备工作: 1.首先需要提供一个word模板,并且标记好您要插入书签的位置,定义书签的命名.如图 2.模拟您要插入的Excel原始数据和图表对象 插入代码如下: Private Sub Command ...

  5. c# 导出数据到Excel模板

    最近在做一个发邮件的功能,客户要求需要导出一个Excel附件,并给了附件的格式, eg: Last Name 姓 First Name 名 Chinese Characters汉字书写(仅大陆人填写) ...

  6. c#读取excel

    Provider根据实际EXCEL的版本来设置,推荐使用ACE接口来读取.需要Access database Engine. 注意修改注册表以下两项的值为0.否则导入EXCEL当单元格内字符长度超过2 ...

  7. C#操作Excel的技巧与方法 设置单元格等

    C#操作Excel可以分为客户端和插件版本,区别就是是否需要Excel环境,功能实现一样 一.通用操作与处理(有点乱有时间再整理) 1:工程对excel类库的导入,如: c:\program file ...

  8. c#excel的操作例子

    class MyData//存储行数据 { public List<string> RowData { get; set; } } static void Main(string[] ar ...

  9. 导出带图形的数据excel表

    public static string StatisticsSR(string parmStr) { try { StatisticsSRInfo parm = JsonConvert.Deseri ...

随机推荐

  1. 动态代理模式——JDK动态代理

    今天,我就来讲一下动态代理的设计模式. 动态代理的意义在于生成一个代理对象,来代理真实对象,从而控制真实对象的访问.操作动态代理需要两个步骤:一.代理对象和真实对象建立代理关系.二.实现代理对象的代理 ...

  2. IOS弓箭传说的插件开发

    1.导出ipa进行解压后,定位到执行程序archero,ida加载后,发现很多都是sub_xxx开头的. 2.搜索资料后,原来Unity编写的程序,可以使用Il2CppDumper进行符号表还原. 下 ...

  3. netcore在CentOS7 下使用处理图片的问题

    请看代码,当你在centos下要把图片转为Base64的时候 MemoryStream ms = new MemoryStream(); try { Bitmap bmp = new Bitmap(f ...

  4. Centos7脚本一键优化

    我把优化centos7的脚本分享给大家,建议刚安装完服务器的朋友执行如下优化脚本 [root@test2 yum.repos.d]# cat centos7.sh #!/bin/bash #autho ...

  5. unittest---unittest多种加载用例方法

    在做自动化测试我们对执行用例很有要求,因为每条用例可能就和上一条数据有关系,那么我想要批量执行一些用例呢?这个怎么去操作呢?unittest自带的功能可以帮助到我们,我们可以通过不同的场景运用不同的执 ...

  6. 面试连环炮系列(九):为什么ConcurrentHashMap是线程安全的

    为什么ConcurrentHashMap是线程安全的 JDK1.7中,ConcurrentHashMap使用的锁分段技术,将数据分成一段一段的存储,然后给每一段数据配一把锁,当一个线程占用锁访问其中一 ...

  7. WSL2(预览版)体验笔记

    WSL2安装 WSL2在今年5月份Microsoft Build大会上发布了,但至今Windows10一直没收到更新推送,我想这么久过去就算没进入正式,至少也到了RC版了吧,于是开始折腾准备体验一把. ...

  8. WPF之DataTemplateSelector的运用

    本文主要记录WPF中DataTemplateSelector的运用,数据模板选择器主要运用在一些项容器中用于根据不同的数据类型选择不同的DataTemplate,以便展示不同的数据.在此以在listb ...

  9. vue-preview vue图片预览插件+缩略图样式

    一.安装 npm i vue-preview -S 二.main.js中  导入组件 //vue-preview 开始 import VuePreview from 'vue-preview'; // ...

  10. ionic项目使用Google FCM插件和Google maps插件打包android报错冲突问题

    这段时间在调FCM推送服务的插件 ,原本以为去年调通过,应该很容易,没想到还是出问题了.现将问题及解决方法整理如下,仅供参考: 先看打包报错截图:         详细报错信息:Please fix ...