java实现Excel和图片的打印预览

今天忙了一天,终于把打印预览的功能给实现了,因为昨天写了两个打印的方法,感觉到既然打印要预览才可以的
一:Excel打印预览:
首先还是一样,引入JCom.jar,并把bin文件放入JDK/bin目录下才可以
代码如下:
/**
* 引入JCom操作Excel的jar
*/
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;  
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;  
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook;  
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks;  
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet;  
/**
* 预览Excel的类
* @author tianmaochun
*
*/
public class PrintViewExcel{

    /**
     * 实线Excel预览的类
     * @param fileName[图片的路径]
     *
     */
public void getExcelView(String fileName){
ReleaseManager rm = new ReleaseManager();  
        try{
        //创建可加载的组件
        ExcelApplication excel = new ExcelApplication(rm);  
             //创建工作薄对象
        ExcelWorkbooks xlBooks = excel.Workbooks();
        //打开工作薄
             ExcelWorkbook xlBook = xlBooks.Open(fileName);
             //打印
             ExcelWorksheet xlSheet = excel.ActiveSheet();
         //显示Excel
             excel.Visible(true);
             //输出页面
         xlSheet.PrintOut(1,1,1,true);   
         //关闭
         xlBook.Close(false,  null,  false);
         excel.Quit();
    }catch  (Exception  e)  {
         e.printStackTrace();
            
    }finally  {
         rm.release();
    }
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintViewExcel prview = new PrintViewExcel();
prview.getExcelView("f:/tian.xls");

}

}
二:打印预览图片:
package com.gyhq.soft;

import java.awt.BasicStroke;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 打印预览窗体类
* @author tianmaochun
*
*/
public class PrintViewImage extends JDialog implements Printable,ActionListener{

//初始化页面的大小和方向
private PageFormat pf = null;
private double sca =1;
//初始化一个img对象
private Image img = null;
//定义一个Button
private JButton but = null;
//定义一个图片的路径
String fileName = null;
/**
* 构造方法
*/
public PrintViewImage(String fileName){
this.fileName = fileName;
initFrame();
}
public void getView(){
//实例化
pf = new PageFormat();
//获取预览图片的路径
img = Toolkit.getDefaultToolkit().getImage(fileName);
}
/**
* 初始化页面的方法
*/
private void initFrame() {
//定义一个容器
        Container container = this.getContentPane();
        this.setTitle("打印预览");
        this.setResizable(false);
        but = new JButton("打印");
        but.setBounds(600, 715, 100, 30);
        but.addActionListener(this);
        this.add(but);
        //实例化预览类
        PreviewCanvas priView = new PreviewCanvas();
        //添加到容器之中去
        container.add(priView);
        //显示
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        //关闭
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
/**
* 预览类
* @author tianmaochun
*
*/
private class PreviewCanvas extends JPanel {
        //private static final long serialVersionUID = 20110208L;
        /**
         * 构造方法
         */
        public PreviewCanvas() {
        //设置窗体的大小
            this.setPreferredSize(new Dimension(800, 750));
        }

        /**
         * 画预览区域
         */
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.translate(10, 10);
            //获取页面宽度
            double width = pf.getWidth();
            //获取页面的高度
            double height = pf.getHeight();
            //返回左上方X
            double imageableX = pf.getImageableX();
            //返回左上方Y
            double imageableY = pf.getImageableY();
            //返回页面可成像区域的宽度
            double imageableWidth = pf.getImageableWidth();
            //返回页面可成像区域的高度
            double imageableHeight = pf.getImageableHeight();
            if (width > 800 || height >700) {
                double scaleWidth = (double) 800 / width;
                double scaleHeight = (double) 700 / height;
                sca = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
                width = width * sca;
                height = height * sca;
                imageableX = imageableX * sca;
                imageableY = imageableY * sca;
                imageableWidth = imageableWidth * sca;
                imageableHeight = imageableHeight * sca;
            }
            //画一个实线的矩形
            g2.drawRect(0, 0, (int) width+200, (int) height);
            float[] dash = { 5f, 5f };
            //定义虚线类型
            g2.setStroke(new BasicStroke(1f, BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_ROUND, 10f, dash, 0f));
            //画虚线矩形
            g2.drawRect((int) (imageableX - 1), (int) (imageableY - 1),
                    (int) (imageableWidth + 2)+200, (int) (imageableHeight + 2));
            try {
            //画相应的图片在框中
PrintViewImage.this.print(g2, pf, 0);
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        }
    }
/**
* 复写Printtable的方法
*/
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
//获取最左X坐标
double imageableX = pageFormat.getImageableX();
//获取最左Y坐标
double imageableY = pageFormat.getImageableY();
//获取图片的宽度
        int imgWidth = img.getWidth(this);
        //获取图片的高度
        int imgHeight = img.getHeight(this);
        //判断图片的大小是否大于页面的宽度
        if(imgWidth>800||imgHeight>750){
        //规定大小画图片在框中
        graphics.drawImage(img, (int) (imageableX * sca),(int) (imageableY * sca), 610,
                    570,  this);
        }else{
        //根据图片大小预览
        graphics.drawImage(img, (int) (imageableX * sca),(int) (imageableY * sca), (int) (imgWidth * sca),
                     (int) (imgHeight * sca),  this);
        }
        if (pageIndex == 0) {
        //从print页面中返回
            return Printable.PAGE_EXISTS;
        } else {
        //返回呈现的页面
            return Printable.NO_SUCH_PAGE;
        }

}
/**
* 按钮事件
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==but){
//实例化相应的类并调用打印相应的方法
PrintImage dp = new PrintImage();
dp.drawImage(fileName);
}

}  
/**
* 主函数的方法
* @param args
*/
public static void main(String[] args){
new PrintViewImage("img/tt.gif").getView();
}
}







你可能感兴趣的:(java实现)