diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/ExcelFileSection.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/ExcelFileSection.java index c18d67780c566a0023b7eab4151e6024018390b5..1f81c132aa134034f6395e1a1e343319a81ebc22 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/ExcelFileSection.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/ExcelFileSection.java @@ -28,6 +28,7 @@ import org.apache.poi.ss.usermodel.Cell; 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 ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked; import ch.systemsx.cisd.common.exceptions.UserFailureException; @@ -76,12 +77,12 @@ public class ExcelFileSection return end; } - public static ExcelFileSection createFromInputStream(InputStream stream, String sectionName) + public static ExcelFileSection createFromInputStream(InputStream stream, String sectionName, + String fileName) { try { - POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream); - Workbook wb = new HSSFWorkbook(poifsFileSystem); + Workbook wb = getWorkBook(stream, fileName); Sheet sheet = wb.getSheetAt(0); return new ExcelFileSection(sheet, sectionName, 0, sheet.getLastRowNum()); } catch (IOException e) @@ -90,13 +91,26 @@ public class ExcelFileSection } } - public static List<ExcelFileSection> extractSections(InputStream stream, String excelSheetName) + private static final Workbook getWorkBook(InputStream stream, String fileName) + throws IOException + { + if (fileName.endsWith("xlsx")) + { + return new XSSFWorkbook(stream); + } else + { + POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream); + return new HSSFWorkbook(poifsFileSystem); + } + } + + public static List<ExcelFileSection> extractSections(InputStream stream, String excelSheetName, + String fileName) { List<ExcelFileSection> sections = new ArrayList<ExcelFileSection>(); try { - POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream); - Workbook wb = new HSSFWorkbook(poifsFileSystem); + Workbook wb = getWorkBook(stream, fileName); Sheet sheet = null; if (excelSheetName == null) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/SampleUploadSectionsParser.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/SampleUploadSectionsParser.java index 0e4c2c8c9672cf61bd6ccaf8eab1ded116fda1be..db4bfe60f3c58cf46951539e008d893669c86572 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/SampleUploadSectionsParser.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/SampleUploadSectionsParser.java @@ -191,17 +191,18 @@ public class SampleUploadSectionsParser new ArrayList<BatchRegistrationResult>(uploadedFiles.size()); for (final NamedInputStream multipartFile : uploadedFiles) { - if (multipartFile.getOriginalFilename().toLowerCase().endsWith("xls")) + final String fileName = multipartFile.getOriginalFilename().toLowerCase(); + if (fileName.endsWith("xls") || fileName.endsWith("xlsx")) { List<ExcelFileSection> sampleSections = new ArrayList<ExcelFileSection>(); if (sampleType.isDefinedInFileEntityTypeCode()) { sampleSections.addAll(ExcelFileSection.extractSections( - multipartFile.getInputStream(), excelSheetName)); + multipartFile.getInputStream(), excelSheetName, fileName)); } else { sampleSections.add(ExcelFileSection.createFromInputStream( - multipartFile.getInputStream(), sampleType.getCode())); + multipartFile.getInputStream(), sampleType.getCode(), fileName)); } int sampleCounter = 0; Map<String, String> defaults = Collections.emptyMap(); @@ -224,8 +225,7 @@ public class SampleUploadSectionsParser + fs.getSectionName() + ")"; final List<NewSample> loadedSamples = excelFileLoader.load(fs.getSheet(), fs.getBegin(), fs.getEnd(), - multipartFile.getOriginalFilename() + sectionInFile, - defaults); + fileName + sectionInFile, defaults); if (loadedSamples.size() > 0) { newSamples.add(new NewSamplesWithTypes(typeFromSection, loadedSamples)); @@ -233,9 +233,9 @@ public class SampleUploadSectionsParser } } } - results.add(new BatchRegistrationResult(multipartFile.getOriginalFilename(), String - .format("%s of %d sample(s) is complete.", operationKind.getDescription(), - sampleCounter))); + results.add(new BatchRegistrationResult(fileName, String.format( + "%s of %d sample(s) is complete.", operationKind.getDescription(), + sampleCounter))); } else { List<FileSection> sampleSections = new ArrayList<FileSection>(); @@ -269,9 +269,8 @@ public class SampleUploadSectionsParser sampleSections.size() == 1 ? "" : " (section:" + fs.getSectionName() + ")"; final List<NewSample> loadedSamples = - tabFileLoader.load( - new DelegatedReader(reader, multipartFile - .getOriginalFilename() + sectionInFile), defaults); + tabFileLoader.load(new DelegatedReader(reader, fileName + + sectionInFile), defaults); if (loadedSamples.size() > 0) { newSamples.add(new NewSamplesWithTypes(typeFromSection, loadedSamples)); @@ -279,9 +278,9 @@ public class SampleUploadSectionsParser } } } - results.add(new BatchRegistrationResult(multipartFile.getOriginalFilename(), String - .format("%s of %d sample(s) is complete.", operationKind.getDescription(), - sampleCounter))); + results.add(new BatchRegistrationResult(fileName, String.format( + "%s of %d sample(s) is complete.", operationKind.getDescription(), + sampleCounter))); } } return results; diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/MaterialUploadSectionsParser.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/MaterialUploadSectionsParser.java index 427de31285332bd7d956cd4fdf17c65865615192..ae76cf4783ab7ea02211765616d1d8e06577c8ea 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/MaterialUploadSectionsParser.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/parser/MaterialUploadSectionsParser.java @@ -106,19 +106,21 @@ public class MaterialUploadSectionsParser { final List<BatchRegistrationResult> results = new ArrayList<BatchRegistrationResult>(uploadedFiles.size()); + for (final NamedInputStream multipartFile : uploadedFiles) { - if (multipartFile.getOriginalFilename().toLowerCase().endsWith("xls")) + final String fileName = multipartFile.getOriginalFilename().toLowerCase(); + if (fileName.endsWith("xls") || fileName.endsWith("xlsx")) { List<ExcelFileSection> materialSections = new ArrayList<ExcelFileSection>(); if (materialType.isDefinedInFileEntityTypeCode()) { materialSections.addAll(ExcelFileSection.extractSections( - multipartFile.getInputStream(), excelSheetName)); + multipartFile.getInputStream(), excelSheetName, fileName)); } else { materialSections.add(ExcelFileSection.createFromInputStream( - multipartFile.getInputStream(), materialType.getCode())); + multipartFile.getInputStream(), materialType.getCode(), fileName)); } int materialCounter = 0; Map<String, String> defaults = Collections.emptyMap(); @@ -150,8 +152,7 @@ public class MaterialUploadSectionsParser + fs.getSectionName() + ")"; final List<NewMaterial> loadedMaterials = excelFileLoader.load(fs.getSheet(), fs.getBegin(), fs.getEnd(), - multipartFile.getOriginalFilename() + sectionInFile, - defaults); + fileName + sectionInFile, defaults); if (loadedMaterials.size() > 0) { newMaterials.add(new NewMaterialsWithTypes(typeFromSection, @@ -160,8 +161,8 @@ public class MaterialUploadSectionsParser } } } - results.add(new BatchRegistrationResult(multipartFile.getOriginalFilename(), String - .format("Registration of %d material(s) is complete.", materialCounter))); + results.add(new BatchRegistrationResult(fileName, String.format( + "Registration of %d material(s) is complete.", materialCounter))); } else { @@ -205,9 +206,8 @@ public class MaterialUploadSectionsParser materialSections.size() == 1 ? "" : " (section:" + fs.getSectionName() + ")"; final List<NewMaterial> loadedMaterials = - tabFileLoader.load( - new DelegatedReader(reader, multipartFile - .getOriginalFilename() + sectionInFile), defaults); + tabFileLoader.load(new DelegatedReader(reader, fileName + + sectionInFile), defaults); if (loadedMaterials.size() > 0) { newMaterials.add(new NewMaterialsWithTypes(typeFromSection, @@ -216,8 +216,8 @@ public class MaterialUploadSectionsParser } } } - results.add(new BatchRegistrationResult(multipartFile.getOriginalFilename(), String - .format("Registration of %d material(s) is complete.", materialCounter))); + results.add(new BatchRegistrationResult(fileName, String.format( + "Registration of %d material(s) is complete.", materialCounter))); } } return results;