📄 reportgeneration.java
字号:
int g_x = 0;
int g_y = 0;
// 分组头,包含列头
JRDesignBand band = new JRDesignBand();
band.setHeight(HEAD_COLUMN_HEIGHT );
JRDesignTextField textField = new JRDesignTextField();
textField.setX(g_x);
textField.setY(g_y);
textField.setWidth(columnWidth);
textField.setHeight(HEAD_COLUMN_HEIGHT);
textField.setMode(JRElement.MODE_OPAQUE);
textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
textField.setStyle(NORMAL_BOLD_STYLE);
expression = new JRDesignExpression();
expression.setValueClass(java.lang.String.class);
expression.setText("$F{" + groupString + "}");
textField.setExpression(expression);
band.addElement(textField);
/**
* 2007-08-15导出到excel时,表头不要了
*/
// createColumnHead(band, 0, HEAD_COLUMN_HEIGHT);
group.setGroupHeader(band);
// 分组角
band = new JRDesignBand();
band.setHeight(HEAD_COLUMN_HEIGHT);
for (int i = 0; i < columns.length; i++) {
RptColumn rptColumn = (RptColumn) columns[i];
String columnCode = rptColumn.getColumnCode();
textField = new JRDesignTextField();
textField.setX(g_x);
textField.setY(g_y);
textField.setWidth(rptColumn.getColumnWidth());
textField.setHeight(HEAD_COLUMN_HEIGHT);
textField
.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
textField.setStyle(NORMAL_BOLD_STYLE);
textField.setBlankWhenNull(false);
expression = new JRDesignExpression();
if (summaryVariables.containsKey(columnCode)) {
expression.setValueClass(java.math.BigDecimal.class);
expression.setText("$V{GROUP_SUMMARY_" + columnCode + "}");
}else{
expression.setValueClass(java.lang.String.class);
String strColumnSummary = "";
if (i == 0) {
strColumnSummary = "合计";
}
expression.setText("\"" + strColumnSummary + "\"");
textField
.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
}
g_x = g_x + rptColumn.getColumnWidth();
textField.setExpression(expression);
textField.setBorder(new Byte("1"));
textField.setBorderColor(new Color(0, 0, 0));
textField.setBlankWhenNull(true);
band.addElement(textField);
}
group.setGroupFooter(band);
jasperDesign.addGroup(group);
}
}
private void createSummary() {
JRDesignBand band = new JRDesignBand();
band.setHeight(HEAD_COLUMN_HEIGHT);
int s_x = 0;
int s_y = 0;
for (int i = 0; i < columns.length; i++) {
RptColumn rptColumn = (RptColumn) columns[i];
String columnCode = rptColumn.getColumnCode();
JRDesignTextField textField = new JRDesignTextField();
textField.setX(s_x);
textField.setY(s_y);
textField.setWidth(rptColumn.getColumnWidth());
textField.setHeight(HEAD_COLUMN_HEIGHT);
JRDesignExpression expression = new JRDesignExpression();
textField.setStyle(NORMAL_BOLD_STYLE);
if (summaryVariables != null
&& summaryVariables.containsKey(columnCode)) {
expression.setValueClass(java.math.BigDecimal.class);
expression.setText("$V{SUMMARY_" + columnCode + "}");
textField
.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
} else {
expression.setValueClass(java.lang.String.class);
String strColumnSummary = rptColumn.getColumnSummary() == null ? ""
: rptColumn.getColumnSummary();
if (i == 0) {
strColumnSummary = "总计";
}
expression.setText("\"" + strColumnSummary + "\"");
textField
.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
}
textField.setExpression(expression);
textField.setBorder(new Byte("1"));
textField.setBorderColor(new Color(0, 0, 0));
textField.setBlankWhenNull(true);
band.addElement(textField);
s_x = s_x + rptColumn.getColumnWidth();
}
jasperDesign.setSummary(band);
}
private void createFieldsAndVariables() throws JRException {
// Fields And Variables
if (rptMain.getRptGroupby() != null
&& !rptMain.getRptGroupby().equals("")) {
JRDesignField field = new JRDesignField();
field.setName(rptMain.getRptGroupby());
field.setValueClass(java.lang.String.class);
jasperDesign.addField(field);
}
for (int i = 0; i < columns.length; i++) {
RptColumn rptColumn = (RptColumn) columns[i];
JRDesignField field = new JRDesignField();
field.setName(rptColumn.getColumnCode());
field.setValueClass(java.lang.String.class);
JRDesignVariable variable = null;
String columnSummary = rptColumn.getColumnSummary();
if ((rptColumn.getColumnType() != null && (rptColumn
.getColumnType().equalsIgnoreCase("integer")
|| rptColumn.getColumnType().equalsIgnoreCase("int") || rptColumn
.getColumnType().equalsIgnoreCase("number")))
&& (columnSummary != null && (columnSummary
.equalsIgnoreCase("SUM")
|| columnSummary.equalsIgnoreCase("AVG") || columnSummary
.equalsIgnoreCase("COUNT")))) {
variable = new JRDesignVariable();
variable.setName("SUMMARY_" + rptColumn.getColumnCode());
variable.setValueClass(java.math.BigDecimal.class);
variable.setResetType(JRVariable.RESET_TYPE_REPORT);
if (columnSummary.equalsIgnoreCase("SUM")) {
variable.setCalculation(JRVariable.CALCULATION_SUM);
} else if (columnSummary.equalsIgnoreCase("AVG")) {
variable.setCalculation(JRVariable.CALCULATION_AVERAGE);
} else if (columnSummary.equalsIgnoreCase("COUNT")) {
variable.setCalculation(JRVariable.CALCULATION_COUNT);
}
JRDesignExpression expression;
expression = new JRDesignExpression();
expression.setValueClass(java.math.BigDecimal.class);
expression.setText("new java.math.BigDecimal($F{"
+ rptColumn.getColumnCode() + "}==null?\"0\":$F{"
+ rptColumn.getColumnCode() + "})");
variable.setExpression(expression);
if (summaryVariables == null) {
summaryVariables = new HashMap();
summaryVariables.put(rptColumn.getColumnCode(), variable);
} else {
summaryVariables.put(rptColumn.getColumnCode(), variable);
}
}
try {
jasperDesign.addField(field);
if (variable != null) {
jasperDesign.addVariable(variable);
}
} catch (JRException e) {
throw new RPTDesignException("报表设计:创建field 和 variable出错");
}
}
}
private void setQuery() throws RPTDesignException {
JRDesignQuery query = new JRDesignQuery();
if (rptMain.getRptScript() == null
|| rptMain.getRptScript().equalsIgnoreCase("")) {
throw new RPTDesignException("报表设置:角本有问题");
} else {
query.setText(rptMain.getRptScript());
jasperDesign.setQuery(query);
}
}
private void setFont() throws JRException {
jasperDesign.addStyle(NORMAL_STYLE);
jasperDesign.addStyle(NORMAL_BOLD_STYLE);
jasperDesign.addStyle(TITLE_STYLE);
jasperDesign.addStyle(TITLE_BOLB_STYLE);
jasperDesign.setDefaultStyle(NORMAL_STYLE);
}
private void createPage() {
int columnWidths[] = new int[columns.length];
for (int i = 0; i < columns.length; i++) {
RptColumn column = (RptColumn) columns[i];
columnWidths[i] = column.getColumnWidth() == 0 ? 30 : column
.getColumnWidth();
}
// 报表居中
for (int i = 0; i < columnWidths.length; i++) {
columnWidth = columnWidth + columnWidths[i];
}
JasperPageSize pageSize = new JasperPageSize(rptMain.getRptPage(),
rptMain.getRptDirection());
int leftMargin = 0;
int rightMargin = 0;
if (rptMain.getRptExpfileType() == null
|| !rptMain.getRptExpfileType().equalsIgnoreCase("xls")) {
leftMargin = (int) (pageSize.getPageWidth() - columnWidth) / 2;
rightMargin = leftMargin;
}
jasperDesign.setName("template");
jasperDesign.setPageWidth(pageSize.getPageWidth());
jasperDesign.setPageHeight(pageSize.getPageHeight());
jasperDesign.setOrientation(pageSize.getOrientation());
jasperDesign.setColumnWidth(columnWidth);
jasperDesign.setColumnSpacing(0);
jasperDesign.setLeftMargin(leftMargin);
jasperDesign.setRightMargin(rightMargin);
jasperDesign.setTopMargin(30);
jasperDesign.setBottomMargin(20);
}
private void addParameter() throws RPTDesignException {
JRDesignParameter parameter = new JRDesignParameter();
parameter.setName("ReportTitle");
parameter.setValueClass(java.lang.String.class);
try {
jasperDesign.addParameter(parameter);
} catch (JRException e) {
throw new RPTDesignException("报表参数设置错误:" + e.getMessage());
}
}
private void getJasperDesign() throws JRException {
if (columns == null) {
throw new RPTDesignException("报表配置:列有问题!");
}
if (rptMain == null) {
throw new RPTDesignException("报表配置:主表有问题!");
}
jasperDesign = new JasperDesign();
// 设置Page
createPage();
// 设置字体
setFont();
// 查询
setQuery();
// 参数
addParameter();
// Field and Variable
createFieldsAndVariables();
// 表头
createTitle();
// 设置列头
if (rptMain.getRptExpfileType() == null
|| !rptMain.getRptExpfileType().equalsIgnoreCase("xls")) {
createColumnHeads();
}
// 设置分组
createGroup();
// 设置列
createColumn();
// 统计汇兑FOOT
createSummary();
// 最后一页
createLastPageFooter();
}
/**
* 从数据库配置取得配置信息
*
*/
public void getReportSource() {
if (rptMain == null) {
errMessage = this.getClass() + ".getReportSource():报表参数没有初始化!";
return;
}
conn = new ReportConnection().getConection();
String sql = null;
try {
Statement st = conn.createStatement();
List<RptColumn> _columns = new ArrayList();
sql = "select * from RPT_COLUMNS where RPT_ID='"
+ rptMain.getRptId() + "' order by COLUMN_ORDER";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
RptColumn rptColumn = new RptColumn();
rptColumn.setColumnCode(rs.getString("column_code").trim());
rptColumn.setColumnName(rs.getString("column_name"));
rptColumn.setColumnWidth(rs.getInt("column_width"));
rptColumn
.setColumnType(rs.getString("column_type") == null ? "String"
: rs.getString("column_type").trim());
_columns.add(rptColumn);
rptColumn.setColumnSummary(rs.getString("column_summary"));
}
columns = _columns.toArray();
sql = "select * from RPT_HEADS where rpt_id = '"
+ rptMain.getRptId()
+ "' order by HEAD_LEVEL asc,HEAD_ORDER asc";
rs = st.executeQuery(sql);
while (rs.next()) {
RptHead rptHead = new RptHead();
int head_Columns = rs.getInt("head_Columns");
rptHead.setHeadName(rs.getString("head_Name"));
rptHead.setHeadColumns(head_Columns);
int head_Level = rs.getInt("head_Level");
rptHead.setHeadLevel(head_Level);
if (head_Level == 0 || head_Level == 1) {
if (columnHeads1 == null) {
columnHeads1 = new ArrayList();
}
columnHeads1.add(rptHead);
} else if (head_Level == 2) {
if (columnHeads2 == null) {
columnHeads2 = new ArrayList();
}
columnHeads2.add(rptHead);
}
}
if (columnHeads1 != null) {
int column_step1 = 0;
for (int i = 0; i < columnHeads1.size(); i++) {
RptHead rptHead = (RptHead) columnHeads1.get(i);
int head_Columns = rptHead.getHeadColumns();
int current_head_width = 0;
for (int j = 0; j < head_Columns; j++, column_step1++) {
current_head_width = current_head_width
+ ((RptColumn) columns[column_step1])
.getColumnWidth();
}
rptHead.setHeadWidth(current_head_width);
}
}
if (columnHeads2 != null) {
int column_step1 = 0;
for (int i = 0; i < columnHeads2.size(); i++) {
RptHead rptHead = (RptHead) columnHeads2.get(i);
int head_Columns = rptHead.getHeadColumns();
int current_head_width = 0;
for (int j = 0; j < head_Columns; j++, column_step1++) {
current_head_width = current_head_width
+ ((RptColumn) columns[column_step1])
.getColumnWidth();
}
rptHead.setHeadWidth(current_head_width);
}
}
return;
} catch (SQLException e) {
}
}
/**
* 设置主参数,有些参数是从外部传进来的,因为有些是变量 动动态的参数必须先给值
*
* @return
*/
public RptMain getRptMain() {
return rptMain;
}
public void setRptMain(RptMain rptMain) {
this.rptMain = rptMain;
}
public String getErrMessage() {
return errMessage;
}
public void setErrMessage(String errMessage) {
this.errMessage = errMessage;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -