这是读取Excel文件的代码,作为完整的示例代码检查. xls和. xlsx (密码是否受保护或密码是否受保护。
privateworkbookcreateworkbookbycheckextension (throwsioexception,InvalidFormatException { )。
工作簿工作簿=null;
if (fileextensionname.equals ('.xls ' ) ) }
try {
fileinputstreamfileinputstream=new file inputstream (new file (文件路径) );
workbook=newhssfworkbook (文件输入流;
} catch (加密文档元素封装) {
//checking of.xlsfilewithpasswordprotected。
fileinputstreamfileinputstream=new file inputstream (new file (文件路径) );
biff 8加密密钥. setcurrentuserpassword (' password );
workbook=newhssfworkbook (文件输入流;
}
} else if (fileextensionname.equals ('.xlsx ' ) ) )。
//checking of.xlsxfilewithpasswordprotected。
is=新文件输入(新文件)文件路径);
if (! is.markSupported () }
is=newpushbackInputstream(is,8 );
}
if (poifs文件系统. haspoifsheader (is ) ) }
poifsfilesystemfs=newpoifsfilesystem (is;
decryptord=decryptor.getinstance (info;
try {
d .验证密码(' password );
is=d.getdatastream(fs;
workbook=newxssfworkbook (OPC包. open (is );
} catch (通用安全类封装) )
e .打印堆栈跟踪(;
}
}
if(isworkbooklock!='true ' ) {
fileinputstreamfileinputstream=new file inputstream (new file (文件路径) );
工作簿=newxssfworkbook (文件输入流);
}
}
返回工作簿;
}