如何用java来打印excel

2025-04-01 20:01:14
推荐回答(2个)
回答1:

import java.io.File;
 
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
 
/**
 * Excel导出(你需要引入jxl的jar包)
 */
public class Test {
 
    public static void main(String[] args)
    {
        Test test = new Test();
        test.exportExcel();
    }
 
    /**
     * 导出(导出到磁盘)
     */
    public void exportExcel() {
        WritableWorkbook book = null;
        try {
            // 打开文件
            book = Workbook.createWorkbook(new File("D:/测试.xls"));
            // 生成名为"学生"的工作表,参数0表示这是第一页
            WritableSheet sheet = book.createSheet("学生", 0);
            // 指定单元格位置是第一列第一行(0, 0)以及单元格内容为张三
            Label label = new Label(0, 0, "张三");
            // 将定义好的单元格添加到工作表中
            sheet.addCell(label);
            // 保存数字的单元格必须使用Number的完整包路径
            jxl.write.Number number = new jxl.write.Number(1, 0, 30);
            sheet.addCell(number);
            // 写入数据并关闭文件
            book.write();
        } catch (Exception e) {
            System.out.println(e);
        }finally{
            if(book!=null){
                try {
                    book.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } 
            }
        }
    }
}

回答2:

引用Spire.Xls.jar, Java打印Excel:

  1. import com.spire.xls.Workbook;

  2. import javax.print.PrintService;

  3. import java.awt.print.PageFormat;

  4. import java.awt.print.Paper;

  5. import java.awt.print.PrinterException;

  6. import java.awt.print.PrinterJob;


  7. public class PrintExcel {


  8.     public static void main(String[] args) throws Exception{


  9.         //Create a workbook and load an Excel file

  10.         Workbook workbook = new Workbook();

  11.         workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.xlsx");


  12.         //Create a PrinterJob object

  13.         PrinterJob printerJob = PrinterJob.getPrinterJob();


  14.         //Specify printer name

  15.         PrintService myPrintService = findPrintService("\\\\192.168.1.104\\HP LaserJet P1007");

  16.         printerJob.setPrintService(myPrintService);


  17.         //Create a PageFormat object and set it to the default size and orientation

  18.         PageFormat pageFormat  = printerJob.defaultPage();


  19.         //Return a copy of the Paper object associated with this PageFormat.

  20.         Paper paper = pageFormat .getPaper();


  21.         //Set the imageable area of this Paper.

  22.         paper.setImageableArea(0,0,pageFormat .getWidth(),pageFormat .getHeight());


  23.         //Set the Paper object for this PageFormat.

  24.         pageFormat .setPaper(paper);


  25.         //Set the number of copies

  26.         printerJob .setCopies(1);


  27.         //Call painter to render the pages in the specified format

  28.         printerJob .setPrintable(workbook,pageFormat);


  29.         //execute print

  30.         try {

  31.             printerJob.print();

  32.         } catch (PrinterException e) {

  33.             e.printStackTrace();

  34.         }

  35.     }

  36.     //Get print service by printer name

  37.     private static PrintService findPrintService(String printerName) {


  38.         PrintService[] printServices = PrinterJob.lookupPrintServices();

  39.         for (PrintService printService : printServices) {

  40.             if (printService.getName().equals(printerName)) {

  41.                 return printService;

  42.             }

  43.         }

  44.         return null;

  45.     }

  46. }