实现打印的4种方法:1,可以用嵌入ie中的word,excel实现,如果你的服务器是tomcat
word:
文件头上加:<%@ page contentType= "application/msword " % >
tomcat\conf\web.xml里:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
excel:
文件头上加:<%@ page contentType="application/vnd.ms-excel" %>
tomcat\conf\web.xml里:
<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>
2.直接调用windows函数
<input type="button" value="print"
onclick="window.focus();window.print()">3.调用js
//放在head 与 head 之间
<SCRIPT LANGUAGE=javascript>
<!--
function setPrint()
{
WB.ExecWB(8,1);
}
function previewPrint()
{
WB.ExecWB(7,1)
}
//-->
</SCRIPT>
//放在body与body之间
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WB width=0>
</OBJECT>
<INPUT type="button" value="打印设置" id=button1 name=button1 onclick="setPrint();">
<INPUT type="button" value="打印预览" id=button2 name=button2 onclick="previewPrint();">
4,如果要求很高,就用专业报表工具,不过价格不便宜
word:
文件头上加:<%@ page contentType= "application/msword " % >
tomcat\conf\web.xml里:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
excel:
文件头上加:<%@ page contentType="application/vnd.ms-excel" %>
tomcat\conf\web.xml里:
<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>
2.直接调用windows函数
<input type="button" value="print"
onclick="window.focus();window.print()">3.调用js
//放在head 与 head 之间
<SCRIPT LANGUAGE=javascript>
<!--
function setPrint()
{
WB.ExecWB(8,1);
}
function previewPrint()
{
WB.ExecWB(7,1)
}
//-->
</SCRIPT>
//放在body与body之间
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WB width=0>
</OBJECT>
<INPUT type="button" value="打印设置" id=button1 name=button1 onclick="setPrint();">
<INPUT type="button" value="打印预览" id=button2 name=button2 onclick="previewPrint();">
4,如果要求很高,就用专业报表工具,不过价格不便宜
{
printStr = area.getText().trim();
if (printStr != null && printStr.length() > 0)
{
PAGES = getPagesCount(printStr);
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = printService.createPrintJob();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(this, flavor, das); try
{
job.print(doc, pras);
}
catch(PrintException pe)
{
pe.printStackTrace();
}
}
else
{
JOptionPane.showConfirmDialog(null, "Sorry, Printer Job is Empty, Print Cancelled!", "Empty"
, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
}
} private void printFileAction()
{
JFileChooser fileChooser = new JFileChooser(SystemProperties.USER_DIR);
fileChooser.setFileFilter(new com.szallcom.file.JavaFilter());
int state = fileChooser.showOpenDialog(this);
if (state == fileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200, printService
, defaultService, flavor, pras);
if (service != null)
{
try
{
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(file);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
} private void printFrameAction()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Properties props = new Properties();
props.put("awt.print.printer", "durango");
props.put("awt.print.numCopies", "2");
if(kit != null)
{
PrintJob printJob = kit.getPrintJob(this, "Print Frame", props);
if(printJob != null)
{
Graphics pg = printJob.getGraphics();
if(pg != null)
{
try
{
this.printAll(pg);
}
finally
{
pg.dispose();
}
}
printJob.end();
}
}
} private void exitApp()
{
this.setVisible(false);
this.dispose();
System.exit(0);
} public static void main(String[] args)
{
(new PrintTest()).setVisible(true);
}
}
你说的报表工具,我用google.com没有找到,可不可以告诉地址呀:)
目前版本为1.0.2,含如下几部分:
OpenOffice.org Writer ------------ Word
OpenOffice.org Calc ------------ Excel
OpenOffice.org Draw
OpenOffice.org Global
OpenOffice.org HTML Editor
OpenOffice.org Impress
OpenOffice.org Math
值得一提的是,它源码公开,可免费嵌入到你的软件中去.通过你的代码来控制其内容.
支持VC++/Java/StarBasic进行开发.
你的方法我已经用了,而且我看了整个的原文,但是我有点不明白,如果我的表格已经生成,我如何把它分解为frame,files等部分,进而打印出整个表格文档呢,可不可以指点一二。