field trip #22 creating an excel spreadsheet with java by keith lynn
TRANSCRIPT
Field Trip #22
Creating an Excel Spreadsheet with JavaBy Keith Lynn
Java Frame
To contain the contents of the Excel document, we will create a JFrame
A JFrame is a graphical interface It can contains components that hold the data
of the spreadsheet It can also contain a menu bar which gives an
interface to the user
LayoutManager
When we create a JFrame, we can specify how components will be layed out
There are built-in layout managers like FlowLayout which will arrange components in a arow and GridLayout which will arrange components in a grid
We set a layout with setLayout
JMenuItem
We can create a JMenuItem and add it to a JMenu
We can add the JMenu to a JMenuBar We can display the JMenuBar with the method
setMenuBar The Menus appear in the upper left corner
Handling Events
When we select a menu item, it creates an event
In order to detect and act on the event, we attach an ActionListener to the JMenuItem
ActionListener is an interface in the java.awt.event package that contains the method actionPerformed
Interfaces
An interface is a special type of Java program that lists methods
In order to use the interface, we must implement all of the methods in the interface
ActionListener is found in the java.awt.event package
When we attach an ActionListener to a JmenuItem, and select the item, the actionPerformed method is called
We can determine the label of the JmenuItem, we use the method getActionCommand()
Packages
Packages are collections of related classes In order to use the classes in a package, we
can import them into our class It is not necessary to import packages We can use the fully qualified name of the class This is necessary if there are two packages we
are using and there is a class in each package with the same name
Note the import statement is not recursive
CLASSPATH
The CLASSPATH is an environment variable that determines what directories are searched to find a class
Jar files are archive files that contain Java classes
If we want to include a class from a jar file in our class, we place the absolute path of the jar file in the CLASSPATH
POI POI, Poor Obfuscation Interface, is a project
that aims to provide a Java API to read various file formats based on the Office Open XML Standard
In order to read and write Excel documents, we can import the org.apache.poi.xssf.usermodel package
In order to open an Excel document, we create a FileInputStream to open the document
We create an instance of an XSSFWorkbook using the FileInputStream
XSSFWorkbook represents the entire document
Sheets
The XSSFWorkbook is made up of one or more XSSFSheet objects
Each XSSFSheet represents a worksheet The XSSFSheet is composed of XSSFRow
objects Each XSSFRow object is composed of
XSSFCell objects
JTable
To display the information on the JFrame, we will create an instance of a JTable
In order to create the JTable, we need to specify a one dimensional array containing column names and a two dimensional array containing the data
We then access the data of the worksheet and place that information in the JTable
Creating a Chart
To create a new chart, we create a new instance of an XSSFWorkbook
We then create a new XSSFSheet When then create rows and place data in cells We can create a new cell and set its value with
setCellValue
Writing the Spreadsheet
In order to write the spreadsheet, we create an instance of a FileOutputStream attached to a file
After we create the XSSFWorkbook, we call the write method and send the FileOutputStream