简体   繁体   English

在不保存的情况下阅读压缩的(.zip和.7z)Excel文件的表格

[英]Read the sheets of a zipped (.zip and .7z) excel file without saving it

I have to make a program in which the user can upload a zipped file(.7z or .zip). 我必须制作一个程序,用户可以在其中上传一个压缩文件(.7z或.zip)。 Now without a need to save this file I have to read its contents and check its extension. 现在无需保存该文件,我必须阅读其内容并检查其扩展名。 Till this point I have reached. 至此,我已经达到了。 Now if the file is a .xls file I want to pass this file to a 'HSSFWorkbook' object and check the contents of a particular sheet. 现在,如果文件是.xls文件,我想将此文件传递给“ HSSFWorkbook”对象,并检查特定工作表的内容。 this is the code which I have written: 这是我编写的代码:

private void getNumberOfItemsInArchive(FormFile uploadedFile) throws Exception 
{
    File archiveFilename = new File(uploadedFile.getFileName());  
    OutputStream os = new FileOutputStream(archiveFilename);  
    InputStream is = new BufferedInputStream(uploadedFile.getInputStream());  
    int count;  
    byte buf[] = new byte[4096];  
    while ((count = is.read(buf)) > -1) 
    {  
        os.write(buf, 0, count);    
    }

    RandomAccessFile randomAccessFile = null;
    ISevenZipInArchive inArchive = null;
    try 
    {
        randomAccessFile = new RandomAccessFile(uploadedFile.getFileName(), "r");
        logger.info("After generating randomAccessFile"+randomAccessFile);
        inArchive = SevenZip.openInArchive(null, // autodetect archive type
                    new RandomAccessFileInStream(randomAccessFile));
        ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
        logger.info("Count of items in archive: "+inArchive.getNumberOfItems());
        logger.info("-----------------------------");
        for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems())
        {
            String filename = item.getPath();
            logger.info("Filename::"+filename);
            String extension = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
            String excel = "xls";
            if (extension.matches(excel) && inArchive.getNumberOfItems() == 1) 
            {
                logger.info("Valid file");
                // what should be written here?
                read(??,"63","247");
            }
            else 
            {
                logger.info("Invalid File");
            }
        }

            .
        .
        .
        .
    }

public  boolean read(FileInputStream inputFile,String appid,String templateid) throws IOException  
{
    logger.info("Inside Excel reader class"+ inputFile);         
        .
        .
        .

    if(versionFlag.equals("true"))
    {   
        try
        {
            // code needed for here

            HSSFWorkbook workBook = new HSSFWorkbook(inputFile);
            logger.info("the file  path in work book is"+workBook);
            int numberOfSheets=workBook.getNumberOfSheets();
            logger.info("the number of sheets are"+numberOfSheets);
            HSSFSheet newSheet=workBook.getSheet("VersionDetails");
            if(newSheet==null)
            {
                result=false;
            }
            logger.info("the file  path in newsheet is"+newSheet);
            //int y=newSheet.getPhysicalNumberOfRows();
            HSSFRow row=newSheet.getRow(17);
            int numberofcell=row.getPhysicalNumberOfCells();

                .
                .
                .
    }
}

if you are talking about reading xls file, this can help : 如果您正在谈论读取xls文件,则可以帮助您:

try {

FileInputStream file = new FileInputStream(new File("C:\\test.xls"));

//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);

//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);

//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
    Row row = rowIterator.next();

    //For each row, iterate through each columns
    Iterator<Cell> cellIterator = row.cellIterator();
    while(cellIterator.hasNext()) {

        Cell cell = cellIterator.next();

        switch(cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                System.out.print(cell.getBooleanCellValue() + "\t\t");
                break;
            case Cell.CELL_TYPE_NUMERIC:
                System.out.print(cell.getNumericCellValue() + "\t\t");
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.print(cell.getStringCellValue() + "\t\t");
                break;
        }
    }
    System.out.println("");
}
file.close();
FileOutputStream out =
    new FileOutputStream(new File("C:\\test.xls"));
workbook.write(out);
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace(); 
} catch (IOException e) {
e.printStackTrace();
} 

and you can validate the data within the xls in the switch case mentioned 您可以在上述开关案例中验证xls中的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM