Easypoi word模板批量导出

版本介绍:

spring boot1.5.6

mysql

easypoi 3.2.0

 

1、需求:

用户在勾选“全选”的时候,点击批量导出时,会根据勾选的单位名称依次导出在一张表中。

Easypoi word模板批量导出_第1张图片

 

Easypoi word模板批量导出_第2张图片

 

2、根据样式制作一个模板

Easypoi word模板批量导出_第3张图片

 

Easypoi word模板批量导出_第4张图片

3、想要使用easypoi框架做excel/word 要导出以下依赖

Easypoi word模板批量导出_第5张图片

 

4、如果要导出excel的话,就在实体上面添加@Excel注解(name="xxx",heigth="20"),里面还可以 添加excel的各个样式(详情可以去看easypoi API自定义)。word的话,是不需要添加注解的!

Easypoi word模板批量导出_第6张图片

5、Controller层的结构:

Easypoi word模板批量导出_第7张图片

Controller(service)

Easypoi word模板批量导出_第8张图片

 

controller:

@RestController

@RequestMapping("/wordController")

@SuppressWarnings("all")

public class PropertyCostBillWordController {

 

@Autowired

private PropertyCostBillExcelService propertyCostBillExcelService;

 

@Autowired

private AirSecondaryOrderService airSecondaryOrderService;

 

@RequestMapping("/getPropertyCostBillWord")

public String getPropertyCostBillWord(HttpServletRequest request, HttpServletResponse response) {

//1,通过id list 获取 实体对象列表

BCSLoginInfo loginInfo = BCSContext.getLoginInfo();

if (loginInfo == null) {

System.out.println("loginInfo is null");

return null;

}

 

String prcbIds = null;

if (!StringUtils.isEmpty(request.getParameter("prcbIds"))) {

prcbIds = request.getParameter("prcbIds");

}

 

//文件下载请求头、字节码、类型

response.setHeader("content-type", "application/octet-stream");

response.setContentType("application/octet-stream");

response.setCharacterEncoding("UTF-8");

 

//总表map

List propertyCostBillList = propertyCostBillExcelService.getPropertyCostBillList(prcbIds);

List xwpfDocuments = new ArrayList<>();

try {

for (PropertyCostBill propertyCostBill : propertyCostBillList) {

DecimalFormat df = new DecimalFormat("#0.00");

double totalMoney = 0.00;

if (propertyCostBill != null) {

totalMoney = propertyCostBill.getDueAmount().doubleValue();

}

String totalMoneyCapital = CapitalUtil.moneyToChinese(totalMoney);

 

Map map = new HashMap<>();

map.put("customerName", propertyCostBill.getCustomerNameStr());

map.put("dueAmount", propertyCostBill.getDueAmountStr());

map.put("unitPrice", propertyCostBill.getUnitPrice());

map.put("totalArea", propertyCostBill.getTotalArea());

map.put("type", propertyCostBill.getTypeStr());

map.put("billBucket", propertyCostBill.getBillBucket());

map.put("totalMoney", df.format(totalMoney));

map.put("totalMoneyCapital", totalMoneyCapital);

 

//从resources中导出word模板

XWPFDocument doc = WordExportUtil.exportWord07("word/property.docx", map);

xwpfDocuments.add(doc);

}

 

if (xwpfDocuments.size()>0) {

XWPFDocument xwpfDocument = xwpfDocuments.get(0);

for (int i = 0; i < xwpfDocuments.size(); i++) {

//每次的追加为了避免样式和格式混乱 加上分页符

//当是只有一条数据的时候 直接输出

if (i == 0) {

xwpfDocument = xwpfDocuments.get(0);

continue;

} else {

//当存在多条时候

xwpfDocument = mergeWord(xwpfDocument, xwpfDocuments.get(i));

}

}

String name = "物管费缴费详情.docx";

// 指定下载的文件名

response.setHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes("utf-8"), "ISO8859-1"));

ServletOutputStream outputStream = response.getOutputStream();

xwpfDocument.write(outputStream);

outputStream.close();

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

 

return CAjaxResult.toJson(200, null, null);

}

//word模板,两个对象进行追加

public XWPFDocument mergeWord(XWPFDocument document, XWPFDocument doucDocument2) throws Exception {

XWPFDocument src1Document = document;

XWPFParagraph p = src1Document.createParagraph();

//设置分页符

p.setPageBreak(true);

CTBody src1Body = src1Document.getDocument().getBody();

XWPFDocument src2Document = doucDocument2;

CTBody src2Body = src2Document.getDocument().getBody();

XWPFParagraph p2 = src2Document.createParagraph();

XmlOptions optionsOuter = new XmlOptions();

optionsOuter.setSaveOuter();

String appendString = src2Body.xmlText(optionsOuter);

String srcString = src1Body.xmlText();

String prefix = srcString.substring(0, srcString.indexOf(">") + 1);

String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));

String sufix = srcString.substring(srcString.lastIndexOf("<"));

String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));

CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix);

src1Body.set(makeBody);

return src1Document;

}

}

 

6、Service层的结构:

Easypoi word模板批量导出_第9张图片

 

6-1、Service(controller)

通过Controller层的service方法中的call,来调用service层的方法

HttpResponse resp = ServAPIUtil.call("billExcel","GetPropertyCostBillExcelList", params);

通过注解@CAPI写service层的业务逻辑:

根据 如:通过String prcbIds="a4796bb7-9530a,a4796bb7-9530a,a4796bb7-9530a"多个prcbIds查询mybatis的要返回的值。

Easypoi word模板批量导出_第10张图片

6-2、Service(service)

Easypoi word模板批量导出_第11张图片

6-3、ServiceImpl

Easypoi word模板批量导出_第12张图片

 

6-4、通过mapper来接口,到Dao层查询所需字段

Easypoi word模板批量导出_第13张图片

6-5、resources-Mapper

Easypoi word模板批量导出_第14张图片

 

6-6、mapper的sql语句查询

Easypoi word模板批量导出_第15张图片

 

Easypoi word模板批量导出_第16张图片

 

 

后端基本可以了,但是还是有些问题

 

前端:

添加批量导出、全选的按钮。第三个红色长方形里面是用vue绑定的id、value、v-model

Easypoi word模板批量导出_第17张图片

 

圈中表示:prcbIds为一个数组

Easypoi word模板批量导出_第18张图片

 

通过ajax文件下载给后台拿到prcbIds数据。

如:String prcbIds="a4796bb7-9530a,a4796bb7-9530a,a4796bb7-9530a"

Easypoi word模板批量导出_第19张图片

 

复选框的设置:

Easypoi word模板批量导出_第20张图片

 

你可能感兴趣的:(通往梦想的大门)