1. 程式人生 > >Easypoi word模板批量匯出

Easypoi word模板批量匯出

版本介紹:

spring boot1.5.6

mysql

easypoi 3.2.0

1、需求:

使用者在勾選“全選”的時候,點選批量匯出時,會根據勾選的單位名稱依次匯出在一張表中。

2、根據樣式製作一個模板

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

4、如果要匯出excel的話,就在實體上面新增@Excel註解(name="xxx",heigth="20"),裡面還可以 新增excel的各個樣式(詳情可以去看easypoi API自定義)。word的話,是不需要添加註解的!

5、Controller層的結構:

Controller(service)

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<PropertyCostBill> propertyCostBillList = propertyCostBillExcelService.getPropertyCostBillList(prcbIds);

List<XWPFDocument> 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<String, Object> 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層的結構:

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的要返回的值。

6-2、Service(service)

6-3、ServiceImpl

6-4、通過mapper來介面,到Dao層查詢所需欄位

6-5、resources-Mapper

6-6、mapper的sql語句查詢

後端基本可以了,但是還是有些問題

前端:

新增批量匯出、全選的按鈕。第三個紅色長方形裡面是用vue繫結的id、value、v-model

圈中表示:prcbIds為一個數組

通過ajax檔案下載給後臺拿到prcbIds資料。

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

複選框的設定: