我必须制作一个程序,用户可以在其中上传压缩文件(.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();
.
.
.
}
}