C#读取剪切板内Excel内容:处理方式
- 一:获取文本处理
读取剪切板内的Excel数据可以用Clipboard.GetText()获取文本信息;
然后按照\r\n拆分行,按照\t拆分列;
如果某个单元格中存在\r\n,这时候拆分就会有问题;
比如这样的Excel:
6、8、8之间是存在回车符(\r\n)的。
遇到这种情况用代码处理一下;
1 string str = Clipboard.GetText();//读取剪切板内容 2 var rowArray = str.TrimEnd("\r\n".ToCharArray()).Split(new[] { "\r\n" }, StringSplitOptions.None);//先用回车拆分 3 List<int> listCount = rowArray.Select(x => x.Length - x.Replace("\t", "").Length).ToList();//算一下每行按\t拆分的数量 4 var result = from item in listCount group item by item into gro orderby gro.Count() descending select gro.Key;//取条数最多的数量 5 var cc = result.First(); 6 List<string> newRowArray = new List<string>(); 7 for (int i = 0; i < rowArray.Length; i++) 8 { 9 var w = rowArray[i]; 10 var c = w.Length - w.Replace("\t", "").Length; 11 if(c >= cc) 12 newRowArray.Add(w); 13 else 14 { 15 while (c < cc) 16 { 17 w += "\r\n" + rowArray[i + 1]; 18 c = w.Length - w.Replace("\t", "").Length; 19 i++; 20 }//这一段判断如果数量不相等,把后面的数据拼接上; 21 newRowArray.Add(w); 22 } 23 } 24 //这样得到的newRowArray就是按行拆分后的Excel数据 25 for (var i = 0; i < newRowArray.Count; i++) 26 { 27 var columnStr = newRowArray[i].Split(new[] { "\t" }, StringSplitOptions.None);//这时候再按照\t拆分就可以了 28 //做需要的数据处理 29 }
注意:
代码只是大概处理一下,还有很多情况没有考虑进去,仅供参考,暂时还没有想到好的更好解决方案,待完善。。。
- 二:用Html格式处理
2022/2/10更新:
今天抽时间研究了一下,发现一种更好的方式去处理剪切板内的Excel数据;
这种数据如果用 Clipboard.GetText()获取的数据看一下,直接用\t,\r\n去拆分的话,明显会特别乱;
我们换一种方式,具体代码如下:
var excelData = Clipboard.GetData(DataFormats.Html);
excelData 内容如下:
Version:1.0 StartHTML:0000000212 EndHTML:0000004378 StartFragment:0000002336 EndFragment:0000004326 SourceURL:file:///C:/Users/*****/AppData/Local/Microsoft/Windows/INetCache/Content.MSO/2CF67B7C.xlsx <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=utf-8"> <meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 15"> <link id=Main-File rel=Main-File href="file:///C:/Users/*****/AppData/Local/Temp/msohtmlclip1/01/clip.htm"> <link rel=File-List href="file:///C:/Users/*****/AppData/Local/Temp/msohtmlclip1/01/clip_filelist.xml"> <style> <!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";} @page {margin:.75in .7in .75in .7in; mso-header-margin:.3in; mso-footer-margin:.3in;} .font5 {color:windowtext; font-size:9.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:宋体; mso-generic-font-family:auto; mso-font-charset:134;} tr {mso-height-source:auto; mso-ruby-visibility:none;} col {mso-width-source:auto; mso-ruby-visibility:none;} br {mso-data-placement:same-cell;} td {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:black; font-size:10.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:"Times New Roman", serif; mso-font-charset:0; mso-number-format:General; text-align:left; vertical-align:top; border:none; mso-background-source:auto; mso-pattern:auto; mso-protection:locked visible; white-space:nowrap; mso-rotate:0;} .xl65 {border:.5pt solid windowtext;} .xl66 {border:.5pt solid windowtext; white-space:normal;} ruby {ruby-align:left;} rt {color:windowtext; font-size:9.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:宋体; mso-generic-font-family:auto; mso-font-charset:134; mso-char-type:none; display:none;} --> </style> </head> <body link=blue vlink=purple> <table border=0 cellpadding=0 cellspacing=0 width=336 style='border-collapse: collapse;width:252pt'> <!--StartFragment--> <col width=56 span=6 style='width:42pt'> <tr height=34 style='height:25.5pt'> <td height=34 class=xl65 width=56 style='height:25.5pt;width:42pt'>1\t2</td> <td class=xl65 width=56 style='border-left:none;width:42pt'>2\t\t4</td> <td class=xl65 width=56 style='border-left:none;width:42pt'>1\r\n3</td> <td class=xl66 width=56 style='border-left:none;width:42pt'>1<br> 2</td> <td class=xl65 width=56 style='border-left:none;width:42pt'>1 2</td> <td class=xl65 width=56 style='border-left:none;width:42pt'>4</td> </tr> <tr height=17 style='height:12.75pt'> <td height=17 class=xl65 style='height:12.75pt;border-top:none'>5</td> <td class=xl65 style='border-top:none;border-left:none'>6</td> <td class=xl65 style='border-top:none;border-left:none'>7</td> <td class=xl65 style='border-top:none;border-left:none'>8</td> <td class=xl65 style='border-top:none;border-left:none'>9</td> <td class=xl65 style='border-top:none;border-left:none'>10</td> </tr> <tr height=34 style='height:25.5pt'> <td height=34 class=xl65 style='height:25.5pt;border-top:none'>3</td> <td class=xl65 style='border-top:none;border-left:none'>4</td> <td class=xl65 style='border-top:none;border-left:none'>5</td> <td class=xl65 style='border-top:none;border-left:none'>6</td> <td class=xl66 width=56 style='border-top:none;border-left:none;width:42pt'>1<br> 2</td> <td class=xl65 style='border-top:none;border-left:none'>1\r\n3</td> </tr> <tr height=51 style='height:38.25pt'> <td height=51 class=xl65 style='height:38.25pt;border-top:none'>1\t2</td> <td class=xl66 width=56 style='border-top:none;border-left:none;width:42pt'>1<br> 2<br> 3</td> <td class=xl65 style='border-top:none;border-left:none'>1 2</td> <td class=xl65 style='border-top:none;border-left:none'>4</td> <td class=xl65 style='border-top:none;border-left:none'>2\t\t4</td> <td class=xl65 style='border-top:none;border-left:none'>1 2</td> </tr> <!--EndFragment--> </table> </body> </html>
用浏览器打开看一下效果:
这种数据已经按照Html的Table给拆分好了,提取数据就会特别方便,处理Html的Table可以参考我的这篇博文:
C#将HTML表格<TABLE>转换成DataTable - 十年新 - 博客园 (cnblogs.com)