【发布时间】:2018-01-09 22:04:41
【问题描述】:
我正在从一张特殊的 Excel 工作簿中读取一些值。我正在将这些值添加到我的数据库中。这个没有问题
但我还想将工作簿的工作表存储到我的数据库 BLOB 单元格中。
我正在尝试将工作表转换为字节 []。我使用下面的代码。
我正在尝试将工作表转换为 java.io.File。但我得到以下异常
线程“AWT-EventQueue-0”java.lang.ClassCastException 中的异常: org.apache.poi.xssf.usermodel.XSSFSheet 无法转换为 java.io.File
public byte[] convertPDFToByteArray() {
InputStream inputStream = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
File file = (File) oku.wb.getSheet("İN");
try {
inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return baos.toByteArray();
}
有没有办法将 "wb.getSheet("İN");" 转换为 java.io.File 或任何更好的方法?
【问题讨论】:
-
不,没有办法将 XSSFSheet 转换为文件。您可以使用 XSSFWorkbook.write() 方法将整个 xlsx 文档保存到文件中
标签: java database apache-poi inputstream