1、Excel文件上传

1.1 pom依赖

  需要加入的处理Excel文件的poi依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>

1.2 前端处理

  html页面:

<div>
    <input id="inputFile" name="excelFile" type="file" style="width: 300px; display: inline;" />
    <input id="save" type="button" style="width: 60px;height: 35px;" value="上传" />
</div>

  js处理:

$(document).ready(function(){
    $("#save").click(function(){
    var formData = new FormData();
    var name = $("#inputFile").val();
    formData.append("file",$("#inputFile").get(0).files[0]);
    //这个地方可以传递多个参数,具体怎么用不太清楚
    //formData.append("name",name);
        $.ajax({
            async : false,
            url: "/test",
            data: formData,
            processData : false,
            contentType : false,
            beforeSend:function(){
                console.log("正在上传文件,请稍候");
            },
            type: "POST",
            success:function(data){
                alert("successful");
            },
            error:function(err){
                alert('连接失败');
            }
        });
    });
});

1.3 后端处理

  控制层和服务层不区分放在一起了:

@RestController
@RequestMapping("/")
public class Query {
    @RequestMapping("/test")
    public void exceltest(MultipartFile file) {
        Workbook workbook = null;
        try {
            InputStream fi = file.getInputStream();
            workbook = new XSSFWorkbook(fi);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取EXcel的第一个sheet
        Sheet sheet = workbook.getSheetAt(0);
        //总行数-1
        int rowLen = sheet.getLastRowNum();
        //总列数
        int cellLen = sheet.getRow(0).getPhysicalNumberOfCells();
        for (int i = 0; i <= rowLen; i++) {
            Row row = sheet.getRow(i);
            for (int j = 0; j < cellLen; j++) {
                Cell cell = row.getCell(j);
                System.out.println(cell.getStringCellValue());
            }
        }
    }
}

 2、json字符串加上转义字符

2.1 pom依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.6</version>
</dependency>

2.2 转义

String s = "{\"aa\":\"bb\"}";
System.out.println(s);
System.out.println(StringEscapeUtils.escapeJson(s));

输出:

{"aa":"bb"}
{\"aa\":\"bb\"}

3、枚举类的使用

public enum FruitEnum {

    APPLE("APPLE", "苹果"),
    PEAR("PEAR", "梨"),
    GRAPE("GRAPE", "葡萄");

    private String code;
    private String name;

    FruitEnum(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public List<String> getAllCode() {
        List<String> list = new ArrayList<>();
        for (FruitEnum fruit : FruitEnum.values()) {
            list.add(fruit.getCode());
        }
        return list;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

posted on 2019-07-18 23:46  浮音  阅读(226)  评论(0)    收藏  举报