Current location - Quotes Website - Personality signature - How to generate reports (Excel files) using java
How to generate reports (Excel files) using java

This is an example of exporting data from Tabel to Excel: you can find the jxl.jar package online. There are many resources. If you can’t find it, you can also leave an email and I will send it to you

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util .Date;

import javax.swing.JOptionPane;

import javax.swing.JTable;

import jxl.Workbook;

import jxl.format.Alignment;

import jxl.format.Colour;

import jxl.format.UnderlineStyle;

import jxl.format.VerticalAlignment;< /p>

import jxl.read.biff.BiffException;

import jxl.write.Label;

import jxl.write.WritableCellFormat;

import jxl.write.WritableFont;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;< /p>

public class TableToExcel {

public static void export(File file,String heading,String note,JTable table) {

WritableWorkbook workbook = null;// Create Workbook

try {

if(file.exists()) {//If the file exists

workbook = Workbook.createWorkbook(file, Workbook.getWorkbook (file));

} else {//If the file does not exist

workbook = Workbook.createWorkbook(file);

}

< p> // Create a worksheet

WritableSheet sheet = workbook.createSheet(heading, workbook.getNumberOfSheets());

//Get the number of rows and columns of jtable

int rowNum = table.getRowCount();

int colNum = table.getColumnCount();

fillHeader(sheet,heading,colNum);//Fill in the main title

fillColName(sheet,table,colNum);//Fill in the column name

fillCell(sheet,table,colNum,rowNum);//Fill in the data

fillNote(sheet,note,colNum,rowNum);//Fill in the signature file

workbook.write();//Write data

workbook.close();//Close

} catch (IOException e) {

JOptionPane.showMessageDialog(null, "Please close the worksheet before importing data");

} catch (BiffException e ) {

e.printStackTrace();

} catch (WriteException e) {

e.printStackTrace();

}

}

/**

* Fill in the table title

* @param sheet

* @param heading

* @param colNum

* @throws WriteException

*/

private static void fillHeader(WritableSheet sheet,String heading, int colNum) throws WriteException {

WritableFont font = new WritableFont(WritableFont.ARIAL, 18, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);//Set the font

WritableCellFormat format = new WritableCellFormat(font);//New formatting object

format.setAlignment(Alignment.CENTRE);//Set horizontal center alignment

format.setVerticalAlignment(VerticalAlignment .CENTRE);//Set vertical center alignment

sheet.mergeCells(0,0, colNum-1, 0);//Merge cells

sheet.setRowView(0, 600); //Set the row height

sheet.addCell(new Label(0,0,heading,format));//Fill in the main title

}

< p> /**

* Fill in the column name

* @param sheet

* @param table

* @param colNum< /p>

* @throws WriteException

*/

private static void fillColName(WritableSheet sheet,JTable table,int colNum) throws WriteException {

WritableFont font = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//Set the font

WritableCellFormat format = new WritableCellFormat(font);//New formatting object

format.setAlignment(Alignment.CENTRE);//Set horizontal center alignment

sheet.setColumnView(0, 15);//Set column width

for(int col = 0; col < colNum;col++) {

Label colName = new Label(col,1,table.getModel().getColumnName(col),format);

sheet.addCell(colName );

}

}

/**

* Fill in the form data to excel

* @ param sheet

* @param table

* @param colNum

* @param rowNum

* @throws WriteException

*/

private static void fillCell(WritableSheet sheet,JTable table,int colNum,int rowNum) throws WriteException {

WritableFont font = new WritableFont(WritableFont.ARIAL,12 ,WritableFont.NO_BOLD);//Set font

WritableCellFormat format = new WritableCellFormat(font);//New formatting object

format.setAlignment(Alignment.CENTRE);// Set horizontal centering

for(int col = 0;col < colNum;col++) {

for(int row = 1;row <= rowNum -1;row++) {// Fill in the data

String value = "";

if(table.getModel().getValueAt(row -1, col) != null)

value = table.getModel().getValueAt(row -1, col).toString();

sheet.addCell(new Label(col,row + 1,value));

}

}

}

/**

* Fill in the signature file

* @param sheet

* @param inscribe

* @param colNum

* @param rowNum

* @throws WriteException

*/

private static void fillNote(WritableSheet sheet,String inscribe,int colNum,int rowNum) throws WriteException {

if( inscribe == null || inscribe.length() < 1 ) {

inscribe = "Export time: "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

}

WritableFont font = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,

false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);//Define font

WritableCellFormat format = new WritableCellFormat(font);//Define formatting object

format.setAlignment(Alignment.RIGHT);//Horizontal center display

sheet.mergeCells (0, rowNum+3, colNum - 1, rowNum+3);//Merge cells

sheet.addCell(new Label(0, rowNum+3, inscribe, format));//Fill in Worksheet

}

}