{"id":19260824,"url":"https://github.com/ashrawan/apache-poi-chart-sample","last_synced_at":"2025-04-11T15:13:18.200Z","repository":{"id":119544862,"uuid":"575707383","full_name":"ashrawan/apache-poi-chart-sample","owner":"ashrawan","description":"A Sample code showcasing how to work with Apache POI Excel file and smart charts.","archived":false,"fork":false,"pushed_at":"2022-12-08T09:30:28.000Z","size":243,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T11:21:42.420Z","etag":null,"topics":["apache-poi","excel","excel-chart","java","poi","spring-boot","xlsx"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashrawan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-08T05:41:31.000Z","updated_at":"2024-10-22T08:07:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"46040ebd-91fc-476d-9f18-46e03cd24517","html_url":"https://github.com/ashrawan/apache-poi-chart-sample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashrawan%2Fapache-poi-chart-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashrawan%2Fapache-poi-chart-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashrawan%2Fapache-poi-chart-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashrawan%2Fapache-poi-chart-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashrawan","download_url":"https://codeload.github.com/ashrawan/apache-poi-chart-sample/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248429118,"owners_count":21101785,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apache-poi","excel","excel-chart","java","poi","spring-boot","xlsx"],"created_at":"2024-11-09T19:23:02.370Z","updated_at":"2025-04-11T15:13:18.154Z","avatar_url":"https://github.com/ashrawan.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Excel Data and Charts\n\n### Dev ReadMe:  \nA Sample code showcasing how to work with ApachePOI Excel file and smart charts.  \n#### 1. Generic Chart module : Act as a generic chart library that plot charts with existing data values.\n#### 2. Playground:  Testing out some classes and functionality for ApachePOI chart.\n#### 3.. ExcelMainLibRunner : Shows a simple approach for using the chart module.\n\n## Introduction\nApache POI is a java library for working with Microsoft Office binary and OOXML file formats. Currently it has support for two formats OOXML and OLE2.\n\n- **OLE2**: Object Linking \u0026 Embedding (It’s Microsoft’s Compound Document format to work with Microsoft files such as XLS, DOC, PPT etc. It’s the legacy implementation based on the OLEObject that uses approach of linking and embedding documents and other objects.)\n- **OOXML**: Office Open XML (also informally known as OOXML is a new standards based XML file format in Microsoft Office 2007 and 2008. The file formats are such as. XLSX, DOCX, PPTX etc.)  \n\nGenerally, Apache POI has classified office documents using various API’s on the basis of following convention:\n- Spread Sheets: SS = H*SS*F + X*SS*F\n- Word Processing: WP = H*WP*F + X*WP*F\n- PowerPoint Presentations (Slideshow): SL = H*SL*F + X*SL*F\n\nNaming's used by POI:\n\n- Components named “**H??F**” are for reading or writing OLE2 binary formats. (known as HF - **H**orrible **F**ormat).\n- Components named “**X??F**” are for reading or writing OpenOffice XML (OOXML) formats. (known as XF – **X**ML **F**ormat).\n   - SXSSF (since 3.8-beta3) – is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited. e.g. SXSSFWorkbook, SXSSFSheet. SXSSF achieves its low memory footprint by limiting access to the rows that are within a sliding window, while XSSF gives access to all rows in the document.\n\n## Working with Excel in Java\n\n*Dependency*:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.apache.poi\u003c/groupId\u003e\n    \u003cartifactId\u003epoi-ooxml\u003c/artifactId\u003e\n    \u003cversion\u003e5.2.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n#### Important Classes:\n- **HSSF** – It’s implementation of the Excel ’97(-2007) file format. e.g. HSSFWorkbook, HSSFSheet. (HSSF – Horrible Spreadsheet Format)\n- **XSSF** – It’s implementation of the Excel 2007 OOXML (.xlsx) file format. e.g. XSSFWorkbook, XSSFSheet. (Open Office XML Spreadsheet Format).\n\nWe will be using XSSF interface and its implementation.\n\n#### Important packages:\nMost of the classes for working with simple excel are available in package “org.apache.poi.xssf.usermodel”.\nApache POI has also introduced new package *“org.apache.poi.xddf.usermodel”*.\n\nThis provides base classes, enums and standards for XSSF classes and chart implementation.\n\n### Supported Charts in Excel and Apache POI\nWe can view the charts supported in excel under “Recommended Charts” section.\n\nCurrently POI only supports this type of Excel charts:\n\n```java \npublic enum ChartTypes {\n   AREA,\n   AREA3D,\n   BAR,\n   BAR3D,\n   DOUGHNUT,\n   LINE,\n   LINE3D,\n   PIE,\n   PIE3D,\n   RADAR,\n   SCATTER,\n   SURFACE,\n   SURFACE3D\n}\n```\n\n## Creating and Working with charts in POI\nGeneral steps:\n1.\tCreate workbook and create sheet.\n2.\tAdd some data (Create a row and put some cells in it – later this cell value range will be used for chart creation)\n3.\tCreate Drawing Patriarch and define anchor position (on defined position - chart will be drawn)\n4.\tSteps – Chart creation\n      - Add and define legend position\n      - Create Axis and set Position\n      -\tDefine Data Source x axis, y-axis values. (CellRangeAddress)\n      -\tDefine Data Source values to be plotted on those axis. (CellRangeAddress)\n      -\tCreate Chart – ChartType, ChartAxis, ValueAxis\n      -\tAdd data series – axis values/category , values\n      -\tPlot chart\n\n#### 1. Create workbook and create sheet\n```java \nXSSFWorkbook wb = new XSSFWorkbook()\n\nXSSFSheet sheet = wb.createSheet(\"example_bar_chart\")\n```\n\n#### 2. Add some data\n```java\nfinal int NUM_OF_ROWS = 3;\nfinal int NUM_OF_COLUMNS = 10;\n\nRow row;\nCell cell;\n\nfor (int rowIndex = 0; rowIndex \u003c NUM_OF_ROWS; rowIndex++) {\nrow = sheet.createRow((short) rowIndex);\n\n\tfor (int colIndex = 0; colIndex \u003c NUM_OF_COLUMNS; colIndex++) {\n\t\tcell = row.createCell((short) colIndex);\n\t\tcell.setCellValue(colIndex * (rowIndex + 1.0)); // some random values\n\t}\n}\n```\n#### 3. Create Drawing Patriarch and define anchor position\n   Creates a new client anchor and sets the top-left and bottom-right coordinates of the anchor by cell references and offsets.\n   \n```java\nXSSFDrawing drawing = sheet.createDrawingPatriarch();\nXSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);\n```\n\nstart: 0, 5 (col, row)    i.e A, 5  \nend: 10, 15 (col, row) i.e J, 15\n\n\n#### 4. Steps – Chart creation\n`   XSSFChart chart = drawing.createChart(anchor); `\n\n- Add and define legend position:\n```java \nXDDFChartLegend legend = chart.getOrAddLegend();\n\nlegend.setPosition(LegendPosition.TOP_RIGHT);\n```\n\n- Create Axis and set Position:\n\n```java\n// Use a category axis for the bottom axis.\n\nXDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);\n\nbottomAxis.setTitle(\"x\");\n\nXDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);\n\nleftAxis.setTitle(\"f(x)\");\n\nleftAxis.setCrosses(AxisCrosses.AUTO_ZERO);\n// Define Data Source x axis / y-axis values. (CellRangeAddress):\nXDDFDataSource\u003cDouble\u003e xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));\n// Define Data Source values to be plotted on those axis. (CellRangeAddress):\nXDDFNumericalDataSource\u003cDouble\u003e ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));\n\nXDDFNumericalDataSource\u003cDouble\u003e ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));\n// Create Chart – ChartType, ChartAxis, ValueAxis:\nXDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);\n// Add data series – axis values/category , values:\nXDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs, ys1);\nseries1.setTitle(\"2x\", null);\n// Plot chart:\nchart.plot(data);\n```\n\n### Official Links\nhttps://poi.apache.org/  ( Official Apache POI website )  \nhttps://github.com/apache/poi ( Source Code – GitHub Mirror Repository )\n\n\n### Document Architecture, Testing and Debugging\n\n**1. Open Packaging Conventions (OPC) (ECMA-376 OpenXML)**\n- https://www.ecma-international.org/publications-and-standards/standards/ecma-376/\n- https://docs.microsoft.com/en-us/previous-versions/windows/desktop/opc/open-packaging-conventions-overview\n\n\n**2. Exploring “.xslx” file contents ( Parts and Relationships)**\n- http://officeopenxml.com/drwOverview.php\n\n### Overview on POI Implementation \n![Overview on POI abstract Implementation and low-level OOXML Implementation\n](docs/POIComp.png?raw=true)\n\n### For DEV:\nGo through the package `playground` to run and explore various implementations. \n\npackage `chart`, `excel`, and `runner` includes framework for a dynamic chart and data creation.\n\n## Feel Free to Contribute !\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashrawan%2Fapache-poi-chart-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashrawan%2Fapache-poi-chart-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashrawan%2Fapache-poi-chart-sample/lists"}