如何打印 excel Workbook
无需在后台打开,使用 Java
?
请注意,我需要打印所有的工作表,而不仅仅是 Activity 的工作表。
我尝试过 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
如下 -![enter image description here](/image/xB57g.png)
上面的代码只会打印
active sheet
即sheet # 5
.如何使用 Jexcel
打印 Sheet 5, 6
和 7
?非常感谢!
最佳答案
在编写代码之前,我们必须下载 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();
}
}
}
关于java - 在不打开excel文档的情况下用java打印整个Excel工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34355619/