?? testgroupreport.java
字號(hào):
package test;
import java.io.*;
import java.sql.*;
import java.awt.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.*;
import com.lucaslee.report.*;
import com.lucaslee.report.model.*;
import com.lucaslee.report.model.Rectangle;
import com.lucaslee.report.model.Table;
import com.lucaslee.report.printer.*;
import com.lucaslee.report.grouparithmetic.*;
/**
*
* <p>Title: 生成行匯總報(bào)表例子。</p>
* <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() {
}
public static Connection getConn() throws Exception {
Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@t3:1521:ora",
"test", "test");
return con;
}
public static void getHTMLReport(Report report) throws Exception {
FileOutputStream fo = new FileOutputStream("group.html");
HTMLCss css = new HTMLCss();
css.setGroupTotal("BACKGROUND-COLOR: #d8e4f1; font: bold 12pt 隸書(shū);");
css.setHead("BACKGROUND-COLOR: #ffdead; font: bold 12pt 隸書(shū);");
css.setTotal("BACKGROUND-COLOR: #d8e4f1; font: bold 12pt 隸書(shū);");
css.setTitle("font: bold 18pt ;");
css.setData("font: 12pt");
new HTMLPrinter().print(report, css, fo);
fo.close();
}
public static void getPDFReport(Report report) throws Exception {
FileOutputStream fo = new FileOutputStream("group.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); //創(chuàng)建中文字體
Font FontChineseBold = new Font(bfChineseBold, 10, Font.NORMAL); //創(chuàng)建中文字體
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);
new PDFPrinter().print(report, css, fo);
fo.close();
}
public static void getCSVReport(Report report) throws Exception {
FileOutputStream fo = new FileOutputStream("group.csv");
new CSVPrinter().print(report, fo);
fo.close();
}
private static Table getTableByConn() throws Exception {
Table t = new Table();
Connection con = null;
t.setBorder(1);
try {
con = getConn();
Statement st = con.createStatement();
String sql = "select * from marketpriceview";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
TableRow tr = new TableRow();
for (int i = 0; i < 4; i++) {
tr.addCell(new TableCell(rs.getString(i + 1) + ""));
}
t.addRow(tr);
}
} finally {
if (con != null)
con.close();
}
return t;
}
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;
}
private static void setTitleFooter(Report report) throws ReportException {
/*****************設(shè)置標(biāo)題,腳注*********************/
Table headerTable = new Table();
int[] widths={20,60,20};
headerTable.setWidths(widths);
report.setHeaderTable(headerTable);
Table footerTable = new Table();
report.setFooterTable(footerTable);
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("中國(guó)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)計(jì)報(bào)表");
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("報(bào)表日期:2003-11-11至2003-11-16");
tc.setAlign(tc.ALIGN_CENTER);
tc = tr.getCell(2);
tc.setContent("單位:噸 元");
tc.setAlign(tc.ALIGN_RIGHT);
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);
}
public static void main(String[] args) throws Exception {
ReportManager rm = new ReportManager();
Report report = new Report();
ReportBody body = new ReportBody();
Table t = getTable();
body.setData(t);
report.setBody(body);
//合并相鄰?fù)祮卧? int[] cols = {
0, 1};
t = rm.mergeSameCells(t, cols, rm.COLUMN_ORIENTATION);
t = rm.split(t, cols);
//設(shè)置表格的屬性
t.setAlign(Rectangle.ALIGN_CENTER);
t.setWidth(75);
t.setBorder(1);
t.setBordercolor(new java.awt.Color(0x000000));
//進(jìn)行行統(tǒng)計(jì)
int[] totalCols = {
1, 2, 3};
rm.generateRowTotal(t, totalCols, true, new SumArithmetic());
//格式化數(shù)據(jù)
int[] formatCols = {
1, 2, 3};
t = rm.formatData(t, formatCols, new DefaultFormatter());
//*****************設(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銷售額"));
setTitleFooter(report);
//生成HTML格式報(bào)表
getHTMLReport(report);
//生成PDF格式報(bào)表
getPDFReport(report);
//生成CSV格式報(bào)表
getCSVReport(report);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -