注意导出Execl表格用Get请求 如:
function aa(){
location.href = 'workList/exportListExport.action?start_date='+参数的值+'&end_date='+参数的值;
}
比如:经过查询展示到前端页面数据如下图:
我们如果想把“饼状图”导入到我们EXCEL表格中如下图:
思想打开就是使用jfree图表绘制类库绘制图表,并生成到本地或读取至输出流用于在excel中生成,使用POI操作EXECL:
首先导入需要的pom
<properties>
<poi-version>3.11</poi-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<!-- 以上是操作excel的pom-->
<!-- https://mvnrepository.com/artifact/jfree/jcommon -->
<dependency>
<groupId>jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jfree/jfreechart -->
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies>
代码如下:
生成饼图:
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TestPoiAndJfreeChart {
public static void main(String[] args) throws Exception {
// excel2003工作表
HSSFWorkbook wb = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = wb.createSheet("Sheet 1");
// 创建字节输出流
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//如 果不使用Font,中文将显示不出来
Font font = new Font("新宋体", Font.BOLD, 15);
// 创建数据:饼状图就是名称和值(比例)
Map<String, Double> map=new HashMap<String, Double>();
map.put("录播主机", (double) 1000);//名称,值
map.put("编码器", (double) 700);
map.put("摄像机", (double) 600);
map.put("流转码服务器", (double) 400);//这里参数写死了用的时候换成你自己参数就行了
// 创建JFreeChart
JFreeChart chart = createPort(