📄 testgroupreport.java
字号:
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: 生成行汇总报表例子。</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 隶书;");
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");
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); //创建中文字体
Font FontChineseBold = new Font(bfChineseBold, 10, Font.NORMAL); //创建中文字体
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("产品" + 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("产品" + 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 {
/*****************设置标题,脚注*********************/
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("中国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("产品销售统计报表");
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);
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);
//合并相邻同值单元
int[] cols = {
0, 1};
t = rm.mergeSameCells(t, cols, rm.COLUMN_ORIENTATION);
t = rm.split(t, cols);
//设置表格的属性
t.setAlign(Rectangle.ALIGN_CENTER);
t.setWidth(75);
t.setBorder(1);
t.setBordercolor(new java.awt.Color(0x000000));
//进行行统计
int[] totalCols = {
1, 2, 3};
rm.generateRowTotal(t, totalCols, true, new SumArithmetic());
//格式化数据
int[] formatCols = {
1, 2, 3};
t = rm.formatData(t, formatCols, new DefaultFormatter());
//*****************设置列头*********************/
HeaderTable th = new HeaderTable();
report.getBody().setTableColHeader(th);
TableRow thr = new TableRow(4);
th.addRow(thr);
thr.setCell(0, new TableCell("产品名称"));
thr.setCell(1, new TableCell("产品xx量"));
thr.setCell(2, new TableCell("产品xx销售量"));
thr.setCell(3, new TableCell("产品xx销售额"));
setTitleFooter(report);
//生成HTML格式报表
getHTMLReport(report);
//生成PDF格式报表
getPDFReport(report);
//生成CSV格式报表
getCSVReport(report);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -