Search in sources :

Example 81 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project cytoscape-impl by cytoscape.

the class ImportAttributeTableReaderTask method run.

@Override
public void run(TaskMonitor tm) throws Exception {
    tm.setTitle("Loading table data");
    tm.setProgress(0.0);
    tm.setStatusMessage("Loading table...");
    Workbook workbook = null;
    // Load Spreadsheet data for preview.
    if (fileType != null && (fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension()) || fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) && workbook == null) {
        try {
            workbook = WorkbookFactory.create(is);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Could not read Excel file.  Maybe the file is broken?");
        } finally {
            if (is != null)
                is.close();
        }
    }
    if (this.fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension()) || this.fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) {
        // Fixed bug# 1668, Only load data from the first sheet, ignore the rest sheets
        // UPDATE: From the user perspective it makes more sense to get the selected tab/sheet instead.
        String networkName = amp.getName();
        if (networkName == null)
            networkName = workbook.getSheetName(0);
        final Sheet sheet = workbook.getSheet(networkName);
        if (sheet != null) {
            reader = new ExcelAttributeSheetReader(sheet, amp, serviceRegistrar);
            loadAnnotation(tm);
        }
    } else {
        try {
            reader = new DefaultAttributeTableReader(null, amp, this.is, serviceRegistrar);
            loadAnnotation(tm);
        } catch (Exception ioe) {
            tm.showMessage(TaskMonitor.Level.ERROR, "Unable to read table: " + ioe.getMessage());
        }
    }
}
Also used : DefaultAttributeTableReader(org.cytoscape.tableimport.internal.reader.DefaultAttributeTableReader) ExcelAttributeSheetReader(org.cytoscape.tableimport.internal.reader.ExcelAttributeSheetReader) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException)

Example 82 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project my_curd by qinyou.

the class ExcelHelper method exportExcelFile.

/**
 * @param head
 * @param modelFile
 * @param outputFile
 * @param dataList
 */
public void exportExcelFile(ExcelHead head, File modelFile, File outputFile, List<?> dataList) {
    // 读取导出excel模板
    InputStream inp = null;
    Workbook wb = null;
    try {
        inp = new FileInputStream(modelFile);
        wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        // 生成导出数据
        buildExcelData(sheet, head, dataList);
        // 导出到文件中
        FileOutputStream fileOut = new FileOutputStream(outputFile);
        wb.write(fileOut);
        fileOut.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (InvalidFormatException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 83 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project Xlsx2Json by noahzark.

the class ExcelParserMain method parseExcelFile.

/**
 * Parse the excel file and save as json
 * @param targetName Excel file name without suffix
 * @param sheetList Target sheet list
 * @param showSheetName Whether show sheet name in result or not
 */
public static void parseExcelFile(String targetName, String[] sheetList, boolean showSheetName) {
    try {
        Workbook workbook = getWorkbook(targetName);
        String jsonText;
        if (showSheetName) {
            // Get the JSON text.
            JSONObject json = constructJsonObject(workbook, sheetList);
            jsonText = json.toString();
        } else {
            // Get the JSON text.
            JSONArray json = constructJsonArray(workbook, sheetList);
            jsonText = json.toString();
        }
        saveStringToFile(targetName, jsonText);
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 84 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project hutool by looly.

the class Word07Writer method addPicture.

/**
 * 增加图片,单独成段落,增加后图片流关闭
 *
 * @param in       图片流
 * @param picType  图片类型,见Document.PICTURE_TYPE_XXX
 * @param fileName 文件名
 * @param width    宽度
 * @param height   高度
 * @param align    图片的对齐方式
 * @return this
 * @since 5.2.4
 */
public Word07Writer addPicture(InputStream in, PicType picType, String fileName, int width, int height, ParagraphAlignment align) {
    final XWPFParagraph paragraph = doc.createParagraph();
    paragraph.setAlignment(align);
    final XWPFRun run = paragraph.createRun();
    try {
        run.addPicture(in, picType.getValue(), fileName, Units.toEMU(width), Units.toEMU(height));
    } catch (InvalidFormatException e) {
        throw new POIException(e);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(in);
    }
    return this;
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) POIException(cn.hutool.poi.exceptions.POIException)

Example 85 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project selenium_java by sergueik.

the class TestWithData method getSpreadSheet.

public Sheet getSpreadSheet() {
    File file = new File(System.getProperty("user.dir") + File.separator + "target\\classes\\Test.xlsx");
    FileInputStream inputStream = null;
    Workbook wb = null;
    try {
        inputStream = new FileInputStream(file);
        wb = WorkbookFactory.create(inputStream);
        System.out.println(wb.toString());
    } catch (IOException ex) {
        System.out.println("Error Message " + ex.getMessage());
    } catch (InvalidFormatException e) {
        System.out.println("Invalid File format!");
    }
    Sheet mySheet = wb.getSheet("MySheet");
    return mySheet;
}
Also used : IOException(java.io.IOException) File(java.io.File) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Sheet(org.apache.poi.ss.usermodel.Sheet) FileInputStream(java.io.FileInputStream) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)89 IOException (java.io.IOException)39 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)22 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)18 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)18 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 InputStream (java.io.InputStream)15 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 Workbook (org.apache.poi.ss.usermodel.Workbook)14 ArrayList (java.util.ArrayList)12 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)11 Sheet (org.apache.poi.ss.usermodel.Sheet)11 Test (org.junit.Test)10 URI (java.net.URI)9 FileInputStream (java.io.FileInputStream)8 POIXMLException (org.apache.poi.POIXMLException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 TikaException (org.apache.tika.exception.TikaException)7 XmlException (org.apache.xmlbeans.XmlException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6