将隐藏的表(Pojo值)复制到剪贴板

时间:2018-06-20 09:28:25

标签: javascript jquery clipboard

我正在尝试将隐藏的表复制到剪贴板。 该表包含pojo列表。

我要完成的任务

  1. 将隐藏的内容复制到剪贴板
  2. 相同复制到剪贴板的表可以表格式复制到excel
  3. 再次以相同的表格格式导入复制的剪贴板包含的内容

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script type="text/javascript">
		function selectElementContents(el) {
			var body = document.body, range, sel;
			debugger;
			if(document.createRange && window.getSelection) {
				range = document.createRange();
				sel = window.getSelection();
				sel.removeAllRanges();
				try {
					range.selectNodeContents(el);
					sel.addRange(range);
				} catch (e) {
					range.selectNode(el);
					sel.addRange(range);
				}

			}else if (body.createTextRange) {
				range = body.createTextRange();
				range.moveToElementText(el);
				range.select();
			}
			document.execCommand('SelectAll');
			document.execCommand("Copy");

		}
	</script>
</head>
<body>
	<input type="button" value="select table" onclick="selectElementContents( document.getElementById('table'));">
	<table id="table" style="display: hidden;">
		<tr>
			<th>Company</th>
			<th>Contact</th>
			<th>Country</th>
		</tr>
		<tr>
			<td>Alfreds Futterkiste</td>
			<td>Maria Anders</td>
			<td>Germany</td>
		</tr>
		<tr>
			<td>Centro comercial Moctezuma</td>
			<td>Francisco Chang</td>
			<td>Mexico</td>
		</tr>
		<tr>
			<td>Ernst Handel</td>
			<td>Roland Mendel</td>
			<td>Austria</td>
		</tr>
		<tr>
			<td>Island Trading</td>
			<td>Helen Bennett</td>
			<td>UK</td>
		</tr>
		<tr>
			<td>Laughing Bacchus Winecellars</td>
			<td>Yoshi Tannamuri</td>
			<td>Canada</td>
		</tr>
		<tr>
			<td>Magazzini Alimentari Riuniti</td>
			<td>Giovanni Rovelli</td>
			<td>Italy</td>
		</tr>
	</table>
</body>
</html>

我当前面临的问题是

  • 我们无法将隐藏的表直接复制到剪贴板
  • 将导出的剪贴板表导入回应用程序

0 个答案:

没有答案