我必须制作一个用户可以上传压缩文件(.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();
.
.
.
}
}
答案 0 :(得分: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中的数据