目前easyExcel可以导出各种复杂的excel,但是如果需要在导出excel的同时绘制柱形图之类的,这个好像是做不到的,在文档中也没有看到相关的描述,如果可以向poi一样绘制各种图形的话,这会更好
xytpbqjk1#
import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFChart; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.util.IOUtils; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class ExcelChartExample { public static void main(String[] args) { try (Workbook workbook = new XSSFWorkbook()) { Sheet sheet = workbook.createSheet("Bar Chart Example"); // Create some data Row row; Cell cell; for (int r = 0; r < 5; r++) { row = sheet.createRow(r); for (int c = 0; c < 2; c++) { cell = row.createCell(c); if (c == 0) { cell.setCellValue("Category " + (r + 1)); } else { cell.setCellValue((r + 1) * 10); } } } // Create a bar chart Drawing<?> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 1, 15, 15); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.BOTTOM); LineChartData data = chart.getChartDataFactory().createLineChartData(); ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 4, 0, 0)); ChartDataSource<Number> ys = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 4, 1, 1)); data.addSeries(xs, ys); chart.plot(data, bottomAxis, leftAxis); // Save the Excel file try (FileOutputStream fileOut = new FileOutputStream("BarChartExample.xlsx")) { workbook.write(fileOut); } System.out.println("Excel file with chart created successfully."); } catch (IOException e) { e.printStackTrace(); } } }
1条答案
按热度按时间xytpbqjk1#