利用java怎么实现生成报表(Excel文件)
1个回答
展开全部
这是从Tabel导出数据到Excel的一个例子:jxl.jar包你可以去网上找,有很多资源,如果没找到,也可以留个邮箱,我发给你
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;
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;
public class TableToExcel {
public static void export(File file,String heading,String note,JTable table) {
WritableWorkbook workbook = null;// 创建工作薄
try {
if(file.exists()) {//如果文件存在
workbook = Workbook.createWorkbook(file, Workbook.getWorkbook(file));
} else {//如果文件不存在
workbook = Workbook.createWorkbook(file);
}
// 创建工作表
WritableSheet sheet = workbook.createSheet(heading, workbook.getNumberOfSheets());
//获取jtable 的行数和列数
int rowNum = table.getRowCount();
int colNum = table.getColumnCount();
fillHeader(sheet,heading,colNum);//填写主标题
fillColName(sheet,table,colNum);//填写列名
fillCell(sheet,table,colNum,rowNum);// 填写数据
fillNote(sheet,note,colNum,rowNum);//填写签名档
workbook.write();//写入数据
workbook.close();//关闭
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "导入数据前请关闭工作表");
} catch (BiffException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/**
* 填写 表标题
* @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);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
format.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直居中对齐
sheet.mergeCells(0,0, colNum-1, 0);//合并单元格
sheet.setRowView(0, 600); // 设置行高
sheet.addCell(new Label(0,0,heading,format));//填写主标题
}
/**
* 填写列名
* @param sheet
* @param table
* @param colNum
* @throws WriteException
*/
private static void fillColName(WritableSheet sheet,JTable table,int colNum) throws WriteException {
WritableFont font = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
sheet.setColumnView(0, 15);// 设置列宽
for(int col = 0; col < colNum;col++) {
Label colName = new Label(col,1,table.getModel().getColumnName(col),format);
sheet.addCell(colName);
}
}
/**
* 填写表格数据到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);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中
for(int col = 0;col < colNum;col++) {
for(int row = 1;row <= rowNum -1;row++) {//填写数据
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));
}
}
}
/**
* 填写 签名档
* @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 = "导出时间: "+ 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);// 定义字体
WritableCellFormat format = new WritableCellFormat(font);// 定义格式化对象
format.setAlignment(Alignment.RIGHT);// 水平居中显示
sheet.mergeCells(0, rowNum+3, colNum - 1, rowNum+3);// 合并单元格
sheet.addCell(new Label(0, rowNum+3, inscribe, format));// 填写工作表
}
}
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;
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;
public class TableToExcel {
public static void export(File file,String heading,String note,JTable table) {
WritableWorkbook workbook = null;// 创建工作薄
try {
if(file.exists()) {//如果文件存在
workbook = Workbook.createWorkbook(file, Workbook.getWorkbook(file));
} else {//如果文件不存在
workbook = Workbook.createWorkbook(file);
}
// 创建工作表
WritableSheet sheet = workbook.createSheet(heading, workbook.getNumberOfSheets());
//获取jtable 的行数和列数
int rowNum = table.getRowCount();
int colNum = table.getColumnCount();
fillHeader(sheet,heading,colNum);//填写主标题
fillColName(sheet,table,colNum);//填写列名
fillCell(sheet,table,colNum,rowNum);// 填写数据
fillNote(sheet,note,colNum,rowNum);//填写签名档
workbook.write();//写入数据
workbook.close();//关闭
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "导入数据前请关闭工作表");
} catch (BiffException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/**
* 填写 表标题
* @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);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
format.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直居中对齐
sheet.mergeCells(0,0, colNum-1, 0);//合并单元格
sheet.setRowView(0, 600); // 设置行高
sheet.addCell(new Label(0,0,heading,format));//填写主标题
}
/**
* 填写列名
* @param sheet
* @param table
* @param colNum
* @throws WriteException
*/
private static void fillColName(WritableSheet sheet,JTable table,int colNum) throws WriteException {
WritableFont font = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
sheet.setColumnView(0, 15);// 设置列宽
for(int col = 0; col < colNum;col++) {
Label colName = new Label(col,1,table.getModel().getColumnName(col),format);
sheet.addCell(colName);
}
}
/**
* 填写表格数据到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);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中
for(int col = 0;col < colNum;col++) {
for(int row = 1;row <= rowNum -1;row++) {//填写数据
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));
}
}
}
/**
* 填写 签名档
* @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 = "导出时间: "+ 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);// 定义字体
WritableCellFormat format = new WritableCellFormat(font);// 定义格式化对象
format.setAlignment(Alignment.RIGHT);// 水平居中显示
sheet.mergeCells(0, rowNum+3, colNum - 1, rowNum+3);// 合并单元格
sheet.addCell(new Label(0, rowNum+3, inscribe, format));// 填写工作表
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询