| @@ -1,21 +1,32 @@ | |||
| package com.iformall.print.data; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.IOException; | |||
| import java.io.StringReader; | |||
| import java.util.ArrayList; | |||
| import java.util.Iterator; | |||
| import java.util.List; | |||
| import javax.xml.parsers.DocumentBuilderFactory; | |||
| import javax.xml.parsers.ParserConfigurationException; | |||
| import javax.xml.xpath.XPath; | |||
| import javax.xml.xpath.XPathFactory; | |||
| import org.jsoup.Jsoup; | |||
| import org.jsoup.nodes.Document; | |||
| import org.jsoup.nodes.Element; | |||
| import org.jsoup.nodes.Node; | |||
| import org.springframework.stereotype.Service; | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.alibaba.fastjson.JSONArray; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils; | |||
| import com.iformall.domain.po.WxFinanceReceive; | |||
| @Service | |||
| public class PrintCustomedTableDataHandler{ | |||
| public String getTableContent(String componentContent,JSONArray datas,JSONObject entity){ | |||
| public String getTableContent(String componentContent,JSONArray datas,JSONObject entity) { | |||
| if (null != entity) { | |||
| for (Iterator<String> it = entity.keySet().iterator(); it.hasNext();) { | |||
| String fieldName = it.next(); | |||
| @@ -39,52 +50,89 @@ public class PrintCustomedTableDataHandler{ | |||
| StringBuffer sb = new StringBuffer(); | |||
| for (int i = 0 ; i < datas.size(); i ++) { | |||
| JSONObject o = datas.getJSONObject(i); | |||
| Element tr = titleTr.parent().appendElement("tr"); | |||
| sb.append("<tr "); | |||
| if (StringUtils.isNotBlank(trclass)) { | |||
| sb.append(" class = \"").append(trclass).append("\" "); | |||
| tr.attr("class", trclass); | |||
| } | |||
| if (StringUtils.isNotBlank(trstyle)) { | |||
| sb.append(" style = \"").append(trstyle).append("\" "); | |||
| tr.attr("style", trstyle); | |||
| } | |||
| sb.append(">"); | |||
| for (int j = 0 ; j < titleTds.size() ; j ++) { | |||
| Node td = titleTds.get(j); | |||
| String tdclass = titleTr.attr("class"); | |||
| String tdstyle = titleTr.attr("style"); | |||
| String tdcolspan = titleTr.attr("colspan"); | |||
| Node td = titleTds.get(j); | |||
| Element tre = tr.appendElement("td"); | |||
| String tdclass = td.attr("class"); | |||
| String tdstyle = td.attr("style"); | |||
| String tdcolspan = td.attr("colspan"); | |||
| sb.append("<td "); | |||
| if (StringUtils.isNotBlank(tdclass)) { | |||
| sb.append(" class = \"").append(tdclass).append("\" "); | |||
| tre.attr("class", tdclass); | |||
| } | |||
| if (StringUtils.isNotBlank(tdstyle)) { | |||
| sb.append(" style = \"").append(tdstyle).append("\" "); | |||
| tre.attr("style", tdstyle); | |||
| } | |||
| if (StringUtils.isNotBlank(tdcolspan)) { | |||
| sb.append(" colspan = \"").append(tdcolspan).append("\" "); | |||
| tre.attr("colspan", tdcolspan); | |||
| } | |||
| sb.append(">"); | |||
| //取什么属性 | |||
| String fieldName = td.attr("fieldName"); | |||
| if (StringUtils.isNotBlank(fieldName)) { | |||
| StringBuffer tdcontent = new StringBuffer(); | |||
| if (fieldName.contains(",")) { | |||
| String sep = td.attr("sep"); | |||
| String[] fs = fieldName.split(","); | |||
| if (null != fs) { | |||
| for (int k = 0 ; k<fs.length; k++) { | |||
| sb.append(o.get(fs[k])).append(sep); | |||
| Object v = o.get(fs[k]); | |||
| if (k == fs.length-1) { | |||
| sep = ""; | |||
| } | |||
| if (null != v) { | |||
| tdcontent.append(v).append(sep); | |||
| }else { | |||
| tdcontent.append("").append(sep); | |||
| } | |||
| } | |||
| } | |||
| }else { | |||
| sb.append(o.get(fieldName)); | |||
| Object v = o.get(fieldName); | |||
| if (null != v) { | |||
| tdcontent.append(v); | |||
| }else { | |||
| tdcontent.append(""); | |||
| } | |||
| } | |||
| tre.appendText(tdcontent.toString()); | |||
| } | |||
| sb.append("</td>"); | |||
| } | |||
| sb.append("</tr>"); | |||
| } | |||
| titleTr.parent().appendText(sb.toString()); | |||
| return doc.html(); | |||
| return doc.head().html() + doc.body().html(); | |||
| } | |||
| public static void main(String[] args) { | |||
| PrintCustomedTableDataHandler p = new PrintCustomedTableDataHandler(); | |||
| String s = "<style>table,td,th{border:1px solid #000;border-collapse:collapse}table{width:515px;font-size:10px;text-align:center}tr{height:30px}.one{width:10%}.two{width:16%}.three{width:32.5%}.four{width:10%}.five{width:10%}.six{width:10%}.seven{width:10%}</style><table id=\"fmbraidhtmltable\"><tr id=\"titletr\"><td class=\"one\" fieldName=\"shopNumber\">房间名称</td><td class=\"two\" fieldName=\"feesName\">费用科目</td><td class=\"three\" fieldName=\"starttime,endtime\" sep=\"~\">费用区间</td><td class=\"four\" fieldName=\"receivePay\">应收金额</td><td class=\"five\" fieldName=\"setOff\">冲抵金额</td><td class=\"six\" fieldName=\"youhui\">优惠金额</td><td class=\"seven\" fieldName=\"pay\">实收金额</td></tr></table>"; | |||
| List<WxFinanceReceive> rlist = new ArrayList<WxFinanceReceive>(); | |||
| WxFinanceReceive r1 = new WxFinanceReceive(); | |||
| r1.setFeesName("adssasd"); | |||
| WxFinanceReceive r2 = new WxFinanceReceive(); | |||
| r2.setFeesName("11111"); | |||
| rlist.add(r1); | |||
| rlist.add(r2); | |||
| String sss = p.getTableListContent(s, JSON.parseArray(JSON.toJSONString(rlist))); | |||
| System.out.println(sss); | |||
| } | |||
| } | |||