Tab Delimited Text Copy Paste To Excel

本秂侑毒 提交于 2019-12-24 01:47:58

问题


I am generating a block of text via C# Stringbuilder, with appropriately tab-delimited text (new lines, "\t", etc.) and displaying the text within a tag in my web application. If I copy/paste this to Excel, all the text pastes into the first column on the spreadsheet.

If I copy the outerHTML of the block (Chrome) or copy/paste into Notepad++ and THEN paste to Excel, all the data pastes neatly into individual cells - which is the desired outcome of this effort. Can anyone tell me what I need to do in order to be able to copy the text from my application and paste it into Excel, so that the text pastes into individual cells (ie, keeps the tab-delimited format)?


回答1:


Unfortunately, it appears an extra step is required.

The default behaviour of paste in Excel (& many other applications) is to use the original format of the contents of the clipboard.

Copying from a web application/HTML page will result in the clipboard contents being flagged as HTML. The clipboard contents will have the tab character, but as HTML renders tabs to whitespace (compacting to a single whitespace if there are multiple tabs) - just using control-V will convert the tabs to spaces & the contents will appear in a single cell when pasting into Excel..

You will need to use the "Paste Special" option & select "Unicode text" to retain the tabs. There is no way to set this by default : https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_windows8-mso_365hp/how-to-change-default-paste-behavior-with-ctrl-v/f58da075-8fd5-4c80-a64b-5e71ec8ad38b?auth=1

Copying the outerHTML of Chrome sets the clipboard format to text in the first place, pasting to Notepad++ does paste as text rather than HTML then recopying sets the format to text - which is why these methods work.




回答2:


I was able to paste to Excel and keep the format by changing the element my text wrote to from a div to textarea. Copy/paste of tsv from textarea to a tab-delimited Excel spreadsheet keeps the data formatted in tabular format when pasted. Thanks all for the help.




回答3:


Try this ,

Before pasting data set excel's text to column delimiters to space and try to paste your values again.




回答4:


You can use 'text to columns' in Excel to format the data in the way you want it to.

After pasting the data into excel, select the column with your data.

  1. Go to Data - click 'Text to columns'
  2. Choose delimited and hit next
  3. Choose Tab and hit finish.

If you don't want to use this extra step, the following works for Excel 2013:

  1. Go to Home, Paste options and click 'Use text import wizard'
  2. Choose delimited and hit next
  3. Choose Tab and hit finish.


来源:https://stackoverflow.com/questions/46645125/tab-delimited-text-copy-paste-to-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!