.hi,我正在尝试创建一个临时的.xlsx文件并使用apache poi写入它。我得到EmptyFileException创建工作簿。这是代码:使用XLSX的Apache临时文件Apache poi
public class Writer{
File file;
public File Write(List<FormData> l, Class elementClass)
{
try {//create temp fiele here
file = File.createTempFile(elementClass.getSimpleName(),".xlsx");
} catch (IOException ex) {
Logger.getLogger(Writer.class.getName()).log(Level.SEVERE, null, ex);
}
XSSFWorkbook workbook;
XSSFSheet sheet;
if (file.exists()) {
FileInputStream inputStream;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException ex) {
throw new AspirinException(AspirinException.Type.INTERNAL_ERROR);
}
try {// this line gets error//
workbook = (XSSFWorkbook) WorkbookFactory.create(inputStream);
} catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
throw new AspirinException(AspirinException.Type.INTERNAL_ERROR);
}
//...
它工作正常,如果我使用一个真实的文件。但对于临时文件它不起作用。请帮我解决一下这个。谢谢
将其写入一个新文件哪个错误和哪个/哪一行? – Marged
你认为你的'文件'不是空文件吗? 'WorkbookFactory.create(inputStream)'尝试创建一个'Workbook'对象,其中包含从'inputStream'中读取的数据。如果没有要读取的数据,则失败。 –
WorkbookFactory.create()用于存在/填充的Excel文件。从头开始创建一个XSSFWorkbook,然后将其写入()到临时文件 – kiwiwings