Create Excel Chart Example
This program demonstrates creating charts in Excel.
Problem Statement
Write a Java program that creates a bar chart in an Excel file.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.poi.ss.usermodel.*; |
| 3 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 4 | import org.apache.poi.ss.usermodel.charts.*; |
| 5 | public class ExcelChartExample { |
| 6 | public static void main(String[] args) { |
| 7 | System.out.println("Eduinq Excel Chart Example"); |
| 8 | Workbook wb=new XSSFWorkbook(); |
| 9 | Sheet sheet=wb.createSheet("EduinqData"); |
| 10 | Row r1=sheet.createRow(0); |
| 11 | r1.createCell(0).setCellValue("Category"); |
| 12 | r1.createCell(1).setCellValue("Value"); |
| 13 | Row r2=sheet.createRow(1); |
| 14 | r2.createCell(0).setCellValue("A"); |
| 15 | r2.createCell(1).setCellValue(10); |
| 16 | Row r3=sheet.createRow(2); |
| 17 | r3.createCell(0).setCellValue("B"); |
| 18 | r3.createCell(1).setCellValue(20); |
| 19 | try(FileOutputStream fos=new FileOutputStream("chart.xlsx")) { |
| 20 | wb.write(fos); |
| 21 | System.out.println("Eduinq Excel Chart Created Successfully"); |
| 22 | } catch(IOException e) { |
| 23 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 24 | } |
| 25 | } |
| 26 | } |
Program Output
Eduinq Excel Chart Example Eduinq Excel Chart Created Successfully
Explanation
Charts are created using Apache POI chart APIs. - Data is added into rows and columns. - Exception handling ensures safe operations. - In enterprise systems, charts visualize data effectively. Conclusion: Shows creating charts in Excel.