Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook.

Prototype

public XSSFWorkbook(PackagePart part) throws IOException 

Source Link

Document

Constructs a XSSFWorkbook object using Package Part.

Usage

From source file:com.frameworkset.platform.util.POIExcelUtil.java

License:Open Source License

/**
 * ??Workbook//w ww .  j a v a2 s  . com
 * 
 * @param file
 *            MultipartFile
 * @return Workbook
 * @throws Exception
 */
public static Workbook getWorkbookByFileContentType(MultipartFile file) throws Exception {
    if (file.getOriginalFilename().endsWith("xlsx")) {
        return new XSSFWorkbook(file.getInputStream());
    } else if (file.getContentType().contains(EXCEL_TYPE_2003_CONTENT_TYPE)
            || file.getContentType().contains("octet-stream")) {
        return new HSSFWorkbook(file.getInputStream());
    } else if (file.getContentType().contains(EXCEL_TYPE_2007_CONTENT_TYPE)) {
        return new XSSFWorkbook(file.getInputStream());
    } else {
        return null;
    }

}

From source file:com.gettec.fsnip.fsn.service.market.impl.ResourceServiceImpl.java

/**
 * excel?//from  w ww. ja v a  2 s .c om
 */
@Override
public List<TestProperty> paseExcelByResource(Resource resource) throws ServiceException {
    if (resource == null) {
        return null;
    }
    List<TestProperty> items = new ArrayList<TestProperty>();
    try {
        if (resource.getFileName().toUpperCase().endsWith("XLS")) {
            WorkbookSettings ws = new WorkbookSettings();
            ws.setEncoding("iso-8859-1");
            Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(resource.getFile()), ws);
            Sheet sheet = wb.getSheet(0);
            if (sheet.getRows() < 1) {
                return null;
            }
            for (int i = 1; i < sheet.getRows(); i++) {
                if (sheet.getCell(0, i).getContents().trim().length() < 1
                        || sheet.getCell(3, i).getContents().trim().length() < 1) {
                    continue;
                }
                TestProperty itm = new TestProperty();
                itm.setName(sheet.getCell(0, i).getContents().trim());
                itm.setUnit(sheet.getCell(1, i).getContents().trim());
                itm.setTechIndicator(sheet.getCell(2, i).getContents().trim());
                itm.setResult(sheet.getCell(3, i).getContents().trim());
                itm.setAssessment("?");
                if (sheet.getCell(4, i).getContents().trim().contains("?")) {
                    itm.setAssessment("??");
                }
                itm.setStandard(sheet.getCell(5, i).getContents().trim());
                items.add(itm);
            }
            if (wb != null) {
                wb.close();
            }
        } else {
            XSSFRow row = null;
            XSSFWorkbook xwb = new XSSFWorkbook(new ByteArrayInputStream(resource.getFile()));
            XSSFSheet sheet = xwb.getSheetAt(0);
            for (int i = sheet.getFirstRowNum(); i <= sheet.getPhysicalNumberOfRows(); i++) {
                row = sheet.getRow(i);
                if (row == null || row.getCell(0) == null || row.getCell(0).toString().trim().length() < 1
                        || row.getCell(3).toString().trim().length() < 1) {
                    continue;
                }
                TestProperty itm = new TestProperty();
                itm.setName(row.getCell(0).toString().trim());
                itm.setUnit(row.getCell(1).toString().trim());
                itm.setTechIndicator(row.getCell(2).toString().trim());
                itm.setResult(row.getCell(3).toString().trim());
                itm.setAssessment("?");
                if (row.getCell(4).toString().trim().contains("?")) {
                    itm.setAssessment("??");
                }
                itm.setStandard(row.getCell(5).toString().trim());
                items.add(itm);
            }
        }
    } catch (Exception e) {
        throw new ServiceException("MkTestResourceServiceImpl.paseExcelByResource() " + e.getMessage(), e);
    }
    return items;
}

From source file:com.gettec.fsnip.fsn.service.sample.impl.ImportExcelServiceImpl.java

@Override
public List<ExcelFileVO> getWorkbook(MultipartFile[] excelFiles) {
    List<ExcelFileVO> list = new ArrayList<ExcelFileVO>();
    for (MultipartFile file : excelFiles) {
        ExcelFileVO excelFileVO = null;//from   w ww .ja v a  2 s  . c om
        if (XLSX_CONTENTTYPE.equals(file.getContentType())) {
            XSSFWorkbook workbook = null;
            try {
                workbook = new XSSFWorkbook(file.getInputStream());
                excelFileVO = ReadFileTool.analyticalWorkbook(workbook);
                excelFileVO.setFileName(file.getOriginalFilename());
            } catch (IOException e) {
                excelFileVO = new ExcelFileVO();
                excelFileVO.setFlag(false);
                excelFileVO.setMessage("????");
            }
        } else if (XLS_CONTENTTYPE.equals(file.getContentType())) {
            HSSFWorkbook workbook = null;
            try {
                workbook = new HSSFWorkbook(file.getInputStream());
                excelFileVO = ReadFileTool.analyticalWorkbook(workbook);
                excelFileVO.setFileName(file.getOriginalFilename());
            } catch (IOException e) {
                excelFileVO = new ExcelFileVO();
                excelFileVO.setFlag(false);
                excelFileVO.setMessage("????");
            }
        } else {
            excelFileVO = new ExcelFileVO();
            excelFileVO.setFlag(false);
            excelFileVO.setMessage("????");
        }
        list.add(excelFileVO);
    }
    return list;
}

From source file:com.github.cutstock.utils.ProfileUtils.java

License:Apache License

public static Profiles parseFile(String filePath) {
    Workbook workbook = null;/*www . j a  v a  2  s.c  o m*/
    Sheet sheet = null;
    // CutStockPlugin.getLogger().log(new Status(0,
    // CutStockPlugin.PLUGIN_ID, filePath));
    InputStream is = null;
    try {
        is = new FileInputStream(filePath);
        if (filePath.toLowerCase().endsWith(".xls")) {
            workbook = new HSSFWorkbook(is);
        } else {
            workbook = new XSSFWorkbook(is);
        }

    } catch (IOException e) {
        MessageDialog.openError(null, "ERROR",
                "?EXCEL,?????Excel");
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    sheet = workbook.getSheetAt(0);
    int sheetRowNum = sheet.getLastRowNum();
    Profiles proflies = new Profiles();
    for (int i = 1; i <= sheetRowNum; i++) {
        Row currentRow = sheet.getRow(i);
        if (currentRow != null) {
            Cell cell = currentRow.getCell(ColumnType.PROFILE_NAME);
            String name = (String) getCellValue(cell);
            if (StringUtil.Empty(name)) {
                break;
            }
            cell = currentRow.getCell(ColumnType.PROFILE_CODE_DATA);
            String codeData = (String) getCellValue(cell);
            cell = currentRow.getCell(ColumnType.PROFILE_COLOR);
            String color = (String) getCellValue(cell);

            cell = currentRow.getCell(ColumnType.PROFILE_WIDTH);
            double width = (Double) getCellValue(cell);
            cell = currentRow.getCell(ColumnType.PROFILE_AMOUNT);
            int amount = ((Double) getCellValue(cell)).intValue();

            proflies.add(name, codeData, color, new BigDecimal(width), amount);
        }
    }
    return proflies;
}

From source file:com.github.jaydsolanki.excelio.ExcelIO.java

public ExcelIO(InputStream is, WorkbookType workbookType) throws IOException {
    switch (workbookType) {
    case OFFICE_1997_2007:
        workbook = new HSSFWorkbook(is);
        break;//from   w ww . j av  a  2  s  .c om
    case OFFICE_2013_AND_ABOVE:
        workbook = new XSSFWorkbook(is);
    }
}

From source file:com.github.jaydsolanki.excelio.ExcelIO.java

public void loadExcelFile(WorkbookType workbookType)
        throws FileNotFoundException, IOException, IllegalArgumentException {
    if (workbookType == null) {
        throw new IllegalArgumentException(
                "The file does not seem to have a valid extension! please pass argument of file type in function loadExcelFile(WorkbookType workbookType)");
    }//from www. j  a v a  2  s  .  co  m
    switch (workbookType) {
    case OFFICE_1997_2007:
        workbook = new HSSFWorkbook(new FileInputStream(new File(excelFilePath)));
        break;
    case OFFICE_2013_AND_ABOVE:
        workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath)));
    }
}

From source file:com.github.poi.LoadPasswordProtectedXlsx.java

License:Apache License

public static void main(String[] args) {
    try {//from w w  w . j av  a  2s .  c  o m
        if (args.length != 2) {
            throw new Exception("Expected 2 params: filename and password");
        }
        XlsxUtils.checkTempFiles();
        String filename = args[0];
        String password = args[1];
        try (FileInputStream fis = new FileInputStream(filename);
                InputStream unencryptedStream = XlsxUtils.decrypt(fis, password);
                AesZipFileZipEntrySource source = AesZipFileZipEntrySource
                        .createZipEntrySource(unencryptedStream);
                OPCPackage pkg = OPCPackage.open(source);
                XSSFWorkbook workbook = new XSSFWorkbook(pkg)) {
            System.out.println("sheet count: " + workbook.getNumberOfSheets());
        }
        XlsxUtils.checkTempFiles();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:com.github.xiilei.ecdiff.Processor.java

License:Apache License

private Workbook readExcelFileByext(File file) throws IOException {
    String ext = getExt(file.getName());
    Workbook wb = null;//from  w w w . jav  a  2  s  .  c  o  m
    switch (ext) {
    case "xls":
        wb = new HSSFWorkbook(new FileInputStream(file));
        break;
    case "xlsx":
        wb = new XSSFWorkbook(new FileInputStream(file));
        break;
    default:
        throw new IOException("Does not recognize the extension:" + ext);
    }
    return wb;
}

From source file:com.globalsight.util.ExcelUtil.java

License:Apache License

public static Workbook getWorkbook(String filename) {
    Workbook workbook = null;//w w w  .jav a  2  s  . c  o m
    if (StringUtil.isEmpty(filename) || !isExcel(filename))
        return null;
    File file = null;
    try {
        file = new File(filename);
        if (!file.exists() || file.isDirectory())
            return null;
        InputStream is = new FileInputStream(file);
        if (isXls(filename))
            workbook = new HSSFWorkbook(is);
        else
            workbook = new XSSFWorkbook(OPCPackage.open(file));
    } catch (Exception e) {
        logger.error("Cannot open Excel file correctly.", e);
    }

    return workbook;
}

From source file:com.globalsight.util.ExcelUtil.java

License:Apache License

public static Workbook getWorkbook(String filename, InputStream is) {
    Workbook workbook = null;/*from w  ww  .java  2  s  .c o m*/
    if (StringUtil.isEmpty(filename) || !isExcel(filename) || is == null)
        return null;

    File file = null;
    try {
        file = new File(filename);
        if (!file.exists() || file.isDirectory())
            return null;
        if (isXls(filename))
            workbook = new HSSFWorkbook(is);
        else
            workbook = new XSSFWorkbook(OPCPackage.open(is));
    } catch (Exception e) {
        logger.error("Cannot open Excel file correctly.", e);
    }

    return workbook;
}