0

我必须制作一个程序,用户可以在其中上传压缩文件(.7z 或 .zip)。现在不需要保存这个文件,我必须阅读它的内容并检查它的扩展名。到目前为止,我已经达到了。现在,如果文件是 .xls 文件,我想将此文件传递给“HSSFWorkbook”对象并检查特定工作表的内容。这是我写的代码:

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();

                .
                .
                .
    }
}
4

1 回答 1

0

如果您正在谈论阅读 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();
} 

并且您可以在提到的开关案例中验证 xls 中的数据

于 2013-10-09T08:22:15.273 回答