?? testcrossreport.java
字號:
package test;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import java.awt.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
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.Table;
import com.lucaslee.report.model.crosstable.*;
import com.lucaslee.report.printer.*;
/**
* 生成交叉表報表例子。
* 從main方法開始執行。
* 注意:例子中對程序段的注釋,方法是:在程序段前面加注釋,在程序段后面注明段的結束.如:
* 前面://****xxxx***********
* 后面://****end xxxx*******
*
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company:Lucas-lee Soft </p>
* @author Lucas Lee
* @version 1.0
*/
public class TestCrossReport {
public TestCrossReport() {
}
/**
* 根據報表對象生成csv格式的報表.
* @param report 報表對象
* @throws Exception
*/
public static void getExcelReport(Report report) throws Exception {
//設置輸出的文件
FileOutputStream fo = new FileOutputStream("crossTab.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定義字體*****************
//***************設置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);
style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFont(fontNormal);
this.setCrossHeadHead(style);
this.setDefaultColumnWidth( (short) 10);
//***************end 設置EXCEL報表的樣式表******************
}
};
//執行EXCEL格式報表的輸出
new ExcelPrinter().print(report, css, fo);
//關閉輸出文件
fo.close();
System.out.println("生成Excel格式報表成功。");
}
/**
* 根據報表對象生成HTML格式的報表.
* @param report 報表對象
* @throws Exception
*/
public static void getHTMLReport(Report report) throws Exception {
//設置輸出的文件
FileOutputStream fo = new FileOutputStream("crossTab.html");
//***************設置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");
css.setCrossHeadHead("BACKGROUND-COLOR: #a68763; font: 9pt ");
//***************end 設置HTML報表的樣式表******************
//執行HTML格式報表的輸出
new HTMLPrinter().print(report, css, fo);
//關閉輸出文件
fo.close();
System.out.println("生成HTML格式報表成功。");
}
/**
* 根據報表對象生成pdf格式的報表.
* @param report 報表對象
* @throws Exception
*/
public static void getPDFReport(Report report) throws Exception {
//設置輸出的文件
FileOutputStream fo = new FileOutputStream("crossTab.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中文字體*****************
//****************設置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);
item = new PDFCssItem();
item.setFont(new Font(bfChinese, 7, Font.NORMAL));
item.setBackgroudColor(new Color(0xffdead));
css.setCrossHeadHead(item);
//****************end 設置pdf報表的樣式表*********************
//執行PDF格式報表的輸出
new PDFPrinter().print(report, css, fo);
//關閉輸出文件
fo.close();
System.out.println("生成PDF格式報表成功。");
}
/**
* 根據報表對象生成csv格式的報表.
* @param report 報表對象
* @throws Exception
*/
public static void getCSVReport(Report report) throws Exception {
//設置輸出的文件
FileOutputStream fo = new FileOutputStream("crossTab.csv");
//執行CSV格式報表的輸出
new CSVPrinter().print(report, fo);
//關閉輸出文件
fo.close();
System.out.println("生成CSV格式報表成功。");
}
/**
* 設置報表的頭部和尾部.
* @param report 要設置頭尾的報表對象.
* @throws ReportException
*/
private static void setTitleFooter(Report report) throws ReportException {
//*****************設置報表頭部*********************
Table headerTable = new Table();
//設置表格的寬度比例(百分比)
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("產品銷售統計報表");
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.setAlign(tc.ALIGN_RIGHT);
tc.setContent("單位:噸 元");
//*****************end 設置報表頭部*********************
//*****************設置報表尾部*********************
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");
tr.getCell(0).setAlign(tc.ALIGN_LEFT);
tr.getCell(1).setContent("審核人:xxx");
tr.getCell(1).setAlign(tc.ALIGN_CENTER);
tr.getCell(2).setContent("制表日期:xxx");
tr.getCell(2).setAlign(tc.ALIGN_RIGHT);
//*****************end 設置報表尾部*********************
}
/**
* 從xml文件獲得原始數據表格.注意其中使用的crossTabSample.xml應該設置為此文件所在的路徑.
* @return
* @throws Exception
*/
private static Table getTableByXML() throws Exception {
//xml文件路徑,應該設置為crossTabSample.xml所在的路徑
String fileName = "crossTabSample.xml";
Table t = new Table();
//用JAXP讀取xml文件
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().
parse(new File(fileName));
Element e = doc.getDocumentElement();
NodeList xmlTab = e.getChildNodes();
for (int i = 0; i < xmlTab.getLength(); i++) {
if (xmlTab.item(i).getNodeName().equals("Row")) {
TableRow tr = new TableRow();
NodeList xmlTR = xmlTab.item(i).getChildNodes();
for (int j = 0; j < xmlTR.getLength(); j++) {
if (xmlTR.item(j).getNodeName().equals("data")) {
TableCell tc = new TableCell();
tc.setContent(xmlTR.item(j).getFirstChild().getNodeValue());
tr.addCell(tc);
}
}
t.addRow(tr);
}
}
return t;
}
public static void main(String[] args) throws Exception {
//報表管理器
ReportManager rm = new ReportManager();
//待處理的原始數據表格對象
Table t = getTableByXML();
//************定義交叉表*************
HeadCol[] colH = {
new HeadCol(2, "廠家")};
HeadCol[] rowH = {
new HeadCol(0, "品種"), new HeadCol(1, "市場")};
Vector sort1 = new Vector();
sort1.add("本單位");
sort1.add("長江牌");
colH[0].setSortSeq(sort1);
CrossCol crs = new CrossCol(3, "銷售額", new SumArithmetic());
CrossTable crossTab = new CrossTable(colH, rowH, crs);
//************end 定義交叉表*************
//通過原始數據和交叉表的定義生成交叉表
t = rm.generateCrossTab(t, crossTab);
//進行行統計
t = rm.generateCrossTabRowTotal(t, crossTab, true,
new SumArithmetic());
//進行列統計
t = rm.generateCrossTabColTotal(t, crossTab, false, new SumArithmetic());
//格式化數據
t = rm.formatData(t, crossTab, new DefaultFormatter());
//****************定義報表的其他部分****************
Report report = new Report();
//將交叉表設為報表的主體
ReportBody body = new ReportBody();
body.setData(t);
report.setBody(body);
//設置報表的頭和尾兩部分
setTitleFooter(report);
//****************end 定義報表的其他部分************
//生成HTML格式報表
getHTMLReport(report);
//生成PDF格式報表
getPDFReport(report);
//生成CSV格式報表
getCSVReport(report);
//生成Excel格式報表
getExcelReport(report);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -