?? testgroupreport.java
字號:
package test;
import java.io.*;
import java.awt.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.*;
import com.lucaslee.report.*;
import com.lucaslee.report.grouparithmetic.*;
import com.lucaslee.report.model.*;
import com.lucaslee.report.model.Rectangle;
import com.lucaslee.report.model.Table;
import com.lucaslee.report.printer.*;
/**
* 生成行匯總報表例子。
* 從main方法開始執(zhí)行。
* 注意:例子中對程序段的注釋,方法是:在程序段前面加注釋,在程序段后面注明段的結(jié)束.如:
* 前面://****xxxx***********
* 后面://****end xxxx*******
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company:Lucas-lee Soft </p>
* @author Lucas Lee
* @version 1.0
*/
public class TestGroupReport {
public TestGroupReport() {
}
/**
* 根據(jù)報表對象生成excel格式的報表.
* @param report 報表對象
* @throws Exception
*/
public static void getExcelReport(Report report) throws Exception {
//設(shè)置輸出的文件
FileOutputStream fo = new FileOutputStream("group.xls");
ExcelCss css = new ExcelCss() {
public void init(HSSFWorkbook workbook) {
//*****************定義字體*****************
//普通字體
HSSFFont fontNormal = workbook.createFont();
fontNormal.setFontHeightInPoints( (short)10);
fontNormal.setFontName("宋體");
//粗體
HSSFFont fontBold = workbook.createFont();
fontBold.setFontHeightInPoints( (short)10);
fontBold.setFontName("宋體");
fontBold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//大、粗字體
HSSFFont fontBig = workbook.createFont();
fontBig.setFontHeightInPoints( (short) 15);
fontBig.setFontName("宋體");
fontBig.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//*****************end定義字體*****************
//***************設(shè)置EXCEL報表的樣式表******************
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFont(fontNormal);
this.setGroupTotal(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFont(fontNormal);
this.setGroupTotal(style);
this.setTotal(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFont(fontBold);
this.setHead(style);
style =workbook.createCellStyle();
style.setFont(fontBig);
this.setTitle(style);
style = workbook.createCellStyle();
style.setFont(fontNormal);
this.setData(style);
this.setDefaultColumnWidth( (short) 20);
//***************end 設(shè)置EXCEL報表的樣式表******************
}
};
//執(zhí)行EXCEL格式報表的輸出
new ExcelPrinter().print(report, css, fo);
//關(guān)閉輸出文件
fo.close();
System.out.println("生成Excel格式報表成功。");
}
public static void getHTMLReport(Report report) throws Exception {
//設(shè)置輸出的文件
FileOutputStream fo = new FileOutputStream("group.html");
//***************設(shè)置HTML報表的樣式表******************
HTMLCss css = new HTMLCss();
css.setGroupTotal("BACKGROUND-COLOR: #d8e4f1; font: bold 12pt 隸書;");
css.setHead("BACKGROUND-COLOR: #ffdead; font: bold 12pt 隸書;");
css.setTotal("BACKGROUND-COLOR: #d8e4f1; font: bold 12pt 隸書;");
css.setTitle("font: bold 18pt ;");
css.setData("font: 12pt");
//***************end 設(shè)置HTML報表的樣式表******************
//執(zhí)行HTML格式報表的輸出
new HTMLPrinter().print(report, css, fo);
//關(guān)閉輸出文件
fo.close();
System.out.println("生成HTML格式報表成功。");
}
public static void getPDFReport(Report report) throws Exception {
//設(shè)置輸出的文件
FileOutputStream fo = new FileOutputStream("group.pdf");
//******************定義pdf中文字體*****************
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
BaseFont bfChineseBold = BaseFont.createFont("STSong-Light,Bold",
"UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 10, Font.NORMAL);
Font FontChineseBold = new Font(bfChineseBold, 10, Font.NORMAL);
//******************end 定義pdf中文字體*****************
//****************設(shè)置pdf報表的樣式表*********************
PDFCss css = new PDFCss();
PDFCssItem item = new PDFCssItem();
item.setBackgroudColor(new Color(0xd8e4f1));
item.setFont(FontChinese);
css.setGroupTotal(item);
css.setTotal(item);
item = new PDFCssItem();
item.setBackgroudColor(new Color(0xffdead));
item.setFont(FontChineseBold);
css.setHead(item);
item = new PDFCssItem();
item.setFont(new Font(bfChineseBold, 15, Font.BOLD));
css.setTitle(item);
item = new PDFCssItem();
item.setFont(new Font(bfChinese, 10, Font.NORMAL));
css.setData(item);
//****************end 設(shè)置pdf報表的樣式表*********************
//執(zhí)行PDF格式報表的輸出
new PDFPrinter().print(report, css, fo);
//關(guān)閉輸出文件
fo.close();
System.out.println("生成PDF格式報表成功。");
}
/**
* 根據(jù)報表對象生成csv格式的報表.
* @param report 報表對象
* @throws Exception
*/
public static void getCSVReport(Report report) throws Exception {
//設(shè)置輸出的文件
FileOutputStream fo = new FileOutputStream("group.csv");
//執(zhí)行CSV格式報表的輸出
new CSVPrinter().print(report, fo);
//關(guān)閉輸出文件
fo.close();
System.out.println("生成CSV格式報表成功。");
}
/**
* 獲得原始數(shù)據(jù)表格.生成一個例子數(shù)據(jù)表格.
* @return
* @throws Exception
*/
public static Table getTable() throws Exception {
Table t = new Table();
double multip = 100.00;
for (int i = 0; i < 9; i++) {
TableRow tr = new TableRow();
tr.addCell(new TableCell("產(chǎn)品" + i));
tr.addCell(new TableCell("" + (i * multip)));
tr.addCell(new TableCell("" + (i + 1) * multip));
tr.addCell(new TableCell("" + (i + 2) * multip));
t.addRow(tr);
t.addRow(tr.cloneAll());
tr = new TableRow();
tr.addCell(new TableCell("產(chǎn)品" + i));
tr.addCell(new TableCell("" + (i + 1) * multip));
tr.addCell(new TableCell("" + (i + 2) * multip));
tr.addCell(new TableCell("" + (i + 2) * multip));
t.addRow(tr);
}
for (int i = 0; i < 0; i++) {
t.addCol(t.getCol(3).cloneAll());
}
return t;
}
/**
* 為報表設(shè)置報表頭和尾.
* @param report 要設(shè)置的報表
* @throws ReportException
*/
private static void setTitleFooter(Report report) throws ReportException {
//*****************設(shè)置報表頭*********************
Table headerTable = new Table();
//設(shè)置表格的寬度比例(百分比)
int[] widths = {
20, 60, 20};
headerTable.setWidths(widths);
report.setHeaderTable(headerTable);
headerTable.setBorder(0);
headerTable.setAlign(headerTable.ALIGN_CENTER);
TableCell tc = null;
TableRow tr = null;
tr = new TableRow(3);
headerTable.addRow(tr);
tc = tr.getCell(0);
tc.setColSpan(3);
tc.setAlign(tc.ALIGN_CENTER);
tc.setContent("中國XXX股份有限公司XXX分公司");
tr.getCell(1).setIsHidden(true);
tr.getCell(2).setIsHidden(true);
tr = new TableRow(3);
headerTable.addRow(tr);
tc = tr.getCell(0);
tc.setColSpan(3);
tc.setAlign(tc.ALIGN_CENTER);
tc.setContent("產(chǎn)品銷售統(tǒng)計報表");
tc.setCssClass(Report.TITLE_TYPE);
tr.getCell(1).setIsHidden(true);
tr.getCell(2).setIsHidden(true);
tr = new TableRow(3);
headerTable.addRow(tr);
tr = new TableRow(3);
headerTable.addRow(tr);
tc = tr.getCell(0);
tc.setContent("單位:xxx分公司");
tc.setAlign(tc.ALIGN_LEFT);
tc = tr.getCell(1);
tc.setContent("日期:2003-11-11至2003-11-16");
tc.setAlign(tc.ALIGN_CENTER);
tc = tr.getCell(2);
tc.setContent("單位:噸 元");
tc.setAlign(tc.ALIGN_RIGHT);
//*****************end 設(shè)置報表頭*********************
//*****************設(shè)置報表尾*********************
Table footerTable = new Table();
report.setFooterTable(footerTable);
tr = new TableRow(3);
footerTable.setBorder(0);
footerTable.setAlign(footerTable.ALIGN_CENTER);
footerTable.addRow(tr);
tr.getCell(0).setContent("制表人:xxx");
tc.setAlign(tc.ALIGN_LEFT);
tr.getCell(1).setContent("審核人:xxx");
tc.setAlign(tc.ALIGN_CENTER);
tr.getCell(2).setContent("制表日期:xxx");
tc.setAlign(tc.ALIGN_RIGHT);
//*****************end 設(shè)置報表尾*********************
}
public static void main(String[] args) throws Exception {
//報表管理器
ReportManager rm = new ReportManager();
//獲得原始數(shù)據(jù)表格
Table t = getTable();
//定義報表對象
Report report = new Report();
//**************設(shè)置報表主體部分**************
ReportBody body = new ReportBody();
body.setData(t);
report.setBody(body);
//**************end 設(shè)置報表主體部分**************
//***********按指定列分組**********
int[] cols = {
0, 1};
//合并列中相鄰的同值單元
t = rm.mergeSameCells(t, cols, rm.COLUMN_ORIENTATION);
//按列的先后順序,完成分組
t = rm.split(t, cols);
//***********end 按指定列分組**********
//*****************設(shè)置表格的屬性********************
t.setAlign(Rectangle.ALIGN_CENTER);
t.setWidth(75);
t.setBorder(1);
t.setBordercolor(new java.awt.Color(0x000000));
//*****************end 設(shè)置表格的屬性********************
//********************進(jìn)行行統(tǒng)計*********************
int[] totalCols = {
1, 2, 3};
rm.generateRowTotal(t, totalCols, true, new SumArithmetic());
//********************end 進(jìn)行行統(tǒng)計*********************
//*********************格式化數(shù)據(jù)**********************
int[] formatCols = {
1, 2, 3};
t = rm.formatData(t, formatCols, new DefaultFormatter());
//*********************end 格式化數(shù)據(jù)**********************
//*****************設(shè)置報表主體表格的列頭*********************/
HeaderTable th = new HeaderTable();
report.getBody().setTableColHeader(th);
TableRow thr = new TableRow(4);
th.addRow(thr);
thr.setCell(0, new TableCell("產(chǎn)品名稱"));
thr.setCell(1, new TableCell("產(chǎn)品xx量"));
thr.setCell(2, new TableCell("產(chǎn)品xx銷售量"));
thr.setCell(3, new TableCell("產(chǎn)品xx銷售額"));
//*****************end 設(shè)置報表主體表格的列頭*********************/
//設(shè)置報表的頭和尾
setTitleFooter(report);
//生成HTML格式報表
getHTMLReport(report);
//生成PDF格式報表
getPDFReport(report);
//生成CSV格式報表
getCSVReport(report);
//生成Excel格式報表
getExcelReport(report);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -