import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.zeus.model.*;
import com.zeus.model.common.PageResult;
import com.zeus.service.CommentsService;
import com.zeus.utils.function.EasyExcelCallable;
import com.zeus.utils.listener.CommentListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
public static void main(String[] args) {
String url = "C:\\活动\\1.xls";
// 数据处理
List<CommentExcel> dueList = new ArrayList<>();
// 查询留言数据
EasyExcelCallable<CommentExcel> callable = dataList -> {
System.out.println("]]]");
if(CollectionUtil.isEmpty(dataList)) {
return;
}
for (CommentExcel commentExcel : dataList) {
CommentV2Param.Comment comment = JSONUtil.toBean(commentExcel.getComment(), CommentV2Param.Comment.class);
if(Objects.isNull(comment) || CollectionUtil.isEmpty(comment.getPicData())) {
continue;
}
//TODO 导出后将 此列内容 复制到 txt 文件中 在重新复制回来就 会以图片得形式展示
String table = "<table><img src={src} width=200></table>";
String img = comment.getPicData().get(0).getUrl();
if(StringUtils.isBlank(img)) {
continue;
}
CommentExcel dueComment = new CommentExcel();
dueComment.setPhone(commentExcel.getPhone());
dueComment.setComment(table.replace("{src}", img));
dueComment.setTime(commentExcel.getTime());
dueList.add(dueComment);
}
System.out.println("==");
// 写法1 JDK8+
// since: 3.0.0-beta1
String fileName = "C:\\活动\\2.xls";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, CommentExcel.class).sheet("模板").doWrite(dueList);
};
ExcelReader excelReader = EasyExcel.read(url, CommentExcel.class, new CommentListener(callable)).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet);
// 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
excelReader.finish();
}