点击跳转到Hutool文档
点击跳转fastjson
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.45</version>
<scope>compile</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserAccount {
private Integer id;
@Alias("Account")
private String account;
@Alias("Password")
private String password;
}
<table id="tab" width="100%" >
<thead>
<th width="10%" >id</th>
<th width="10%">account</th>
<th width="10%">password</th>
</thead>
<tbody>
<tr th:each="c : ${csvlist}">
<td><input type="text" name="id" th:value="${c.id}" ></td>
<td><input type="text" name="account" th:value="${c.account}" ></td>
<td><input type="text" name="password" th:value="${c.password}"></td>
</tr>
</tbody>
</table>
<script>
function push()
{
var tr = $("#tab tr");
var result = [];
for (var i = 0; i < tr.length; i++) {
var tds = $(tr[i]).find("td");
if (tds.length > 0) {
result.push({
"id" : $(tds[0]).find("input").val(),
"account" : $(tds[1]).find("input").val(),
"password": $(tds[2]).find("input").val(),
})
}
}
console.log(result);
console.log(JSON.stringify(result));
$.ajax({
type : "post",
url : "postcsv",
contentType : "application/json;charset=UTF-8",
data : JSON.stringify(result),
success : function(msg) {
console.log("ajax:");
console.log(msg);
}
})
}
</script>
@ResponseBody
@PostMapping("/postcsv")
public String postcsv(@RequestBody String json){
List<User> list = JSON.parseArray(json,User.class);
CsvWriter writer = CsvUtil.getWriter( csvPathProperties.getCsvFilePath().getPath(), CharsetUtil.CHARSET_GBK);
writer.writeBeans(list);
writer.close();
return "success";
}
@GetMapping("/csv")
public String getcsv_workspace1(Map<String,Object> map, HttpServletRequest request) {
CsvReader reader = CsvUtil.getReader();
List<UserAccount> result = reader.read(
ResourceUtil.getReader(workspace1_csvPathProperties.getCsvFilePath().getPath(), CharsetUtil.CHARSET_GBK), UserAccount.class);
reader.close();
}