springboot在线编辑CSV文件

点击跳转到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>

/**
 对应csv实体
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserAccount {

    private Integer id;
    //读取csv时,此名称要与表头名称一致(此处大小写不一致),不一致时用下列注解Alias对应csv文件的表头
    @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"); // 获取table中每一行内容
        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),// 将json数据转化为字符串
            success : function(msg) {
                console.log("ajax:");
                console.log(msg);         
            }
        })
    }
</script>
  /*
     * 	将一个Bean集合写出到Writer,并自动生成表头  表头就是实体类属性
     */
    @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";

    }
    /*
     * 从Reader中读取CSV数据并转换为Bean列表,读取后关闭Reader
     * 此方法默认识别首行为标题行。
     */
    @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();

			
        
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值