pom引入
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi</ artifactId>
< version> 4.1.2</ version>
</ dependency>
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi-ooxml</ artifactId>
< version> 4.1.2</ version>
</ dependency>
测试数据
private List < KeyValueVo > pieChartDateList ( ) {
List < KeyValueVo > list = Lists . newArrayList ( ) ;
for ( int i = 1 ; i <= 50 ; i++ ) {
Random random = new Random ( ) ;
KeyValueVo kv = new KeyValueVo ( ) ;
kv. setKey ( "v" + i) ;
kv. setValue ( String . valueOf ( 10 + random. nextInt ( 11 ) ) ) ;
list. add ( kv) ;
}
return list;
}
private List < NodeData > lineChartDataList ( ) {
List < NodeData > list = Lists . newArrayList ( ) ;
for ( int i = 1 ; i <= 30 ; i++ ) {
Random random = new Random ( ) ;
NodeData data = new NodeData ( ) ;
data. setTime ( String . valueOf ( i) ) ;
data. setV1 ( 10 + random. nextInt ( 11 ) ) ;
data. setV2 ( 10 + random. nextInt ( 11 ) ) ;
data. setV3 ( 10 + random. nextInt ( 11 ) ) ;
data. setV4 ( 10 + random. nextInt ( 11 ) ) ;
list. add ( data) ;
}
return list;
}
柱状图
@ApiOperation ( "柱状图" )
@GetMapping ( "/chart/bar" )
public Object barChart ( HttpServletRequest request) throws Exception {
List < NodeData > list = lineChartDataList ( ) ;
XSSFWorkbook workbook = new XSSFWorkbook ( ) ;
XSSFSheet dataSheet = workbook. createSheet ( "Data" ) ;
XSSFRow topRow = dataSheet. createRow ( 0 ) ;
topRow. createCell ( 0 ) . setCellValue ( "时间" ) ;
topRow. createCell ( 1 ) . setCellValue ( "v1" ) ;
topRow. createCell ( 2 ) . setCellValue ( "v2" ) ;
topRow. createCell ( 3 ) . setCellValue ( "v3" ) ;
int rowIdx = 1 ;
for ( NodeData data : list) {
XSSFRow row = dataSheet. createRow ( rowIdx++ ) ;
row. createCell ( 0 ) . setCellValue ( data. getTime ( ) ) ;
row. createCell ( 1 ) . setCellValue ( data. getV1 ( ) ) ;
row. createCell ( 2 ) . setCellValue ( data. getV2 ( ) ) ;
row. createCell ( 3 ) . setCellValue ( data. getV3 ( ) ) ;
}
XSSFDrawing drawing = dataSheet. createDrawingPatriarch ( ) ;
XSSFClientAnchor anchor = drawing. createAnchor ( 0 , 0 , 0 , 0 , 3 , 5 , 20 , 25 ) ;
XSSFChart chart = drawing. createChart ( anchor) ;
chart. setTitleText ( "Bar Chart" ) ;
XDDFCategoryAxis xAxis = chart. createCategoryAxis ( AxisPosition . BOTTOM ) ;
xAxis. setTitle ( "时间" ) ;
XDDFValueAxis yAxis = chart. createValueAxis ( AxisPosition . LEFT ) ;
yAxis. setTitle ( "值" ) ;
XDDFBarChartData data = ( XDDFBarChartData ) chart. createData ( ChartTypes . BAR , xAxis, yAxis) ;
XDDFDataSource < String > xData = XDDFDataSourcesFactory . fromStringCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 0 , 0 ) ) ;
XDDFNumericalDataSource < Double > yData1 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 1 , 1 ) ) ;
XDDFNumericalDataSource < Double > yData2 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 2 , 2 ) ) ;
XDDFNumericalDataSource < Double > yData3 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 3 , 3 ) ) ;
data. addSeries ( xData, yData1) . setTitle ( "v1" , null ) ;
data. addSeries ( xData, yData2) . setTitle ( "v2" , null ) ;
data. addSeries ( xData, yData3) . setTitle ( "v3" , null ) ;
data. setBarDirection ( BarDirection . COL ) ;
chart. plot ( data) ;
File excelFile = ExcelUtil . turnExcelToFile ( workbook) ;
FileInfo fileInfo = new FileInfo ( ) ;
fileInfo. setInputStream ( new FileInputStream ( excelFile) ) ;
fileInfo. setFilename ( "柱状图测试" + ExcelConstant . EXCEL_FORMAT ) ;
return FileUtil . forwardWeb ( fileInfo, request) ;
}
折线图
@ApiOperation ( "折线图" )
@GetMapping ( "/chart/line" )
public Object lineChart ( HttpServletRequest request) throws Exception {
List < NodeData > list = lineChartDataList ( ) ;
XSSFWorkbook workbook = new XSSFWorkbook ( ) ;
XSSFSheet dataSheet = workbook. createSheet ( "Data" ) ;
XSSFRow topRow = dataSheet. createRow ( 0 ) ;
topRow. createCell ( 0 ) . setCellValue ( "时间" ) ;
topRow. createCell ( 1 ) . setCellValue ( "v1" ) ;
topRow. createCell ( 2 ) . setCellValue ( "v2" ) ;
topRow. createCell ( 3 ) . setCellValue ( "v3" ) ;
topRow. createCell ( 4 ) . setCellValue ( "v4" ) ;
int rowIdx = 1 ;
for ( NodeData data : list) {
XSSFRow row = dataSheet. createRow ( rowIdx++ ) ;
row. createCell ( 0 ) . setCellValue ( data. getTime ( ) ) ;
row. createCell ( 1 ) . setCellValue ( data. getV1 ( ) ) ;
row. createCell ( 2 ) . setCellValue ( data. getV2 ( ) ) ;
row. createCell ( 3 ) . setCellValue ( data. getV3 ( ) ) ;
row. createCell ( 4 ) . setCellValue ( data. getV4 ( ) ) ;
}
XSSFDrawing drawing = dataSheet. createDrawingPatriarch ( ) ;
XSSFClientAnchor anchor = drawing. createAnchor ( 0 , 0 , 0 , 0 , 7 , 5 , 25 , 25 ) ;
XSSFChart chart = drawing. createChart ( anchor) ;
chart. setTitleText ( "Line Chart" ) ;
XDDFCategoryAxis xAxis = chart. createCategoryAxis ( AxisPosition . BOTTOM ) ;
xAxis. setTitle ( "时间" ) ;
XDDFValueAxis yAxis = chart. createValueAxis ( AxisPosition . LEFT ) ;
yAxis. setTitle ( "值" ) ;
XDDFLineChartData data = ( XDDFLineChartData ) chart. createData ( ChartTypes . LINE , xAxis, yAxis) ;
XDDFDataSource < String > xData = XDDFDataSourcesFactory . fromStringCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 0 , 0 ) ) ;
XDDFNumericalDataSource < Double > yData1 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 1 , 1 ) ) ;
XDDFNumericalDataSource < Double > yData2 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 2 , 2 ) ) ;
XDDFNumericalDataSource < Double > yData3 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 3 , 3 ) ) ;
XDDFNumericalDataSource < Double > yData4 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , 30 , 4 , 4 ) ) ;
data. addSeries ( xData, yData1) . setTitle ( "v1" , null ) ;
data. addSeries ( xData, yData2) . setTitle ( "v2" , null ) ;
data. addSeries ( xData, yData3) . setTitle ( "v3" , null ) ;
data. addSeries ( xData, yData4) . setTitle ( "v4" , null ) ;
chart. plot ( data) ;
File excelFile = ExcelUtil . turnExcelToFile ( workbook) ;
FileInfo fileInfo = new FileInfo ( ) ;
fileInfo. setInputStream ( new FileInputStream ( excelFile) ) ;
fileInfo. setFilename ( "折线图测试" + ExcelConstant . EXCEL_FORMAT ) ;
return FileUtil . forwardWeb ( fileInfo, request) ;
}
饼图
@ApiOperation ( "饼图" )
@GetMapping ( "/chart/pie" )
public Object pieChart ( HttpServletRequest request) throws Exception {
List < KeyValueVo > list = pieChartDateList ( ) ;
XSSFWorkbook workbook = new XSSFWorkbook ( ) ;
XSSFSheet dataSheet = workbook. createSheet ( "Data" ) ;
XSSFRow topRow = dataSheet. createRow ( 0 ) ;
topRow. createCell ( 0 ) . setCellValue ( "类别" ) ;
topRow. createCell ( 1 ) . setCellValue ( "值" ) ;
int rowIdx = 1 ;
for ( KeyValueVo kv : list) {
XSSFRow row = dataSheet. createRow ( rowIdx++ ) ;
row. createCell ( 0 ) . setCellValue ( kv. getKey ( ) ) ;
row. createCell ( 1 ) . setCellValue ( Double . parseDouble ( kv. getValue ( ) ) ) ;
}
XSSFDrawing drawing = dataSheet. createDrawingPatriarch ( ) ;
XSSFClientAnchor anchor = drawing. createAnchor ( 0 , 0 , 0 , 0 , 7 , 5 , 25 , 25 ) ;
XSSFChart chart = drawing. createChart ( anchor) ;
chart. setTitleText ( "Pie Chart" ) ;
XDDFPieChartData data = ( XDDFPieChartData ) chart. createData ( ChartTypes . PIE , null , null ) ;
XDDFDataSource < String > categoryData = XDDFDataSourcesFactory . fromStringCellRange ( dataSheet, new CellRangeAddress ( 1 , list. size ( ) , 0 , 0 ) ) ;
XDDFNumericalDataSource < Double > valueData = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , list. size ( ) , 1 , 1 ) ) ;
data. addSeries ( categoryData, valueData) . setTitle ( "Values" , null ) ;
chart. plot ( data) ;
File excelFile = ExcelUtil . turnExcelToFile ( workbook) ;
FileInfo fileInfo = new FileInfo ( ) ;
fileInfo. setInputStream ( new FileInputStream ( excelFile) ) ;
fileInfo. setFilename ( "饼图测试" + ExcelConstant . EXCEL_FORMAT ) ;
return FileUtil . forwardWeb ( fileInfo, request) ;
}
复合图(折线图和柱状图)
@ApiOperation ( "折线柱状复合图" )
@GetMapping ( "/chart/line-and-bar" )
public Object lineAndBarChart ( HttpServletRequest request) throws Exception {
List < NodeData > list = lineChartDataList ( ) ;
XSSFWorkbook workbook = new XSSFWorkbook ( ) ;
XSSFSheet dataSheet = workbook. createSheet ( "Data" ) ;
XSSFRow topRow = dataSheet. createRow ( 0 ) ;
topRow. createCell ( 0 ) . setCellValue ( "时间" ) ;
topRow. createCell ( 1 ) . setCellValue ( "v1" ) ;
topRow. createCell ( 2 ) . setCellValue ( "v2" ) ;
topRow. createCell ( 3 ) . setCellValue ( "v3" ) ;
topRow. createCell ( 4 ) . setCellValue ( "v4" ) ;
int rowIdx = 1 ;
for ( NodeData data : list) {
XSSFRow row = dataSheet. createRow ( rowIdx++ ) ;
row. createCell ( 0 ) . setCellValue ( data. getTime ( ) ) ;
row. createCell ( 1 ) . setCellValue ( data. getV1 ( ) ) ;
row. createCell ( 2 ) . setCellValue ( data. getV2 ( ) ) ;
row. createCell ( 3 ) . setCellValue ( data. getV3 ( ) ) ;
row. createCell ( 4 ) . setCellValue ( data. getV4 ( ) ) ;
}
XSSFDrawing drawing = dataSheet. createDrawingPatriarch ( ) ;
XSSFClientAnchor anchor = drawing. createAnchor ( 0 , 0 , 0 , 0 , 7 , 5 , 25 , 25 ) ;
XSSFChart chart = drawing. createChart ( anchor) ;
chart. setTitleText ( "Line and Bar Chart" ) ;
XDDFCategoryAxis xAxis = chart. createCategoryAxis ( AxisPosition . BOTTOM ) ;
xAxis. setTitle ( "时间" ) ;
XDDFValueAxis yAxis = chart. createValueAxis ( AxisPosition . LEFT ) ;
yAxis. setTitle ( "值" ) ;
XDDFBarChartData barData = ( XDDFBarChartData ) chart. createData ( ChartTypes . BAR , xAxis, yAxis) ;
XDDFLineChartData lineData = ( XDDFLineChartData ) chart. createData ( ChartTypes . LINE , xAxis, yAxis) ;
XDDFDataSource < String > xData = XDDFDataSourcesFactory . fromStringCellRange ( dataSheet, new CellRangeAddress ( 1 , list. size ( ) , 0 , 0 ) ) ;
XDDFNumericalDataSource < Double > yData1 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , list. size ( ) , 1 , 1 ) ) ;
XDDFNumericalDataSource < Double > yData2 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , list. size ( ) , 2 , 2 ) ) ;
XDDFNumericalDataSource < Double > yData3 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , list. size ( ) , 3 , 3 ) ) ;
XDDFNumericalDataSource < Double > yData4 = XDDFDataSourcesFactory . fromNumericCellRange ( dataSheet, new CellRangeAddress ( 1 , list. size ( ) , 4 , 4 ) ) ;
barData. addSeries ( xData, yData1) . setTitle ( "v1" , null ) ;
barData. addSeries ( xData, yData2) . setTitle ( "v2" , null ) ;
barData. setBarDirection ( BarDirection . COL ) ;
lineData. addSeries ( xData, yData3) . setTitle ( "v3" , null ) ;
lineData. addSeries ( xData, yData4) . setTitle ( "v4" , null ) ;
chart. plot ( barData) ;
chart. plot ( lineData) ;
File excelFile = ExcelUtil . turnExcelToFile ( workbook) ;
FileInfo fileInfo = new FileInfo ( ) ;
fileInfo. setInputStream ( new FileInputStream ( excelFile) ) ;
fileInfo. setFilename ( "饼图测试" + ExcelConstant . EXCEL_FORMAT ) ;
return FileUtil . forwardWeb ( fileInfo, request) ;
}