之前工作中遇到粘贴复制大量数据,研究一下java代码解放双手
模拟鼠标录入数据
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.19</version>
</dependency>
前端页面
<script>
window.onload = function() {
document.getElementById("data").focus();
}
function submitForm(event) {
event.preventDefault(); // Prevent the default form submission
var data = document.getElementById("data").value; // Get the input value
var url = "/add/" + encodeURIComponent(data); // Construct the URL
window.location.href = url; // Redirect to the URL
}
</script>
</head>
<body>
<h1>Auto Submit Form</h1>
<form onsubmit="submitForm(event)" method="post">
<label for="data">Data:</label>
<input type="text" id="data" required>
<br>
<input type="submit" value="Submit">
</form>
</body>
图中红框部分,位于哪个位置,需要自己用截图工具 获取坐标,snipatst
后端接口
public class TestController {
@GetMapping("/add/{data}")
public String add(Model model,@PathVariable("data") String data){
System.out.println("+++++++++++++++++++++++++++++=您添加了一条数据" + data);
return "index";
}
}
public static void main(String[] args) throws AWTException {
int x = 100, y =150;
List<String> list = Arrays.asList("123", "345", "test");
Robot robot = new Robot();
for (String value : list) {
String code = String.valueOf(value);
//模拟
ClipboardUtil.setStr(code);
robot.mouseMove(x,y);
// 模拟鼠标左键点击
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
ClipboardUtil.getStr();
// 模拟粘贴操作(Ctrl+V)
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
// 模拟按下Enter键
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}}
执行代码:
实现自动录入数据。
比如获取execl 中某列数据,可以依赖easyexcel 获取数据,或者从数据库中直接查询数据。实现自动录入,解放双手。