? ? ? ?在Excel文檔中,圖表是指將工作表中的數據用圖形的方式表示出來载庭。它不僅能夠反映數據間的某種相對關系,以此來幫助我們分析和對比數據;同時能夠使得數據更加易于閱讀囚聚,引人注目靖榕。本文就將通過使用Java程序來演示如何在Excel文檔中創(chuàng)建一些常用的圖表,包括柱狀圖顽铸、餅狀圖和折線圖茁计。
使用工具:Free Spire.XLS for Java (免費版)
Jar文件獲取及導入:
方法1:通過官網下載獲取jar包。解壓后將lib文件夾下的Spire.Xls.jar文件導入Java程序谓松。(如下圖)
方法2:通過maven倉庫安裝導入簸淀。具體安裝詳解參見此網頁。
【示例1】創(chuàng)建柱狀圖
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import com.spire.xls.charts.ChartSeries;
import java.awt.*;
public class ColumnChart { ???
public static void main(String[] args) { ???????
//創(chuàng)建Workbook對象 ???????
Workbook workbook = new Workbook(); ???????
//獲取第一個工作表 ? ??????
Worksheet sheet = workbook.getWorksheets().get(0); ???????
//將圖表數據寫入工作表 ???????
sheet.getCellRange("A1").setValue("部門");? ? ? ?
sheet.getCellRange("A2").setValue("開發(fā)部"); ??????? sheet.getCellRange("A3").setValue("測試部"); ??????? sheet.getCellRange("A4").setValue("銷售部"); ??????? sheet.getCellRange("A5").setValue("技術支持部"); ??????? sheet.getCellRange("B1").setValue("男性"); ??????? sheet.getCellRange("B2").setNumberValue(65); ??????? sheet.getCellRange("B3").setNumberValue(21); ??????? sheet.getCellRange("B4").setNumberValue(12); ??????? sheet.getCellRange("B5").setNumberValue(30); ??????? sheet.getCellRange("C1").setValue("女性"); ??????? sheet.getCellRange("C2").setNumberValue(13); ??????? sheet.getCellRange("C3").setNumberValue(33); ??????? sheet.getCellRange("C4").setNumberValue(28); ??????? sheet.getCellRange("C5").setNumberValue(21); ???????
//設置單元格樣式 ???????
sheet.getCellRange("A1:C1").setRowHeight(15); ??????? sheet.getCellRange("A1:C1").getCellStyle().setColor(Color.darkGray); ??????? sheet.getCellRange("A1:C1").getCellStyle().getExcelFont().setColor(Color.white); ??????? sheet.getCellRange("A1:C1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center); ??????? sheet.getCellRange("A1:C1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center); ???????
sheet.autoFitColumn(1); ???????
//添加柱狀圖 ???????
Chart chart = sheet.getCharts().add(ExcelChartType.ColumnClustered); ???????
//設置圖表數據區(qū)域 ???????
chart.setDataRange(sheet.getCellRange("A1:C5")); ??????? chart.setSeriesDataFromRange(false); ???????
//設置圖表位置 ???????
chart.setLeftColumn(5); ???????
chart.setTopRow(1); ???????
chart.setRightColumn(14); ???????
chart.setBottomRow(21); ???????
//設置圖表標題 ???????
chart.setChartTitle("部門信息"); ???????
chart.getChartTitleArea().isBold(true); ???????
chart.getChartTitleArea().setSize(12); ???????
//設置x,y軸的名稱 ???????
chart.getPrimaryCategoryAxis().setTitle("部門"); ??????? chart.getPrimaryCategoryAxis().getFont().isBold(true); ??????? chart.getPrimaryCategoryAxis().getTitleArea().isBold(true); ??????? chart.getPrimaryValueAxis().setTitle("人數"); ??????? chart.getPrimaryValueAxis().hasMajorGridLines(false); ??????? chart.getPrimaryValueAxis().getTitleArea().isBold(true); ??????? chart.getPrimaryValueAxis().getTitleArea().setTextRotationAngle(90); ???????
//設置系列顏色毒返、重疊、寬度和數字標簽 ???????
ChartSeries series = chart.getSeries(); ???????
for (int i = 0 ; i < series.size() ; i++) { ???????????
ChartSerie cs = series.get(i); ???????????
cs.getFormat().getOptions().isVaryColor(true); ??????????? cs.getFormat().getOptions().setOverlap(-50); ??????????? cs.getFormat().getOptions().setGapWidth(350); ??????????? cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true); ??????? } ? ?????
//設置圖例位置 ???????
chart.getLegend().setPosition(LegendPositionType.Top); ???????
//保存文檔 ???????
workbook.saveToFile("output/ColumnChart.xlsx",ExcelVersion.Version2016);? ??
}
}
柱狀圖創(chuàng)建效果:
【示例2】創(chuàng)建餅狀圖
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import java.awt.*;
public class PieChart { ???
public static void main(String[] args) { ???????
//創(chuàng)建Workbook對象 ???????
Workbook workbook = new Workbook(); ???????
//獲取第一個工作表 ???????
Worksheet sheet = workbook.getWorksheets().get(0); ???????
//將圖表數據寫入工作表 ???????
sheet.getCellRange("A1").setValue("國家"); ???????
sheet.getCellRange("A2").setValue("古巴"); ???????
sheet.getCellRange("A3").setValue("墨西哥"); ??????? sheet.getCellRange("A4").setValue("法國"); ???????
sheet.getCellRange("A5").setValue("德國"); ???????
sheet.getCellRange("B1").setValue("銷售額"); ??????? sheet.getCellRange("B2").setNumberValue(400000); ??????? sheet.getCellRange("B3").setNumberValue(600000); ??????? sheet.getCellRange("B4").setNumberValue(700000); ??????? sheet.getCellRange("B5").setNumberValue(850000); ???????
//設置單元格樣式 ???????
sheet.getCellRange("A1:B1").setRowHeight(15); ??????? sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray); ??????? sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white); ??????? sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center); ??????? sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center); ??????? sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("\"$\"#,##0"); ???????
//添加餅狀圖 ???????
Chart chart = sheet.getCharts().add(ExcelChartType.Pie); ???????
//設置圖表數據區(qū)域 ???????
chart.setDataRange(sheet.getCellRange("B2:B5")); ??????? chart.setSeriesDataFromRange(false); ???????
//設置圖表位置 ???????
chart.setLeftColumn(4); ???????
chart.setTopRow(1); ???????
chart.setRightColumn(13); ???????
chart.setBottomRow(21); ???????
//設置圖表標題 ???????
chart.setChartTitle("市場份額"); ???????
chart.getChartTitleArea().isBold(true); ???????
chart.getChartTitleArea().setSize(12); ???????
//設置系列標簽 ???????
ChartSerie cs = chart.getSeries().get(0); ??????? cs.setCategoryLabels(sheet.getCellRange("A2:A5")); ??????? cs.setValues(sheet.getCellRange("B2:B5")); ??????? cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true); ??????? cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasPercentage(true); ??????? chart.getPlotArea().getFill().setVisible(false); ???????
//保存文檔 ???????
workbook.saveToFile("output/PieChart.xlsx", ExcelVersion.Version2016); ???
}
}
餅狀圖創(chuàng)建效果:
【示例3】創(chuàng)建折線圖
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import com.spire.xls.charts.ChartSeries;
import java.awt.*;
public class LineChart { ???
public static void main(String[] args) { ???????
//創(chuàng)建Workbook對象 ???????
Workbook workbook = new Workbook(); ???????
//獲取第一個工作表 ???????
Worksheet sheet = workbook.getWorksheets().get(0); ???????
//將圖表數據寫入工作表 ???????
sheet.getCellRange("A1").setValue("月份"); ???????
sheet.getCellRange("A2").setValue("一月"); ???????
sheet.getCellRange("A3").setValue("二月"); ???????
sheet.getCellRange("A4").setValue("三月"); ???????
sheet.getCellRange("A5").setValue("四月"); ???????
sheet.getCellRange("A6").setValue("五月"); ???????
sheet.getCellRange("A7").setValue("六月"); ???????
sheet.getCellRange("B1").setValue("臺式電腦"); ??????? sheet.getCellRange("B2").setNumberValue(80); ???? ???sheet.getCellRange("B3").setNumberValue(45); ??????? sheet.getCellRange("B4").setNumberValue(25); ??????? sheet.getCellRange("B5").setNumberValue(20); ??????? sheet.getCellRange("B6").setNumberValue(10); ??????? sheet.getCellRange("B7").setNumberValue(5); ??????? sheet.getCellRange("C1").setValue("手提電腦"); ??????? sheet.getCellRange("C2").setNumberValue(30); ??????? sheet.getCellRange("C3").setNumberValue(25); ??????? sheet.getCellRange("C4").setNumberValue(35); ??????? sheet.getCellRange("C5").setNumberValue(50); ??????? sheet.getCellRange("C6").setNumberValue(45); ??????? sheet.getCellRange("C7").setNumberValue(55); ??????? sheet.getCellRange("D1").setValue("平板電腦"); ??????? sheet.getCellRange("D2").setNumberValue(10); ??????? sheet.getCellRange("D3").setNumberValue(15); ??????? sheet.getCellRange("D4").setNumberValue(20); ??????? sheet.getCellRange("D5").setNumberValue(35); ??????? sheet.getCellRange("D6").setNumberValue(60); ??????? sheet.getCellRange("D7").setNumberValue(95); ???????
//設置單元格樣式 ?? ?????
sheet.getCellRange("A1:D1").setRowHeight(15); ??????? sheet.getCellRange("A1:D1").getCellStyle().setColor(Color.darkGray); ??????? sheet.getCellRange("A1:D1").getCellStyle().getExcelFont().setColor(Color.white); ??????? sheet.getCellRange("A1:D1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center); ??????? sheet.getCellRange("A1:D1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center); ???????
//添加折線圖 ???????
Chart chart = sheet.getCharts().add(ExcelChartType.Line); ???????
//設置圖表數據區(qū)域 ???????
chart.setDataRange(sheet.getCellRange("A1:D7")); ??????? chart.setSeriesDataFromRange(false); ???????
//設置圖表位置 ???????
chart.setLeftColumn(6); ???????
chart.setTopRow(1); ???????
chart.setRightColumn(15); ???????
chart.setBottomRow(21); ???????
//設置圖表標題 ???????
chart.setChartTitle("產品銷售趨勢圖"); ???????
chart.getChartTitleArea().isBold(true); ???????
chart.getChartTitleArea().setSize(12); ???????
//設置x,y軸的名稱 ???????
chart.getPrimaryCategoryAxis().setTitle("月份"); ??????? chart.getPrimaryCategoryAxis().getFont().isBold(true); ??????? chart.getPrimaryCategoryAxis().getTitleArea().isBold(true); ??????? chart.getPrimaryValueAxis().setTitle("銷售量"); ??????? chart.getPrimaryValueAxis().hasMajorGridLines(false); ??????? chart.getPrimaryValueAxis().getTitleArea().setTextRotationAngle(90); ??????? chart.getPrimaryValueAxis().getTitleArea().isBold(true); ???????
//設置系列顏色和數字標簽 ???????
ChartSeries series = chart.getSeries(); ???????
for (int i = 0; i < series.size(); i++) { ?? ?????????
ChartSerie cs = series.get(i); ???????????
cs.getFormat().getOptions().isVaryColor(true); ??????????? cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true); ??????????? cs.getDataFormat().setMarkerStyle(ChartMarkerType.Circle); ??????? } ???????
//設置圖例位置 ???????
chart.getLegend().setPosition(LegendPositionType.Top); ???????
//保存文檔 ??????? workbook.saveToFile("LineChart.xlsx", ExcelVersion.Version2016); ???
}
}
折線圖創(chuàng)建效果:
(本文完)