JSP报表打印问题,用ireport报表 实现复杂报表功能

您所在的位置: &
子报表Subreport的示例--静态文本(1)
子报表Subreport的示例--静态文本(1)
清华大学出版社
《JasperReports+iReport报表开发详解》第2章控件,本章的子报表可以实现二级联动的效果,对创建复杂报表起着非常重要的作用。子报表是JasperReports框架中比较重要的知识点。本节为大家介绍子报表Subreport的示例--静态文本。
5.1.2& 子报表Subreport的示例--静态文本(1)
本示例将演示主、子报表的使用,并且主、子报表中的数据都是静态文本,学习这个实验的主要目的是熟悉Subreport在iReport软件中的使用,需要注意的是:如果使用主、子报表时,子报表的DataSource中没有数据,则在预览主报表时子报表默认是不显示的。
1. 创建主报表及子报表
新建名称为main.jrxml主报表模板文件,把除了Detail 1栏之外的所有Band删除,并且添加一个Subreport控件,报表模板布局与添加的Subreport控件如图5.5所示。
弹出询问是否新建一个.jrxml文件还是使用存在的.jrxml,在这里选择新建一个.jrxml,如图5.6所示。
设置后单击&下一步&按钮继续配置,弹出选择报表模板对话框,如图5.7所示,为子报表选择一个模板,在这里只选择A4空白报表模板。
单击&下一步&按钮继续配置,在如图5.8所示的对话框中选择Empty datasource,即不需要数据库连接。
单击&下一步&按钮继续配置,不选择任何Field对象,如图5.9所示。
单击&下一步&按钮继续配置,不需要对子报表中的数据进行分组,如图5.10所示。
【责任编辑: TEL:(010)】&&&&&&
关于&&的更多文章
对于任何一门编程语言来说,算法都是程序的"灵魂"。正是因为算法
本书描述了黑客用默默无闻的行动为数字世界照亮了一条道路的故事。
每天,Google都要测试和发布数百万个源文件、亿万行的
DSL(领域专用语言)的要旨在于沟通。精心设计的DSL
面向模式的软件架构系列丛书被公认为程序员必读经典。
本书译自Grails项目负责人Graeme Keith Rocher所著的“The Definitive Guide to Grails”一书,着重介绍了如何在Grails框架下使
51CTO旗下网站Swing调用IReport报表打印和导出Excel表功能
效果报表设计图
1、报表源文件 dedailBill.jrxml
&?xml version="1.0"
encoding="UTF-8"?&
&jasperReport xmlns=""
xmlns:xsi=""
xsi:schemaLocation="
name="detailBill" language="groovy" pageWidth="595"
pageHeight="842" columnWidth="525" leftMargin="40" rightMargin="30"
topMargin="20" bottomMargin="20"
uuid="ea4d01dd-1c55-46b5-ab1b-64fe48aa11a5"&
&&property name="ireport.zoom"
value="1.0"/&
&&property name="ireport.x"
value="0"/&
&&property name="ireport.y"
value="0"/&
&&parameter name="Fkdw"
class="java.lang.String"/&
&&parameter name="Begin"
class="java.lang.String"/&
&&parameter name="End"
class="java.lang.String"/&
&&queryString&
&&&![CDATA[SELECT
`t_billdetail`
where `fkdw`=$P{Fkdw} and SUBSTRING(`billId`,1,8)&
BETWEEN& $P{Begin}&
and& $P{End}]]&
&&/queryString&
&&field name="billId"
class="java.lang.String"/&
&&field name="fknr"
class="java.lang.String"/&
&&field name="fkdw"
class="java.lang.String"/&
&&field name="skDate"
class="java.sql.Timestamp"/&
&&field name="skMark"
class="java.lang.Integer"/&
&&field name="fkje"
class="java.math.BigDecimal"/&
&&field name="skje"
class="java.math.BigDecimal"/&
&&field name="sumje"
class="java.math.BigDecimal"/&
&&sortField
name="billId"/&
&&variable name="sumRow"
class="java.math.BigDecimal"
calculation="Sum"&
&&&variableExpression&&![CDATA[$F{fkje}-$F{skje}]]&&/variableExpression&
&&/variable&
&&variable name="V_skMark"
class="java.lang.String"&
&&&variableExpression&&![CDATA[($F{skMark}==0)
"贷"]]&&/variableExpression&
&&/variable&
&&variable name="JorD"
class="java.lang.String"&
&&&variableExpression&&![CDATA[($F{sumje}&0)
"贷"]]&&/variableExpression&
&&/variable&
&&variable name="DsumRow"
class="java.math.BigDecimal"&
&&&variableExpression&&![CDATA[($F{sumje}&=0)
? $F{sumje} :
$F{sumje}]]&&/variableExpression&
&&/variable&
&&variable name="total"
class="java.math.BigDecimal" resetType="Column"
calculation="Sum"&
&&&variableExpression&&![CDATA[$V{DsumRow}]]&&/variableExpression&
&&/variable&
&&background&
splitType="Stretch"/&
&&/background&
height="80" splitType="Stretch"&
&&&&staticText&
&&&&&reportElement
uuid="c9cfba08-df64-44c9-a501-9c48d18c6322" x="201" y="13"
width="147" height="40"/&
&&&&&textElement
textAlignment="Center"&
&&&&&&font
size="20"/&
&&&&&/textElement&
&&&&&text&&![CDATA[资金往来表]]&&/text&
&&&&/staticText&
&&&&textField
pattern="yyyy年MM月dd日"&
&&&&&reportElement
uuid="e7d9bd9f-fe7c-4d85-aa6a-02c" x="425" y="59"
width="90" height="20"/&
&&&&&textElement&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&textFieldExpression&&![CDATA[new
java.util.Date()]]&&/textFieldExpression&
&&&&/textField&
&&&&textField&
&&&&&reportElement
uuid="78534cab--be41-" x="0" y="59"
width="160" height="20"/&
&&&&&textElement/&
&&&&&textFieldExpression&&![CDATA["往来单位:"+$P{Fkdw}]]&&/textFieldExpression&
&&&&/textField&
&&pageHeader&
splitType="Stretch"/&
&&/pageHeader&
&&columnHeader&
height="20" splitType="Stretch"&
&&&&staticText&
&&&&&reportElement
uuid="f717f626-3ce0-442c-8ca9-e28b0f70bfeb" x="0" y="0" width="70"
height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"/&
&&&&&text&&![CDATA[单据编号]]&&/text&
&&&&/staticText&
&&&&staticText&
&&&&&reportElement
uuid="e-7c4b159108" x="270" y="0"
width="70" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"/&
&&&&&text&&![CDATA[借方发生额]]&&/text&
&&&&/staticText&
&&&&staticText&
&&&&&reportElement
uuid="96d64e70-448c-4741-aae0-10a97df06422" x="340" y="0"
width="70" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"/&
&&&&&text&&![CDATA[贷方发生额]]&&/text&
&&&&/staticText&
&&&&staticText&
&&&&&reportElement
uuid="c45bcbba-38-ef7e5facaf20" x="450" y="0"
width="70" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"/&
&&&&&text&&![CDATA[余额]]&&/text&
&&&&/staticText&
&&&&staticText&
&&&&&reportElement
uuid="23ff7780-e9bc-4bc6-8cf2-3fe0761ae59e" x="410" y="0"
width="40" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center" verticalAlignment="Middle"
markup="none"/&
&&&&&text&&![CDATA[借或贷]]&&/text&
&&&&/staticText&
&&&&staticText&
&&&&&reportElement
uuid="a38c81b5-58e8-48f0-be7f-d51d683aa6dd" x="70" y="0"
width="100" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"/&
&&&&&text&&![CDATA[摘要]]&&/text&
&&&&/staticText&
&&&&staticText&
&&&&&reportElement
uuid="4c64b8ef-7b8a-46c2-900d-f378afd6d7ba" x="170" y="0"
width="100" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&text&&![CDATA[交易时间]]&&/text&
&&&&/staticText&
&&/columnHeader&
height="20" splitType="Stretch"&
&&&&textField&
&&&&&reportElement
uuid="377bee2-8e18-c2662522ddb6" x="0" y="0" width="70"
height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&textFieldExpression&&![CDATA[$F{billId}]]&&/textFieldExpression&
&&&&/textField&
&&&&textField
pattern="#,##0.00"&
&&&&&reportElement
uuid="6ef743eb--b31b-a93f681baab6" x="270" y="0"
width="70" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Right"
verticalAlignment="Middle"&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&textFieldExpression&&![CDATA[$F{fkje}&0?$F{fkje}:""]]&&/textFieldExpression&
&&&&/textField&
&&&&textField
pattern="#,##0.00" isBlankWhenNull="true"&
&&&&&reportElement
uuid="b57c312d-e-ba36-9ed" x="340" y="0"
width="70" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Right"
verticalAlignment="Middle"&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&textFieldExpression&&![CDATA[$F{skje}&0?$F{skje}:""]]&&/textFieldExpression&
&&&&/textField&
&&&&textField
pattern="#,##0.00"&
&&&&&reportElement
uuid="7f03c5c4-ef8-bbf4fc7132f5" x="450" y="0"
width="70" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Right"
verticalAlignment="Middle"&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&textFieldExpression&&![CDATA[$V{DsumRow}&0
$V{DsumRow}:-$V{DsumRow}]]&&/textFieldExpression&
&&&&/textField&
&&&&textField&
&&&&&reportElement
uuid="6cd6a974-f29c-45e9-82ce-16f8118afea4" x="410" y="0"
width="40" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&textFieldExpression&&![CDATA[$V{JorD}]]&&/textFieldExpression&
&&&&/textField&
&&&&textField&
&&&&&reportElement
uuid="daff-474a-a6ed-151a058ef300" x="70" y="0"
width="100" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&textFieldExpression&&![CDATA[$F{fknr}]]&&/textFieldExpression&
&&&&/textField&
&&&&textField
pattern="yyyy-MM-dd HH:mm:ss"&
&&&&&reportElement
uuid="fab0ea99-a7d4-4d0e-8b79-fdeb739f6bd1" x="170" y="0"
width="100" height="20"/&
lineWidth="0.5"/&
&&&&&&topPen
lineWidth="0.5"/&
&&&&&&leftPen
lineWidth="0.5"/&
&&&&&&bottomPen
lineWidth="0.5"/&
&&&&&&rightPen
lineWidth="0.5"/&
&&&&&/box&
&&&&&textElement
textAlignment="Center"
verticalAlignment="Middle"&
&&&&&&font
size="9"/&
&&&&&/textElement&
&&&&&textFieldExpression&&![CDATA[$F{skDate}]]&&/textFieldExpression&
&&&&/textField&
&&/detail&
&&columnFooter&
height="15" splitType="Stretch"/&
&&/columnFooter&
&&pageFooter&
height="54" splitType="Stretch"/&
&&/pageFooter&
&&summary&
height="42" splitType="Stretch"/&
&&/summary&
&/jasperReport&
2、swing界面
//单位资金往来查询功能
public class QueryCollPanel extends JInternalFrame implements
ActionListener {
&private static final long serialVersionUID =
&private JButton queryBtn, delBtn, detailBtn,
printBtn, excelBtn,extBtn,viewB
&private JTable table= new JTable(); ;
&private DefaultTableModel defaultM
&private JTextField queryEnd,queryB
&private JLabel strC
&private JScrollPane scorV
&private String beginStr,endStr,dwS
&private int limit = 8;
&private String[] headerName = { "编号", "内容","时间",
"借方发生额", "贷方发生额", "借或贷", "余额"};
&private Object[][] headerD
&private JLabel lblEnd,lblB
&private JComboBox dwC
&private CompanyService companyService=
(CompanyService)
AdapterContext.getContext().getBean("companyService");
&public QueryCollPanel() {
&&this.setTitle("单位资金往来查询");
&&this.setSize(740, 550);
&&setFrameIcon(null);
&&this.setResizable(false);
&&this.setVisible(true);
&public void init() {
&&getContentPane().setLayout(null);
&&delBtn = new
JButton("清& 空");
&&delBtn.addActionListener(this);&&
&&queryBegin=new
JTextField();
&&queryBegin.setFont(new
Font("黑体", Font.PLAIN, 14));
&&queryBegin.setBounds(210, 35,
&&queryBegin.addActionListener(this);
&&queryBegin.addKeyListener(new
InputKeyListener());
&&getContentPane().add(queryBegin);
&&queryEnd=new
JTextField();
&&queryEnd.setFont(new Font("黑体",
Font.PLAIN, 14));
&&queryEnd.setBounds(314, 35, 74,
&&queryEnd.addActionListener(this);
&&queryEnd.addKeyListener(new
InputKeyListener());
&&getContentPane().add(queryEnd);
&&detailBtn = new
JButton("查看明细");
&&detailBtn.setBounds(51, 46, 81,
&&detailBtn.addActionListener(this);
&&JLabel con = new
JLabel("&html&&font
color=blue&\u8BF7\u8F93\u\u8BE2\u65E5\u671F&br&\u683C&\u5F0F&&/font&&/html&");
&&con.setBorder(new
LineBorder(Color.MAGENTA));
&&con.setHorizontalAlignment(SwingConstants.CENTER);
&&con.setBounds(60, 30, 110,
&&getContentPane().add(con);
&&queryBtn = new
JButton("查& 询");
&&queryBtn.setBounds(608, 35, 70,
&&getContentPane().add(queryBtn);
&&queryBtn.addActionListener(this);
&&excelBtn = new
JButton("导出Excel");
&&excelBtn.setBounds(60, 460, 90,
&&getContentPane().add(excelBtn);
&&excelBtn.addActionListener(this);
&&scorView=new
JScrollPane();
&&scorView.setBounds(10, 70, 710,
&&getContentPane().add(scorView);
&&strCount = new
JLabel("");
&&strCount.setBounds(425, 450,
&&getContentPane().add(strCount);
&&printBtn = new
JButton("打印列表");
&&printBtn.addActionListener(this);
&&printBtn.setBounds(160, 460,
&&getContentPane().add(printBtn);
&&extBtn= new
JButton("退& 出");
&&extBtn.setBounds(345, 460, 70,
&&extBtn.addActionListener(this);
&&getContentPane().add(extBtn);
&&viewBtn= new
JButton("打印预览");
&&viewBtn.setBounds(252, 460, 82,
&&viewBtn.addActionListener(this);
&&getContentPane().add(viewBtn);
&&lblBegin = new
JLabel("起始");
&&lblBegin.setHorizontalAlignment(SwingConstants.CENTER);
&&lblBegin.setBounds(180, 35, 30,
&&getContentPane().add(lblBegin);
&&lblEnd = new
JLabel("结束");
&&lblEnd.setHorizontalAlignment(SwingConstants.CENTER);
&&lblEnd.setBounds(284, 35, 30,
&&getContentPane().add(lblEnd);
&&dwCmb= new
JComboBox(companyService.getCompanyName());
&&dwCmb.setBounds(448, 35, 150,
&&getContentPane().add(dwCmb);
&&//initPayCompany();
&&JLabel lblNewLabel = new
JLabel("选择单位");
&&lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
&&lblNewLabel.setBounds(388, 35,
&&getContentPane().add(lblNewLabel);
&public void actionPerformed(ActionEvent e)
&&if (e.getSource() ==
queryBtn)select();
&&if (e.getSource() ==
excelBtn)excel();
&&if (e.getSource() ==
printBtn)print();
&&if(e.getSource()==extBtn)ext();
&&if(e.getSource()==viewBtn)printView();
&public void ext(){
&&int result =
JOptionPane.showOptionDialog(null, "你确认是否要退出该窗口?","系统提示",
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
null,new String[]& {"是","否","取消"},"是");
if (result == JOptionPane.YES_OPTION)
{&&&&&&&&&&&
&&&&&&&&&&
this.dispose();
//直接打印列表中的数据
&private void print() {
&&if (table.getRowCount() == 0)
&&&JOptionPane.showMessageDialog(null,
"对不起,没有你要打印的数据\n请选择数据后再进行打印!",
&&&&&"打印提示信息",
RMATION_MESSAGE);
&&} else {
&&&MessageFormat
header = new
MessageFormat("资金往来明细表\n"+dwCmb.getSelectedItem());
&&&MessageFormat
footer = new MessageFormat("第 {0,number,integer} 页");
&&&&table.print(JTable.PrintMode.FIT_WIDTH,
header, footer);
&&&} catch
(PrinterException ep) {
&&&&JOptionPane.showMessageDialog(null,
ep.getMessage(), "打印提示信息",
&&&&&&<RMATION_MESSAGE);
System.err.format("不能打印 %s%n", ep.getMessage());
&//利用ireport打印预览
&private void printView(){
&&JasperReport
&&JasperPrint jasperP
&&&InputStream
inReport =
QueryCollPanel.class.getResourceAsStream("/org/bill/report/detailBill.jrxml");
&&&jasperReport
= pileReport(inReport);
&&&Connection
&&&DataSource
dataSource=(DataSource)
AdapterContext.getContext().getBean("dataSource");
&&&con=dataSource.getConnection();
Map&String, Object& parameters =
new& HashMap&String,
Object&();
parameters.put("Fkdw", dwCmb.getSelectedItem().toString());
parameters.put("Begin", queryBegin.getText().trim());
parameters.put("End", queryEnd.getText().trim());
&&&jasperPrint=JasperFillManager.fillReport(jasperReport,
parameters, con);
&&&// 打印预览
&&&if(jasperPrint.getPages().size()&1){
&&&&JOptionPane.showMessageDialog(null,
"对不起,没有查询到要打印的数据\n请选择数据后再进行打印!",
&&&&&"打印提示信息",
RMATION_MESSAGE);
&&&&con.close();
&&&JasperViewer.setDefaultLookAndFeelDecorated(false);
&&&//预览后直接退出程序
&&&//JasperViewer.viewReport(jasperPrint);
&&&//预览后不退出程序
&&&JasperViewer.viewReport(jasperPrint,false);
&&&//不预览直接打印
&&&//JasperPrintManager.printReport(jasperPrint,
&&&con.close();
&&} catch (Exception e) {
&&&System.out.println(e.getMessage());
&public void select() {
&&//开始计时
&&long begin =
System.currentTimeMillis();
&&//清空数据表的数据
&&scorView.setViewportView(null);
&&//table.removeAll();
&&strCount.setText("");
&&//验证条件
&&yanzheng();&
查询数据&&
&&dwStr=dwCmb.getSelectedItem().toString();
&&beginStr=queryBegin.getText().trim();
&&endStr = (String)
queryEnd.getText().trim();
&&int k=0;
&&List&BillDetail&
list=BillSum.getBillDetailVoList(dwStr, beginStr, endStr);
&&k=list.size();
&&if(k&1){
&&&scorView.setViewportView(null);
&&&strCount.setText("");
&&&JOptionPane.showMessageDialog(null,
"没有找到满足条件的数据!!!", "系统提示",RMATION_MESSAGE);
&&headerData = new
Object[0][headerName.length];
&&defaultModel = new
DefaultTableModel(headerData, headerName) {
&&&private
static final long serialVersionUID = 1L;
boolean isCellEditable(int row, int column) {
&&&&return
&&table.setDragEnabled(false);
&&table.setRowSelectionAllowed(true);
&&// 禁止移动列的位置
&&table.getTableHeader().setBackground(new
Color(0xffffff));
&&table.getTableHeader().setReorderingAllowed(false);
&&table.setRowHeight(25);
&&// 设置被选择的行的颜色
&&table.setSelectionBackground(Color.GRAY);
&&//设置标题居中
&&JTableHeader
tableHeader=table.getTableHeader();
&& DefaultTableCellRenderer hr =
(DefaultTableCellRenderer) tableHeader
.getDefaultRenderer();&
hr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);//列名居中&&
&&// 数据全部居中
&&DefaultTableCellRenderer r =
new DefaultTableCellRenderer();
&&r.setHorizontalAlignment(JLabel.CENTER);
&&table.setDefaultRenderer(Object.class,
&&table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
&&table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
&&Iterator&BillDetail&
iter=list.iterator();
&&int j=0;
&&while (iter.hasNext()) { //
遍历结果集
&&&&BillDetail
billDetail=(BillDetail) iter.next();
&&&&Vector&Object&
insertRow = new
Vector&Object&();
&&&&insertRow.addElement(billDetail.getBillId());
&&&&insertRow.addElement(billDetail.getFknr());
&&&&insertRow.addElement(billDetail.getSkDate());
&&&&insertRow.addElement(billDetail.getFkje());
&&&&insertRow.addElement(billDetail.getSkje());
&&&&if(billDetail.getSumje()&0)insertRow.addElement("借");
if(billDetail.getSumje()==0)insertRow.addElement("平");
insertRow.addElement("贷");
&&&&insertRow.addElement(billDetail.getSumje());
&&&&defaultModel.addRow(insertRow);
&&table.setModel(defaultModel);
&&TableColumnModel cm =
table.getColumnModel();
&&cm.getColumn(0).setPreferredWidth(100);
&&cm.getColumn(1).setPreferredWidth(160);
&&cm.getColumn(2).setPreferredWidth(140);
&&cm.getColumn(3).setPreferredWidth(120);
&&cm.getColumn(4).setPreferredWidth(120);
&&cm.getColumn(5).setPreferredWidth(60);
&&cm.getColumn(6).setPreferredWidth(120);
&&//使各个列不可变动
&& for(int
i=0;i&cm.getColumnCount();i++){
cm.getColumn(i).setResizable(false);
//方法二:重写getTableCellRendererComponent()方法
table.getColumnModel().getColumn(0).setCellRenderer(&&
&&&&&&&&&&
new DefaultTableCellRenderer()
&&&&&&&&&&&&&&
private static final long serialVersionUID =
&&&&&&&&&&&&&&
public Component
getTableCellRendererComponent(&&
&&&&&&&&&&&&&&&&&&&&&&
JTable table, Object value, boolean
isSelected,&&
&&&&&&&&&&&&&&&&&&&&&&
boolean hasFocus, int row, int column)
&&&&&&&&&&&&&
& setForeground(Color.BLUE);
&&&&&&&&&&&&&
setHorizontalAlignment(SwingConstants.CENTER);
&&&&&&&&&&&&&&&&&&
Component c = super&
&&&&&&&&&&&&&&&&&&&&&&&&&&
.getTableCellRendererComponent(table,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
isSelected, hasFocus, row,
column);&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&
&&scorView.setViewportView(table);
&&long end =
System.currentTimeMillis();
&&strCount.setText("&html&共查到数据
&font color=red&"+ k +
"&/font& 条,耗时 &font
color=red&"+(end -
begin)+"&/font&
color=red&"+list.get(k-1).getSumje()+"
元。&/html&");
&// 验证日期输入框
&private void yanzheng() {
numEnd=queryEnd.getText().trim();
numBegin=queryBegin.getText().trim();
&&if (numEnd.length()
& limit) {
&&&getToolkit().beep();
&&&JOptionPane.showMessageDialog(null,
"输入数据的长度不能大于 " + limit);
&&&String str =
queryEnd.getText();
&&&queryEnd.setText(str.substring(0,
&&} else if (numEnd.length()
& limit) {
&&&getToolkit().beep();
&&&JOptionPane.showMessageDialog(null,
"输入数据的长度不能小于 " + limit
",系统将用当前日期自动填充!");
&&&String str =
queryEnd.getText();
&&&SimpleDateFormat
ff = new SimpleDateFormat("yyyyMMdd");
ff.format(new java.util.Date());
&&&queryEnd.setText(str);
&if(Integer.valueOf(numBegin)&Integer.valueOf(numEnd)){
&&JOptionPane.showMessageDialog(null,"起始日期不能大于结束日期,请重新输入!!!",
"系统提示",&JOptionPane.ERROR_MESSAGE);
&&queryBegin.setText("");
&&queryBegin.requestFocusInWindow();
} catch (Exception e) {
&//JOptionPane.showMessageDialog(null,e.getMessage()+",请重新输入!!!",
"系统提示",&JOptionPane.ERROR_MESSAGE);
&SimpleDateFormat ff = new
SimpleDateFormat("yyyyMMdd");&
&queryBegin.setText(ff.format(new
java.util.Date()));
&private void excel() {
&&long begin =
System.currentTimeMillis();
&&yanzheng();
&&// 建立新HSSFWorkbook对象
&&HSSFWorkbook wb = new
HSSFWorkbook();
&&// 建立新的sheet对象
&&HSSFSheet sheet =
wb.createSheet(queryEnd.getText().trim());
&&HSSFCellStyle setBorder =
wb.createCellStyle();
&&// 建立新行
&&// HSSFRow row =
sheet.createRow((short) 0);
&&// 建立新cell
&&// HSSFCell cell =
row.createCell((short) 0);
&&// 查询数据
&&// 设置cell的整数类型的值
&&// cell.setCellValue(1);
&&// 设置cell浮点类型的值
&&// row.createCell((short)
1).setCellValue(1.2);
&&// 设置cell字符类型的值
&&// row.createCell((short)
2).setCellValue("test");
&&// 设置cell布尔类型的值
&&// row.createCell((short)
3).setCellValue(true);
&&// 建立新的cell样式
&&// HSSFCellStyle cellStyle =
wb.createCellStyle();
&&// 设置cell样式为定制的日期格式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy
&&// HSSFCell dCell =
row.createCell((short) 4);
&&// 设置cell为日期类型的值
dCell.setCellValue(DateUtils.getSQLNow());
&&// 设置该cell日期的显示格式
dCell.setCellStyle(cellStyle);
&&// HSSFCell csCell =
row.createCell((short) 5);
&&// 设置cell编码解决中文高位字节截断
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);
&&// 设置字符串
&&// csCell.setCellValue("This is
测试字符串");
&&// 建立错误cell
&&// row.createCell((short)
6).setCellType(HSSFCell.CELL_TYPE_ERROR);
&&//设置居中:
&&//setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
&&// 定义列宽
&&sheet.setColumnWidth(0,
&&sheet.setColumnWidth( 1,
&&sheet.setColumnWidth( 2,
&&sheet.setColumnWidth(3,
&&sheet.setColumnWidth(4,&
&&sheet.setColumnWidth(
5,& 3300);
&&sheet.setColumnWidth(6,&
&&setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
查询数据&&
&&dwStr=dwCmb.getSelectedItem().toString();
&&beginStr=queryBegin.getText().trim();
&&endStr = (String)
queryEnd.getText().trim();
&&List&BillDetail&
list=BillSum.getBillDetailVoList(dwStr, beginStr, endStr);
&&if(list.size()&1){
&&&scorView.setViewportView(null);
&&&strCount.setText("");
&&&JOptionPane.showMessageDialog(this,
"没有查询到数据!");
&&int m=list.size();
&&int k=1;
row=sheet.createRow(0);
&&HSSFCell data[] = new
HSSFCell[7];
j=0;j&data.j++ ){
&&&data[j]=row.createCell(j);
&&data[0].setCellValue(headerName[0]);
&&data[1].setCellValue(headerName[1]);
&&data[2].setCellValue(headerName[2]);&&
&&data[3].setCellValue(headerName[3]);
&&data[4].setCellValue(headerName[4]);
&&data[5].setCellValue(headerName[5]);
&&data[6].setCellValue(headerName[6]);
&&&&Iterator&BillDetail&
iter=list.iterator();
(iter.hasNext()) { // 遍历结果集
= sheet.createRow(k);
&&&&BillDetail
cs=(BillDetail)
iter.next();&&
= new HSSFCell[7];
&&&&for(int
j=0;j&data.j++ ){
&&&&&data[j]=row.createCell(j);
&&&&data[0].setCellValue(cs.getBillId().toString());
&&&&data[1].setCellValue(cs.getFknr());
&&&&data[2].setCellValue(cs.getSkDate());
&&&&data[3].setCellValue(cs.getFkje());
&&&&data[4].setCellValue(cs.getSkje());
&&&&if(cs.getSumje()&0)
&&&&&data[5].setCellValue("借");
if(cs.getSumje()==0)data[5].setCellValue("平");
data[5].setCellValue("贷");
&&&&data[6].setCellValue(cs.getSumje());
&&} catch (Exception sqle)
&&&System.out.println(sqle.toString());
&&FileOutputStream fileOut =
&&&System.out.println(System.getProperty("user.dir"));
&&&SimpleDateFormat
ff = new SimpleDateFormat("yyyyMMddhhmmss");
&&&fileOut = new
FileOutputStream(System.getProperty("user.dir")+ "" + ff.format(new Date()) +
&&&wb.write(fileOut);
&&&long end =
System.currentTimeMillis();
&&&strCount.setText("&html&共导出数据
&font color=red&"+ m +
"&/font& 条,耗时 &font
color=red&"+(end -
begin)+"&/font&
&/html&");
&&&JOptionPane.showMessageDialog(this,"导出成功。\n\r"
+ System.getProperty("user.dir") + ""&+ ff.format(new
Date()) + ".xls");
&&} catch (IOException e) {
&&&JOptionPane.showMessageDialog(this,
e.getMessage());
&&} finally {
&&&&fileOut.close();
&&&} catch
(IOException e) {
&&&&JOptionPane.showMessageDialog(this,
e.getMessage());
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

 

随机推荐