XSSFSheet 无法转换为 java.io.File
XSSFSheet cannot be cast to java.io.File
我正在从 excel 工作簿的特定 sheet 中读取一些值。我正在将这些值添加到我的数据库中。这个
没有任何问题
但我还想将工作簿的 sheet 存储到我的数据库 BLOB 单元格中。
我正在尝试将 sheet 转换为 Byte[]。我使用下面的代码。
我正在尝试将 sheet 转换为 java.io.File。但我收到以下异常
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
org.apache.poi.xssf.usermodel.XSSFSheet cannot be cast to 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 或任何更好的方法?
您不能只在两个不相关的 类 之间进行转换。
在你的情况下,我认为你应该使用 sheet 工作簿的 getBytes()
,因为 sheet 对象没有这个:
byte[] buffer = sheet.getWorkbook().getBytes();
看起来您的 oku.wb
已经包含工作簿,在这种情况下您可以直接在 oku.wb
上调用 getBytes()
:
byte[] buffer = oku.wb.getBytes();
EDIT Blast,我从我自己的一个项目中挑选了这段代码,但它使用了 HSSFSheet
而不是 XSSFSheet
。因此,最好的办法是使用评论中概述的工作簿的 write()
方法:
public byte[] convert(XSSFSheet sheet) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
sheet.getWorkbook().write(baos);
} catch (IOException e) {
// blah
}
return baos.toByteArray();
}
我正在从 excel 工作簿的特定 sheet 中读取一些值。我正在将这些值添加到我的数据库中。这个
没有任何问题但我还想将工作簿的 sheet 存储到我的数据库 BLOB 单元格中。
我正在尝试将 sheet 转换为 Byte[]。我使用下面的代码。
我正在尝试将 sheet 转换为 java.io.File。但我收到以下异常
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.apache.poi.xssf.usermodel.XSSFSheet cannot be cast to 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 或任何更好的方法?
您不能只在两个不相关的 类 之间进行转换。
在你的情况下,我认为你应该使用 sheet 工作簿的 getBytes()
,因为 sheet 对象没有这个:
byte[] buffer = sheet.getWorkbook().getBytes();
看起来您的 oku.wb
已经包含工作簿,在这种情况下您可以直接在 oku.wb
上调用 getBytes()
:
byte[] buffer = oku.wb.getBytes();
EDIT Blast,我从我自己的一个项目中挑选了这段代码,但它使用了 HSSFSheet
而不是 XSSFSheet
。因此,最好的办法是使用评论中概述的工作簿的 write()
方法:
public byte[] convert(XSSFSheet sheet) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
sheet.getWorkbook().write(baos);
} catch (IOException e) {
// blah
}
return baos.toByteArray();
}