在不打开Excel文档的情况下在java中打印整个Excel工作簿

时间:2015-12-18 12:22:26

标签: java excel apache-poi jexcelapi

如何使用excel Workbook打印Java而不在背景中打开Desktop.print(FileName);

请注意,我需要打印所有纸张,而不仅仅是活动纸张。

我尝试了类似Apache POI API的内容,但这似乎并不适用于所有工作表。

我正在使用Excel通过Java创建Apace POI个文件。

非常感谢!

更新

为了解释我的问题,我使用Excel打印HSSFSheet sheet = workbook.getSheet("Sheet5"); HSSFRow row21 = sheet.getRow((short)5); row21.getCell((short)2).setCellValue(notcorrect); desktop.print(new File(filename)); HSSFSheet sheet7 = workbook.getSheet("Sheet7"); HSSFRow row20 = sheet7.getRow((short)3); row20.getCell((short)2).setCellValue(logreference); row20.getCell((short)4).setCellValue(ISSUENUMBER); desktop.print(new File(filename)); HSSFSheet sheet8 = workbook.getSheet("Sheet8"); HSSFRow row22 = sheet8.getRow((short)3); row22.getCell((short)2).setCellValue(logreference); row22.getCell((short)4).setCellValue(ISSUENUMBER); desktop.print(new File(filename)); 文件为 -

active workbook

但这似乎仅适用于workbook。如果我Excel file中的active sheet如下 -

enter image description here

上面的代码只会打印sheet # 5,即Jexcel。如何使用Sheet 5, 6打印7 AND export PGPASSWORD=the_password if [[ `psql -h 127.0.0.1 -d postgres -U username -p 5432 -tAc "SELECT 1 FROM pg_database WHERE datname='test_djangoprojectname'"` == "1" ]] then psql -h 127.0.0.1 -d postgres -U username -p 5432 -a -w -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'test_djangoprojectname' AND pid <> pg_backend_pid();" psql -h 127.0.0.1 -d postgres -U username -p 5432 -a -w -c "DROP DATABASE test_djangoprojectname;" fi

非常感谢!

1 个答案:

答案 0 :(得分:0)

在编写代码之前,我们必须下载 apache-poi jars https://poi.apache.org/download.html 下载 - poi-bin-5.0.0-20210120.tar.gz(当前版本)

package test1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class P1 {

    public static void main(String[] args) throws IOException {
        
        //Path to excel from system = /Users/user/Downloads/test1.xlsx"
        File f = new File("/Users/user/Downloads/test1.xlsx");
        FileInputStream fis = new FileInputStream(f);
        XSSFWorkbook wb = new XSSFWorkbook(fis);

        //June2021 is my sheet name of excel
        XSSFSheet sheet = wb.getSheet("June2021");
        
        int RowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
        for(int i=0; i<= RowCount; i++) {
            System.out.print("Row "+ i + " data is : ");
            int cellCount = sheet.getRow(i).getLastCellNum();
            
            for(int j=0; j<cellCount; j++) {
                XSSFCell c1 = sheet.getRow(i).getCell(j);
                System.out.print(c1 + " ");
            }
            System.out.println();
        }
    }
}