我有一个Microsoft Excel工作表,其中包含以下形式的一些数据:
String
- String
- Integer
我希望能够选择一个项目,然后以PDF格式打印其信息:
姓名:String
日期:String
ID:int
有可能吗?如果有,怎么样?如果不是,是否有一些允许我这样做的java库?
答案 0 :(得分:0)
好的,我想给你一个小例子,它可以在XLS
文档上找到字符串值:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
/**
*
* @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
* @version 1.0
*/
public class App {
public static final String USER_HOME = System.getProperty("user.home");
public static final void main(String[] args) {
FileInputStream fis = null;
try {
// change the path, where your xls file is located
fis = new FileInputStream(new File(USER_HOME + "/test/workbook.xls"));
// create a new Workbook
HSSFWorkbook workbook = new HSSFWorkbook(fis);
// get the first Sheet, if you have more, then 1 or 2 or ....
HSSFSheet sheet = workbook.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
// go through all rows
for (int i = 0; i < lastRowNum; i++) {
if (sheet.getRow(i) != null) {
HSSFRow row = sheet.getRow(i);
// get the last cell of the current row
short lastCellNum = row.getLastCellNum();
for(int j = 0; j < lastCellNum; j++) {
// if the current column is != null and contains a String and
// equals("Espresso") (i added this for test in my example) then
// print "Found", here you can start to create the PDF
if(row.getCell(j) != null && row.getCell(j).getCellType() == Cell.CELL_TYPE_STRING && row.getCell(j).getStringCellValue().equalsIgnoreCase("Espresso")) {
System.out.println("Found");
}
}
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
如果您想使用Maven,那么这里是pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.professional_webworkx.apachepoi</groupId>
<artifactId>ApachePOIDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-contrib</artifactId>
<version>3.5-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
否则,您必须手动将Apache POI Jar添加到您的项目中。
使用Java创建PDF文件时,您可以查看iText或PDFBox两个库还有Maven依赖项,您可以将其添加到pom.xml
。
我希望这可以帮助您从自己的应用开始,如果您遇到特殊问题,请再次询问,但请确保问题不会得到解答。
帕特里克