只好重新问一次:java如何作report? 我也正遇到这样的问题!!!如果是用java来作数据库开发都应会用同样的问题!!!还有java如何作成EXCEL一样的电子表格界面可随时更改数据项,最后按保存后所有的更改会存入数据库中。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以引用Excel.appliction对象呀~~~~~~~ Formular onehttp://www.tidestone.com/ TO 万里晴空无云不要再提Formular one啦,下载了,可是不大会用,比方说如何同JAVA的APPLICATION连接.... TO CITYLINK我想要的是自己可以定义格式,表头,字段....的能够打印出来的给领导看的REPORT.....可以么?... 建议你不如拿VB +Crystal report 来搞. 看来自己来答复一下吧:作REPORT可以将TABLE打出来,至于标题之类的就另行添加吧.import javax.swing.*;import javax.swing.table.*;import java.awt.print.*;import java.util.*;import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.Dimension;public class Report implements Printable{ JFrame frame; JTable tableView; public Report() { frame = new JFrame("Sales Report"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);}}); final String[] headers = {"Description", "open price", "latest price", "End Date", "Quantity"}; final Object[][] data = { {"Box of Biros", "1.00", "4.99", new Date(), new Integer(2)}, {"Blue Biro", "0.10", "0.14", new Date(), new Integer(1)}, {"legal pad", "1.00", "2.49", new Date(), new Integer(1)}, {"tape", "1.00", "1.49", new Date(), new Integer(1)}, {"stapler", "4.00", "4.49", new Date(), new Integer(1)}, {"legal pad", "1.00", "2.29", new Date(), new Integer(5)} }; TableModel dataModel = new AbstractTableModel() { public int getColumnCount() { return headers.length; } public int getRowCount() { return data.length;} public Object getValueAt(int row, int col) { return data[row][col];} public String getColumnName(int column) { return headers[column];} public Class getColumnClass(int col) { return getValueAt(0,col).getClass();} public boolean isCellEditable(int row, int col) { return (col==1);} public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; } }; tableView = new JTable(dataModel); JScrollPane scrollpane = new JScrollPane(tableView); scrollpane.setPreferredSize(new Dimension(500, 80)); frame.getContentPane().setLayout( new BorderLayout()); frame.getContentPane().add( BorderLayout.CENTER,scrollpane); frame.pack(); JButton printButton= new JButton(); printButton.setText("print me!"); frame.getContentPane().add( BorderLayout.SOUTH,printButton); // for faster printing turn double buffering off RepaintManager.currentManager( frame).setDoubleBufferingEnabled(false); printButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent evt) { PrinterJob pj=PrinterJob.getPrinterJob(); pj.setPrintable(Report.this); pj.printDialog(); try{ pj.print(); }catch (Exception PrintException) {} } }); frame.setVisible(true); } public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.black); int fontHeight=g2.getFontMetrics().getHeight(); int fontDesent=g2.getFontMetrics().getDescent(); //leave room for page number double pageHeight = pageFormat.getImageableHeight()-fontHeight; double pageWidth = pageFormat.getImageableWidth(); double tableWidth = (double) tableView.getColumnModel( ).getTotalColumnWidth(); double scale = 1; if (tableWidth >= pageWidth) { scale = pageWidth / tableWidth; } double headerHeightOnPage= tableView.getTableHeader( ).getHeight()*scale; double tableWidthOnPage=tableWidth*scale; double oneRowHeight=(tableView.getRowHeight()+ tableView.getRowMargin())*scale; int numRowsOnAPage= (int)((pageHeight-headerHeightOnPage)/ oneRowHeight); double pageHeightForTable=oneRowHeight* numRowsOnAPage; int totalNumPages= (int)Math.ceil(( (double)tableView.getRowCount())/ numRowsOnAPage); if(pageIndex>=totalNumPages) { return NO_SUCH_PAGE; } g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());//bottom center g2.drawString("Page: "+(pageIndex+1), (int)pageWidth/2-35, (int)(pageHeight +fontHeight-fontDesent)); g2.translate(0f,headerHeightOnPage); g2.translate(0f,-pageIndex*pageHeightForTable); //If this piece of the table is smaller //than the size available, //clip to the appropriate bounds. if (pageIndex + 1 == totalNumPages) { int lastRowPrinted = numRowsOnAPage * pageIndex; int numRowsLeft = tableView.getRowCount() - lastRowPrinted; g2.setClip(0, (int)(pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(oneRowHeight * numRowsLeft)); } //else clip to the entire area available. else{ g2.setClip(0, (int)(pageHeightForTable*pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable)); } g2.scale(scale,scale); tableView.paint(g2); g2.scale(1/scale,1/scale); g2.translate(0f,pageIndex*pageHeightForTable); g2.translate(0f, -headerHeightOnPage); g2.setClip(0, 0, (int) Math.ceil(tableWidthOnPage), (int)Math.ceil(headerHeightOnPage)); g2.scale(scale,scale); tableView.getTableHeader().paint(g2); //paint header at top return Printable.PAGE_EXISTS; } public static void main(String[] args) { new Report(); }} java报表市场的leader...www.inetsoftcorp.com 这里有个demo: http://stone.tristone.com:7100/servlet/SreeDemo 下面的代码会出错,Exception in thread "main" java.lang.Error:? Jmenu 可以鼠标移上去就改变颜色吗? JAVA socket 优化传输效率!!《求助》高手快来 那里有log4j.jar包下啊! java代码小问题急请教? 100分急救%¥¥#◎¥……StyledDocument…… 错误出在哪里?帮忙看一下! thinking in java 的一道简单练习题 我是新来的,对此很感兴趣 做一个简单数字游戏的问题 有没有报表自动生成工具for java的,如VB的seagate crystal Reports? 这里没有高手吗?
http://www.tidestone.com/
不要再提Formular one啦,
下载了,可是不大会用,比方说如何同JAVA的APPLICATION连接....
我想要的是自己可以定义格式,表头,字段....的能够打印出来的给领导看的REPORT.....
可以么?...
作REPORT可以将TABLE打出来,至于标题之类的就另行添加吧.
import javax.swing.*;
import javax.swing.table.*;
import java.awt.print.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Dimension;public class Report implements Printable{
JFrame frame;
JTable tableView; public Report() {
frame = new JFrame("Sales Report");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}}); final String[] headers = {"Description", "open price",
"latest price", "End Date", "Quantity"};
final Object[][] data = {
{"Box of Biros", "1.00", "4.99", new Date(),
new Integer(2)},
{"Blue Biro", "0.10", "0.14", new Date(),
new Integer(1)},
{"legal pad", "1.00", "2.49", new Date(),
new Integer(1)},
{"tape", "1.00", "1.49", new Date(),
new Integer(1)},
{"stapler", "4.00", "4.49", new Date(),
new Integer(1)},
{"legal pad", "1.00", "2.29", new Date(),
new Integer(5)}
}; TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return headers.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {
return data[row][col];}
public String getColumnName(int column) {
return headers[column];}
public Class getColumnClass(int col) {
return getValueAt(0,col).getClass();}
public boolean isCellEditable(int row, int col) {
return (col==1);}
public void setValueAt(Object aValue, int row,
int column) {
data[row][column] = aValue;
}
}; tableView = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(tableView); scrollpane.setPreferredSize(new Dimension(500, 80));
frame.getContentPane().setLayout(
new BorderLayout());
frame.getContentPane().add(
BorderLayout.CENTER,scrollpane);
frame.pack();
JButton printButton= new JButton(); printButton.setText("print me!"); frame.getContentPane().add(
BorderLayout.SOUTH,printButton); // for faster printing turn double buffering off RepaintManager.currentManager(
frame).setDoubleBufferingEnabled(false); printButton.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent evt) {
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(Report.this);
pj.printDialog();
try{
pj.print();
}catch (Exception PrintException) {}
}
}); frame.setVisible(true);
} public int print(Graphics g, PageFormat pageFormat,
int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight=g2.getFontMetrics().getHeight();
int fontDesent=g2.getFontMetrics().getDescent(); //leave room for page number
double pageHeight =
pageFormat.getImageableHeight()-fontHeight;
double pageWidth =
pageFormat.getImageableWidth();
double tableWidth = (double)
tableView.getColumnModel(
).getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
} double headerHeightOnPage=
tableView.getTableHeader(
).getHeight()*scale;
double tableWidthOnPage=tableWidth*scale; double oneRowHeight=(tableView.getRowHeight()+
tableView.getRowMargin())*scale;
int numRowsOnAPage=
(int)((pageHeight-headerHeightOnPage)/
oneRowHeight);
double pageHeightForTable=oneRowHeight*
numRowsOnAPage;
int totalNumPages=
(int)Math.ceil((
(double)tableView.getRowCount())/
numRowsOnAPage);
if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;
} g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
//bottom center
g2.drawString("Page: "+(pageIndex+1),
(int)pageWidth/2-35, (int)(pageHeight
+fontHeight-fontDesent)); g2.translate(0f,headerHeightOnPage);
g2.translate(0f,-pageIndex*pageHeightForTable); //If this piece of the table is smaller
//than the size available,
//clip to the appropriate bounds.
if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted =
numRowsOnAPage * pageIndex;
int numRowsLeft =
tableView.getRowCount()
- lastRowPrinted;
g2.setClip(0,
(int)(pageHeightForTable * pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(oneRowHeight *
numRowsLeft));
}
//else clip to the entire area available.
else{
g2.setClip(0,
(int)(pageHeightForTable*pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(pageHeightForTable));
} g2.scale(scale,scale);
tableView.paint(g2);
g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0,
(int) Math.ceil(tableWidthOnPage),
(int)Math.ceil(headerHeightOnPage));
g2.scale(scale,scale);
tableView.getTableHeader().paint(g2);
//paint header at top return Printable.PAGE_EXISTS;
} public static void main(String[] args) {
new Report();
}
}
www.inetsoftcorp.com