private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object[] values = new Object[jlValues.getModel().getSize() + 1]; // creates an array that holds the values in the JList
for (int i = 0; i < values.length; i++) { // retreive all the pre-existing values in the JList
if (i != values.length - 1) { // make sure you get add the inputted text at the end
values[i] = jlValues.getModel().getElementAt(i);
} else {
values[i] = txtInput.getText(); // sets the inputted text at the end of the list
txtInput.setText(""); // clears the text box to allow for faster entry
}
}
jlValues.setListData(values); // set the values to the array that holds the previous values plus the new entry
}
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
public class GUI extends javax.swing.JFrame {
.....
Now we are ready to write the code that actually creates the Excel Spreadsheet with the values from the database. Double-click on the "Export to Excel" button and add the code snippets by following these next set of steps:
Code to Create the Excel Spreadsheet
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper(); // allows us to create cell values that are String rather than simply numbers
Sheet sheet = wb.createSheet("Values"); // name the sheet whatever you like
// Loop through the values in the JList and create the cells for them and the counter
for (int i = 0; i < this.jlValues.getModel().getSize(); i++) {
Row row = sheet.createRow((short) i); // creates a new row for each value in the JList
// creates a cellStyle for centering the values in the cells
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// create the counter and put it in the column A
Cell counterCell = row.createCell(0); // 0 = column A
counterCell.setCellValue(i + 1); // the counter is not zero based
counterCell.setCellStyle(cellStyle); // uses the center-aligned cell style
// create the cells that hold the values in the JList and put them in column B
Cell valueCell = row.createCell(1); // 1 = column B
valueCell.setCellValue(createHelper.createRichTextString(this.jlValues.getModel().getElementAt(i).toString()));
valueCell.setCellStyle(cellStyle); // uses the center-aligned cell style
}
try {
java.io.FileOutputStream fileOut = new java.io.FileOutputStream("src/values.xls"); // creates the Excel file and places it in the <default package>
wb.write(fileOut);
fileOut.close(); // ends writing the Stream
java.awt.Desktop.getDesktop().open(new java.io.File("src/values.xls")); //opens the Excel Spreadsheet for viewing
} catch (java.io.FileNotFoundException fnfe) {
javax.swing.JOptionPane.showMessageDialog(this, "The Excel Spreadsheet cannot be found :(");
} catch (java.io.IOException ioe) {
javax.swing.JOptionPane.showMessageDialog(this, "There was an error creating the Excel Spreadsheet :(");
}
That's it! You've created an Excel Spreadsheet with your Java Application!!!
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)