- 论坛徽章:
- 0
|
打印问题,如何在web下对查询出的数据生成格式报表进行打印?
有很多方法,我把我知道的列在下面:
1.直接在IE中打印
2.用applet调用打印API实现打印。(有例子在下面)
3.用ActiveX实现
4.用javascript通过OLE操作word/excel进行客户端打印。
5.通过pdf相关的类库,生成pdf文档,客户端下载pdf文档再进行打印。可以实现跨平台
6.用JAVA.AWT.PRINT包中的类来打印
7.第三方软件(如JReport)实现报表打印
http://www.chinajavaworld.net/fo ... ;start=10&show=
用applet调用打印API实现打印的例子(chinajavaworld转贴,网友shonelau提供
)
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.swing.*;
public class PrintTestApplet extends JApplet implements ActionListener
{
public void init()
{
String BuyerName=getParameter("BuyerName"
String BidDate=getParameter("BidDate"
String Bail=getParameter("Bail"
String Agency=getParameter("Agency"
//String BidNO=getParameter("BidNO"
String Today=getParameter("Today"
String payBank=getParameter("payBank"
String payAccount=getParameter("payAccount"
String payer=getParameter("payer"
Container contentPane=getContentPane();
canvas=new PrintPanel(BuyerName,BidDate,Bail,Agency,Today,payBank,payAccount,payer);
contentPane.add(canvas,"Center"
JPanel buttonPanel=new JPanel();
printButton=new JButton("rint");
buttonPanel.add(printButton);
printButton.addActionListener(this);
pageSetupButton=new JButton("age Setup");
buttonPanel.add(pageSetupButton);
pageSetupButton.addActionListener(this);
contentPane.add(buttonPanel,"North");
paper = new Paper();
//设置纸张大小
paper.setSize(680D, 340D);
paper.setImageableArea(0D, 0D, 680D, 340D);
}
public void actionPerformed(ActionEvent event)
{
Object source=event.getSource();
if(source==printButton)
{
PrinterJob printJob=PrinterJob.getPrinterJob();
if(pageFormat==null)
{
pageFormat=printJob.defaultPage();
pageFormat.setPaper(paper);
}
printJob.setPrintable(canvas,pageFormat);
if(printJob.printDialog())
{
try{
printJob.print();
}catch(PrinterException exception)
{
JOptionPane.showMessageDialog(this,exception);
}
}
}
}
private JButton printButton;
private JButton pageSetupButton;
private PrintPanel canvas;
private PageFormat pageFormat;
private Paper paper;
}
class PrintPanel extends JPanel implements Printable
{
public PrintPanel(String BuyerName1,String BidDate1,String Bail1,String Agency1,String Today1,String payBank1,String payAccount1,String payer1)
{
Buyer=BuyerName1;
BidDate=BidDate1;
Bail=Bail1;
Agency=Agency1;
//BidNO=BidNO1;
Today=Today1;
payBank=payBank1;
payAccount=payAccount1;
payer=payer1;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
drawPage(g2);
}
public int print(Graphics g,PageFormat pf,int page) throws PrinterException
{
if(page>;=1)
{
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2=(Graphics2D)g;
g2.setPaint(Color.black);
g2.translate(pf.getImageableX()+50,pf.getImageableY()+10);
//g2.draw(new Rectangle2D.Double(0,0,pf.getImageableWidth(),pf.getImageableHeight()));
drawPage(g2);
return Printable.PAGE_EXISTS;
}
public void drawPage(Graphics2D g2)
{
String s1="收 款 收 据";
String s2=Today.substring(0,4)+"年"+Today.substring(5,7)+"月"+Today.substring(+"日";
Font f=new Font("SansSerif",Font.BOLD,20);
g2.setFont(f);
FontMetrics fm=g2.getFontMetrics(f);
Dimension d=getSize();
//**print the title
int w1=fm.stringWidth(s1);
//int h1=fm.stringHeight(s1);
g2.drawString(s1,(d.width-20-w1)/2,d.height/20);
g2.drawLine((d.width-w1)/2-10,d.height/20+5,d.width/2+w1/2+10,d.height/20+5);
g2.drawLine((d.width-w1)/2-10,d.height/20+10,d.width/2+w1/2+10,d.height/20+10);
//**print the date when to print this reciept
f=new Font("SansSerif",Font.PLAIN,10);
g2.setFont(f);
fm=g2.getFontMetrics(f);
int w2=fm.stringWidth(s2);
g2.drawString(s2,(d.width-w2)/2,d.height/20+25);
//**print the rectange of the reciept
g2.drawRect(d.width/20+30,d.height/20+35,d.width-d.width/10-70,d.height-d.height/10-d.height/20-65);
//**print the content of the reciept
f=new Font("SansSerif",Font.PLAIN,14);
g2.setFont(f);
g2.drawString(" 今收到:",d.width/20+5+30,d.height/20+60);
fm=g2.getFontMetrics(f);
int w3=fm.stringWidth(" 今收到:");
//**print buyer
g2.drawString(Buyer,d.width/20+5+w3+5+30,d.height/20+60);
//**print the underline of the buyer
g2.drawLine(d.width/20+5+w3+5+30,d.height/20+65,d.width-d.width/20-5-50,d.height/20+65);
g2.drawString(" 交 来:",d.width/20+5+30,d.height/20+105);
int w4=fm.stringWidth(" 交 来:");
g2.drawString(BidDate.substring(0,16)+"拍卖会保证金",d.width/20+5+w4+5+30,d.height/20+105);
g2.drawLine(d.width/20+5+w4+5+30,d.height/20+110,d.width-d.width/20-5-50,d.height/20+110);
f=new Font("SansSerif",Font.PLAIN,16);
g2.setFont(f);
g2.drawString(" 金额(大写):",d.width/20+5+30,d.height/20+150);
int w5=fm.stringWidth(" 金额(大写):");
//计算货币的大小写 开始
String M1="仟佰拾万仟佰拾元";
String M2="";
for(int i=0;i<Bail.length();i++)
{
if(i<(Bail.length()-1)){
M2=M2+toUpperM(Bail.substring(i,i+1))+M1.substring((8-Bail.length()+i),(8-Bail.length())+1+i);
//M2=M2+Bail.substring(i,i+1)+M1.substring((8-Bail.length()+i),(8-Bail.length())+1+i);
}else{
M2=M2+toUpperM(Bail.substring(i))+"元整";
//M2=M2+Bail.substring(i)+"元整";
}
}
//计算货币的大小写 结束
g2.drawString(M2,d.width/20+5+w5+10+30,d.height/20+150);
int w6=fm.stringWidth(M2);
g2.drawString("¥"+Bail+".",d.width/20+5+w5+10+w6+35+30,d.height/20+150);
int w7=fm.stringWidth("¥"+Bail+".");
f=new Font("SansSerif",Font.PLAIN,12);
g2.setFont(f);
g2.drawString("00",d.width/20+5+w5+10+w6+35+w7+5+30,d.height/20+14;
g2.drawLine(d.width/20+5+w5+10+30,d.height/20+155,d.width-d.width/20-5-50,d.height/20+155);
//银行信息
f=new Font("SansSerif",Font.PLAIN,14);
g2.setFont(f);
g2.drawString(" 付款行:"+payBank+" 帐号名:"+payer+" 帐号:"+payAccount,d.width/20+5+30,d.height/20+175);
//插入备注事项
f=new Font("SansSerif",Font.PLAIN,14);
g2.setFont(f);
g2.drawString(" 收款单位盖章)",d.width/20+5+30,d.height/20+195);
g2.drawString(" 本收据作为已交款凭证;凭本收据换取竟买牌号;请妥善保管,遗失不补",d.width/20+5+30,d.height-80);
g2.drawString("付款人:",d.width/2,d.height-50);
g2.drawString("经办人:"+Agency,d.width/2+150,d.height-50);
}
public String toUpperM(String m)
{
int Money1=Integer.parseInt(m);
if(Money1==0)
{
return "零";
}else if(Money1==1)
{
return "壹";
}else if(Money1==2)
{
return "贰";
}else if(Money1==3)
{
return "叁";
}else if(Money1==4)
{
return "肆";
}else if(Money1==5)
{
return "伍";
}else if(Money1==6)
{
return "陆";
}else if(Money1==7)
{
return "柒";
}else if(Money1==
{
return "捌";
}else if(Money1==9)
{
return "玖";
}else
{
return m;
}
}
private String Buyer;
private String BidDate;
private String Bail;
private String Agency;
private String BidNO;
private String Today;
private String payBank;
private String payAccount;
private String payer;
} |
|