我编写了一个程序,它接收.pdf文件,并将pdf中的某些数据写入Excel工作表的列中。
我无法找出一个错误,当选择多个.pdf时,它会填充所有列,只选择最终的pdf。
I.E:选择3个pdf(a,b,c):创建的excel有3列,所有列都包含来自pdf c的相应数据。
我正在寻找这个:
|column 1|column 2 |column 3|
|data a | data b | data c|
我认为我的过于复杂的循环结构存在问题。
这是我的第一个程序,代码有点乱,但这里有一个片段:
String[] listString = (String[]) list.toArray(new String[0]);
String[] ionString = (String[]) Ion.toArray(new String[0]);
FileInputStream template = new FileInputStream(new File(templatePath));
HSSFWorkbook workbook = new HSSFWorkbook(template);
HSSFSheet worksheet = workbook.getSheetAt(0);
int totalRowNum = worksheet.getPhysicalNumberOfRows();
System.out.println("Last row at: " + totalRowNum);
for (int r = 0; r < totalRowNum; r++) { // check each row
Row rw = worksheet.getRow(r);
System.out.println("Row Number: " + (r + 1));
if (rw == null) {
System.out.println("Row ERROR");
continue;
}
System.out.println("No Row erros: Line 693");
for (int x = 0; x < size; x++) { //check each cell
Cell c = rw.getCell(x);
if (c == null) { //if cell is null, make it Blank
c = rw.getCell(x, Row.CREATE_NULL_AS_BLANK);
System.out.println("Converting Null cells to Blank");
}
System.out.println("No Cell Erros: Line 699");
if (c.getCellType() == Cell.CELL_TYPE_BLANK) { //if cell is blank
System.out.println("No more Null Cells");
for (int n = 1; n < size + 1; n++) { //fill blank cell
int i = 0;
System.out.println("Line 705, Should be populating");
//fills each cell n with string from String array index i
HSSFCell cellB1 = name.createCell((short) n);
cellB1.setCellValue(listString[i]);
i++;
/** ..... a bunch of these for each cell ..... **/
}
}
}
}
FileOutputStream fileOut = new FileOutputStream(templatePath);
//在outputStream上方写入模板
System.out.println("Your file is located at: " + templatePath);
workbook.write(fileOut);
fileOut.close();
System.out.println("*************COMPLETE*************");
}
根据要求,这里是我从以下位置获取listString的地方:
首先来自JavaFX:
private List<File> showMultipleFileChooser() throws FileNotFoundException {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select PDF files");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("PDF Files", "*.pdf"));
List<File> selectedFiles = fileChooser.showOpenMultipleDialog(savedStage);
// List of files selected
if (selectedFiles !=null){ //if files are selected
for (int i = 0; i < selectedFiles.size(); i++){
String fPath = selectedFiles.get(i).toString();
//gets file path in String format
int size = selectedFiles.size();
System.out.println("LOOK AT ME " + fPath);
PDFTest.pdfTotxt(fPath, size, templatePath);
}
}
然后转换为.csv并使用CSVParser:
List<String> list = new ArrayList<String>(); //creates ArrayList<String>
List<String> Ion = new ArrayList<String>();
FileResource csv = new FileResource ("C:\\PDFTester\\CSV.csv");
//pdf has already been converted to .csv named CSV.csv
CSVParser parser = csv.getCSVParser(false); //Parses CSV
for (CSVRecord record : parser) {
a = record.get(0);
if (a.contains("Material:")){ //if index 0 is the one i'm looking for
System.out.println(a + " " + record.get(1));
material = record.get(1);
//saves the data from that line into global String named material
Ion.add(record.get(0));
list.add(material); //adds data i want into List<String> list
}
//A bunch more like this...