c#读取剪切板数据的时候,通常读取字符串:var txt = Clipboard.GetText();但此时可能精度丢失,比如excel中,数据是:1.3213216532132,但是格式保留2个精度。
此时如果直接用文字读取剪切板,读取到的是格式化后的结果,即1.32。这时就丢失了精度。如下图。
为了避免精度丢失,可以采用读取xml格式的剪切板数据,这样精度就可以保证了。
代码如下:
var clipboard = Clipboard.GetDataObject(); string XmlFmt = "XML Spreadsheet"; if (!clipboard.GetDataPresent(XmlFmt)) { return null; } var clipData = clipboard.GetData(XmlFmt); StreamReader streamReader = new StreamReader((MemoryStream)clipData); streamReader.BaseStream.SetLength(streamReader.BaseStream.Length - 1); string xmlText = streamReader.ReadToEnd(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xmlText); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable); nsmgr.AddNamespace("o", "urn:schemas-microsoft-com:office:office"); nsmgr.AddNamespace("x", "urn:schemas-microsoft-com:office:excel"); nsmgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet"); var tbRows = xmlDocument.SelectNodes("//ss:Worksheet/ss:Table/ss:Row", nsmgr); var maxColumnCount = GetMaxColumnCount(tbRows, nsmgr); StringBuilder resultString = new StringBuilder(); int rowIndex = 0; foreach (XmlNode tbRow in tbRows) { rowIndex++; if (tbRow.Attributes["ss:Index"] == null) { AddRowCells(tbRow, nsmgr, resultString, maxColumnCount); } else { var rowIndexAttr = int.Parse(tbRow.Attributes["ss:Index"].Value); while (rowIndex < rowIndexAttr) { for (int i = 0; i < maxColumnCount; i++) { resultString.Append("\t"); } rowIndex++; resultString.Append("\r\n"); } //添加row AddRowCells(tbRow, nsmgr, resultString, maxColumnCount); } resultString.Append("\r\n"); } var txt = resultString.ToString(); return txt;
源码如下:https://files.cnblogs.com/files/congqiandehoulai/2021-10-20-winform%E8%AF%BB%E5%8F%96excel%E7%B2%98%E8%B4%B4%E6%9D%BF.rar
参考文档:
https://stackoverflow.com/questions/8614910/paste-from-excel-into-c-sharp-app-retaining-full-precision
https://stackoverflow.com/questions/43793360/how-to-use-clipboard-to-copy-data-from-excel-sheet-to-datatable