本文主要是介绍FineReport从数据库取数据并统计展示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、在数据库中建库建表并插入数据,一定要插入一些数值类型的数据,方便进行统计,在此就不多做阐述了,大家应该都会的
2、创建一个web项目,并使用Tomcat部署,在此也不多做阐述,直接开始编写代码了,首先需要创建一个工具类,方便我们发送请求,到另外一个springboot项目去获取数据库中的值
import java.io.*;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import com.alibaba.fastjson.JSONObject;public class HTTPOpener {private static HTTPOpener opener = null;private static HttpClient httpClient = null;private static HttpClientContext httpClientContext = null;private HTTPOpener() {//私有构造方法 单例模式 共享cookie//构造头List<Header> headers = new ArrayList<Header>();headers.add(new BasicHeader(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"));headers.add(new BasicHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"));headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, sdch"));headers.add(new BasicHeader(HttpHeaders.CACHE_CONTROL, "max-age=0"));headers.add(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));headers.add(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.8"));//2021/03/23 更新 防止被XssFilter 拦截headers.add(new BasicHeader(HttpHeaders.REFERER, "http://jhtj.crc.cr/"));httpClient = HttpClients.custom().setDefaultHeaders(headers).build();httpClientContext = HttpClientContext.create();}public static HTTPOpener getOpener() {if(opener==null) {opener = new HTTPOpener();}return opener;}public HttpResponse doPost(String url,Map<String,String> data) throws KeyManagementException, NoSuchAlgorithmException, IOException {HttpPost httpPost = new HttpPost(url);httpPost.setHeader("ContentType", "application/json");httpPost.setHeader("Connection", "Close");if(url.startsWith("http:")) {//http方式的postString jsonObj = JSONObject.toJSONString(data);// 构建消息实体StringEntity entity = new StringEntity(jsonObj, Charset.forName("UTF-8"));entity.setContentEncoding("UTF-8");entity.setContentType("application/json");// 发送Json格式的数据请求httpPost.setEntity(entity);}else if(url.startsWith("https:")) {//https方式的postSSLContext sslcontext = createIgnoreVerifySSL();// 设置协议http和https对应的处理socket链接工厂的对象Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https:", new SSLConnectionSocketFactory(sslcontext)).build();PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);HttpClients.custom().setConnectionManager(connManager);List<NameValuePair> datalist = new ArrayList<NameValuePair>();if(data!=null){//表单数据for (Entry<String, String> entry : data.entrySet()) {datalist.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}}httpPost.setEntity(new UrlEncodedFormEntity(datalist,"UTF-8"));}else {throw new RuntimeException("Error Url,it should been started with 'http' or 'https'.");}HttpResponse httpResponse = httpClient.execute(httpPost,httpClientContext);return httpResponse;}public HttpResponse doGet(String url) throws IOException {try {HttpGet target = new HttpGet(url);HttpResponse httpResponse = httpClient.execute(target,httpClientContext);return httpResponse;}catch (IOException e) {e.printStackTrace();throw e;}}}
3、开始编写我们需要在帆软设计器中展示的数据,编写一个conroller,可以随便命名,在此不做要求,代码如下:
import com.alibaba.fastjson.JSONArray;
import com.fr.data.AbstractTableData;
import com.fr.general.data.TableDataException;
import com.jctj.report.util.HTTPOpener;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;import java.io.*;
import java.util.List;
import java.util.Map;public class ArrayTableData extends AbstractTableData {protected String[] columnNames;protected List<Map<String, Object>> rowData;public ArrayTableData() {String[] columnNames = {"zp", "zd", "pj", "pd", "j", "ts", "zsj", "tzsj", "lxcs", "sxs", "bz"};this.columnNames = columnNames;}@Overridepublic int getColumnCount() throws TableDataException {return columnNames.length;}@Overridepublic String getColumnName(int i) throws TableDataException {return columnNames[i];}@Overridepublic int getRowCount() throws TableDataException {HttpResponse res = null;try {res = HTTPOpener.getOpener().doGet("http://localhost:8090/v1.0/jprocess/find");} catch (IOException e) {e.printStackTrace();}System.out.println("res ::: " + res);HttpEntity entity = res.getEntity();String result = null;if (entity != null) {try {result = EntityUtils.toString(entity);} catch (IOException e) {e.printStackTrace();}System.out.println("result : " + result);}try {System.out.println("content ::" + entity.getContent());rowData = JSONArray.parseObject(result, List.class);System.out.println("rowData : " + rowData);} catch (IOException e) {e.printStackTrace();}System.out.println("rowData : " + rowData);return rowData.size();}@Overridepublic Object getValueAt(int i, int i1) {HttpResponse res = null;try {//获取springboot项目中数据的内容res = HTTPOpener.getOpener().doGet("http://localhost:8090/v1.0/jprocess/find");} catch (IOException e) {e.printStackTrace();}System.out.println("res ::: " + res);HttpEntity entity = res.getEntity();String result = null;if (entity != null) {try {result = EntityUtils.toString(entity);} catch (IOException e) {e.printStackTrace();}System.out.println("result : " + result);}try {rowData = JSONArray.parseObject(result, List.class);System.out.println("rowData :" + rowData);} catch (Exception e) {e.printStackTrace();}try {if (i >= rowData.size() || i1 >= columnNames.length) {return null;}return rowData.get(i).get(columnNames[i1]);} catch (Throwable t) {t.printStackTrace();return "";}}
}
注意:所编写的类一定要继承AbstractTableData ,并实现该类的四个方法
4、启动你下载的FineReport设计器,开始创建一个arrayData.cpt文件,名称自己随便起:,如图所示:
5、双击该cpt文件,进入编辑页面,然后开始进行编辑,如下图所示:
6、接下来需要向该统计文件中添加内容了, 在左下角有个+,点击+号后选择程序,如图所示:
7、进入程序页面,给程序数据集起个名字,我命名的是data,然后映射到我们所编写的controller类,如下图所示:
8、映射好该类后,便可以进行cpt文件的数据编辑操作以及统计操作,直接将模板数据集中的内容拖到单元格中,统计内容如如台数,输入=号,然后定义公式,如:IF(sum(G10) > 0, SUM(G10), '')
9、保存该cpt文件,然后创建另一个springboot项目,该项目的主要功能是读取数据库中的数据,在此就不在赘述了,然后启动该springboot项目,再启动tomcat部署的帆软的项目,便可以直接展示我们的数据了,如下图所示:
10、至此,一个帆软统计的小demo就搞定了
路漫漫其修远兮,吾将上下而求索,希望此篇文章对大家有所帮助................
这篇关于FineReport从数据库取数据并统计展示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!