- 一:獲取文本處理
讀取剪切板內的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)