Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Apache POI for XLSX support. Fixes #136 #175

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/SparseBitSet-1.2.jar
Binary file not shown.
Binary file added lib/commons-codec-1.15.jar
Binary file not shown.
Binary file added lib/commons-collections4-4.4.jar
Binary file not shown.
Binary file added lib/commons-compress-1.21.jar
Binary file not shown.
Binary file added lib/commons-io-2.11.0.jar
Binary file not shown.
Binary file added lib/commons-logging-1.2.jar
Binary file not shown.
Binary file added lib/commons-math3-3.6.1.jar
Binary file not shown.
Binary file added lib/curvesapi-1.07.jar
Binary file not shown.
Binary file added lib/jakarta.activation-2.0.1.jar
Binary file not shown.
Binary file added lib/jakarta.xml.bind-api-3.0.1.jar
Binary file not shown.
Binary file removed lib/jxl.jar
Binary file not shown.
Binary file added lib/log4j-api-2.18.0.jar
Binary file not shown.
Binary file added lib/poi-5.2.3.jar
Binary file not shown.
Binary file added lib/poi-ooxml-5.2.3.jar
Binary file not shown.
Binary file added lib/poi-ooxml-lite-5.2.3.jar
Binary file not shown.
Binary file added lib/slf4j-api-1.7.36.jar
Binary file not shown.
Binary file added lib/xmlbeans-5.1.1.jar
Binary file not shown.
74 changes: 43 additions & 31 deletions src/pattypan/panes/CreateFilePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import com.drew.metadata.exif.ExifSubIFDDirectory;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
Expand All @@ -40,13 +42,12 @@
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
import jxl.CellView;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import pattypan.Session;
import pattypan.Settings;
import pattypan.Template;
Expand Down Expand Up @@ -103,7 +104,7 @@ private WikiPane setActions() {
createSpreadsheet();
showOpenFileButton();
Settings.saveProperties();
} catch (IOException | BiffException | WriteException ex) {
} catch (IOException ex) {
addElement(new WikiLabel("create-file-error"));
Session.LOGGER.log(Level.WARNING,
"Error occurred during creation of spreadsheet file: {0}",
Expand Down Expand Up @@ -135,43 +136,49 @@ private void showOpenFileButton() {
nextButton.setVisible(true);
}

private void autoSizeColumn(int column, WritableSheet sheet) {
CellView cell = sheet.getColumnView(column);
cell.setAutosize(true);
sheet.setColumnView(column, cell);
private void autoSizeColumn(int column, Sheet sheet) {
sheet.autoSizeColumn(column);
}

private void createSpreadsheet() throws IOException, BiffException, WriteException {
File f = new File(Session.DIRECTORY, fileName.getText() + ".xls");
WritableWorkbook workbook = Workbook.createWorkbook(f);
private void createSpreadsheet() throws IOException {
File f = new File(Session.DIRECTORY, fileName.getText() + ".xlsx");
// Autosize appears to only be supported for OOXML workbooks, so making this specific instead of generic
// boolean xml = true;
// final Workbook workbook = xml ? new SXSSFWorkbook() : new HSSFWorkbook();
final SXSSFWorkbook workbook = new SXSSFWorkbook();

createDataSheet(workbook);
createTemplateSheet(workbook);

workbook.write();
try (OutputStream os = new FileOutputStream(f)){
workbook.write(os);
os.flush();
}
workbook.close();
Session.FILE = f;
}

/**
*
* @param workbook
* @throws WriteException
*/
private void createDataSheet(WritableWorkbook workbook) throws WriteException {
WritableSheet sheet = workbook.createSheet("Data", 0);
private void createDataSheet(SXSSFWorkbook workbook) {
SXSSFSheet sheet = workbook.createSheet("Data");

// first row (header)
Row header = sheet.createRow(0);
int column = 0;
for (String variable : Session.VARIABLES) {
sheet.addCell(new Label(column++, 0, variable));
Cell c = header.createCell(column++);
c.setCellValue(variable);
}

// next rows with path and name
int row = 1;
for (File file : Session.FILES) {
sheet.addCell(new Label(0, row, file.getAbsolutePath()));
sheet.addCell(new Label(1, row++, Util.getNameFromFilename(file.getName())));
Row cells = sheet.createRow(row++);
cells.createCell(0, CellType.STRING).setCellValue(file.getAbsolutePath());
cells.createCell(1, CellType.STRING).setCellValue(Util.getNameFromFilename(file.getName()));
}

if (Session.METHOD.equals("template")) {
Expand All @@ -181,7 +188,8 @@ private void createDataSheet(WritableWorkbook workbook) throws WriteException {
column = Session.VARIABLES.indexOf(tf.name);
row = 1;
for (File file : Session.FILES) {
sheet.addCell(new Label(column, row++, tf.value));
// TODO: We may not have the cells created yet here. Let's see what happens
sheet.getRow(row++).getCell(column).setCellValue(tf.value);
}
}
}
Expand All @@ -191,32 +199,36 @@ private void createDataSheet(WritableWorkbook workbook) throws WriteException {
if (column >= 0 && !Settings.getSetting("exifDate").isEmpty()) {
row = 1;
for (File file : Session.FILES) {
sheet.addCell(new Label(column, row++, getExifDate(file)));
// TODO: We may not have the cells created yet here. Let's see what happens
sheet.getRow(row++).getCell(column).setCellValue(getExifDate(file));
}
}

for (int num = 0; num < sheet.getColumns(); num++) {
sheet.trackAllColumnsForAutoSizing();
for (int num = 0; num < sheet.getRow(0).getLastCellNum(); num++) {
autoSizeColumn(num, sheet);
}
}

/**
*
* @param workbook
* @throws WriteException
*/
private void createTemplateSheet(WritableWorkbook workbook) throws WriteException {
WritableSheet templateSheet = workbook.createSheet("Template", 1);
templateSheet.addCell(new Label(0, 0, "'" + Session.WIKICODE));
// ^^
private void createTemplateSheet(SXSSFWorkbook workbook) {
SXSSFSheet templateSheet = workbook.createSheet("Template");
Row row = templateSheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("'" + Session.WIKICODE);
// ^^
// leading apostrophe prevents turning wikitext into formula in Excel

templateSheet.trackAllColumnsForAutoSizing();
autoSizeColumn(0, templateSheet);
}

/**
*
* @param filePath
* @param file
* @return
*/
private String getExifDate(File file) {
Expand Down
96 changes: 60 additions & 36 deletions src/pattypan/panes/LoadPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
*/
package pattypan.panes;

import freemarker.core.InvalidReferenceException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
Expand All @@ -43,13 +47,17 @@
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import pattypan.Session;
import pattypan.Settings;
import pattypan.UploadElement;
Expand Down Expand Up @@ -236,10 +244,9 @@ private void addInfo(String text, String cssClass) {
* @throws Exception when essential headers are missing
*/
private void readHeaders(Sheet sheet) throws Exception {
int columns = sheet.getColumns();
ArrayList<String> cols = new ArrayList<>();
for (int col = 0; col < columns; col++) {
cols.add(sheet.getCell(col, 0).getContents());
List<String> cols = new ArrayList<>();
for (Cell c : sheet.getRow(0)) {
cols.add(c.getStringCellValue());
}

if (cols.isEmpty()) {
Expand All @@ -264,18 +271,22 @@ private String getCellValue(Sheet sheet, int column, int row) {
formatDate.setTimeZone(TimeZone.getTimeZone("UTC"));
formatDateHour.setTimeZone(TimeZone.getTimeZone("UTC"));

Cell valueCell = sheet.getCell(column, row);
String value;
Cell valueCell = sheet.getRow(row).getCell(column);
String value = null;

if (valueCell.getType() == CellType.DATE) {
DateCell dateCell = (DateCell) valueCell;
if (valueCell != null) {
if (valueCell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(valueCell)) {
Date date = DateUtil.getJavaDate(valueCell.getNumericCellValue());
// FIXME: Restore more sophisticated date handling
value = formatDate.format(date);
//@TODO: more elegant hour detection
value = dateCell.getContents().contains(":")
? formatDateHour.format(dateCell.getDate())
: formatDate.format(dateCell.getDate());
} else {
value = sheet.getCell(column, row).getContents().trim();
}
// value = dateCell.getContents().contains(":")
// ? formatDateHour.format(dateCell.getDate())
// : formatDate.format(dateCell.getDate());
} else {
value = valueCell.getStringCellValue().trim();
}
}
return value;
}

Expand All @@ -294,9 +305,7 @@ private void loadSpreadsheet(File file) {
readSpreadSheet();
} catch (IOException ex) {
addInfo("File error: there are problems opening file. It may be corrupted.");
} catch (BiffException ex) {
addInfo("File error: file needs to be saved in binnary format. Please save your file in \"Excel 97-2003 format\"");
} catch (InvalidReferenceException ex) {
} catch (TemplateException ex) {
addInfo("File error: variables mismatch. Column headers variables must match wikitemplate variables.");
} catch (Exception ex) {
addInfo(ex.getMessage());
Expand All @@ -312,14 +321,27 @@ private void loadSpreadsheet(File file) {
*/
private ArrayList<Map<String, String>> readDescriptions(Sheet sheet) {
ArrayList<Map<String, String>> descriptions = new ArrayList<>();
int rows = sheet.getRows();
int columns = sheet.getColumns();
int rows = sheet.getLastRowNum();
int columns = sheet.getRow(0).getLastCellNum();

// Collect header labels
String[] labels = new String[columns];
Row cells = sheet.getRow(0);
for (int col = 0; col < columns; col++) {
Cell cell = cells.getCell(col);
if (cell != null) {
String value = cell.getStringCellValue();
labels[col] = value;
} else {
labels[col] = null;
}
}
for (int row = 1; row < rows; row++) {
Map<String, String> description = new HashMap();
cells = sheet.getRow(row);
for (int column = 0; column < columns; column++) {
String label = sheet.getCell(column, 0).getContents().trim();
if (label.isEmpty()) {
String label = labels[column];
if (label == null || label.isEmpty()) {
continue;
}
String value = getCellValue(sheet, column, row);
Expand All @@ -333,17 +355,19 @@ private ArrayList<Map<String, String>> readDescriptions(Sheet sheet) {
/**
* Reads spreadsheet stored in Session.FILE.
*/
private void readSpreadSheet() throws BiffException, IOException, Exception {
private void readSpreadSheet() throws IOException, TemplateException, Exception {
infoContainer.getChildren().clear();
Session.SCENES.remove("CheckPane");

WorkbookSettings ws = new WorkbookSettings();
ws.setEncoding("Cp1252");
InputStream inputStream = new BufferedInputStream(new FileInputStream(Session.FILE));
Workbook wb = FileMagic.valueOf(inputStream) == FileMagic.OOXML ? new XSSFWorkbook(inputStream)
: new HSSFWorkbook(new POIFSFileSystem(inputStream));

// ws.setEncoding("Cp1252"); // FIXME

try {
Workbook workbook = Workbook.getWorkbook(Session.FILE, ws);
Sheet dataSheet = workbook.getSheet(0);
Sheet templateSheet = workbook.getSheet(1);
Sheet dataSheet = wb.getSheetAt(0);
Sheet templateSheet = wb.getSheetAt(1);
readHeaders(dataSheet);
addFilesToUpload(readDescriptions(dataSheet), readTemplate(templateSheet));
} catch (IndexOutOfBoundsException ex) {
Expand All @@ -362,7 +386,7 @@ private void readSpreadSheet() throws BiffException, IOException, Exception {
*/
private Template readTemplate(Sheet sheet) throws Exception {
try {
String text = sheet.getCell(0, 0).getContents();
String text = sheet.getRow(0).getCell(0).getStringCellValue();
return new Template("wikitemplate", new StringReader(text), cfg);
} catch (ArrayIndexOutOfBoundsException ex) {
throw new Exception("Error: template in spreadsheet looks empty. Check if wikitemplate is present in second tab of your spreadsheet (first row and first column).");
Expand Down