java以base64⽂件格式导出excel表格
在项⽬中要求查询数据库并且⽤base64⽂件流的格式返回excel表格,⾃⼰试了好⼏种⽅法,最后到的答案。
错误⽅式:⽤HSSFWorkbook直接⽣成相对应的⽂件,然后⽤base64转化,这种解析出来的⽂件是打不开的
String encode="data:application/vnd.ms-excel;base64,";
HSSFWorkbook wb = port(fileName, files, activityStatisticsVos);
byte[] bytes = wb.getBytes();
Base64.Encoder baseEncoder = Encoder();
encode+= deToString(bytes);
正确⽅式:
先创建⽂件,然后⽤FileOutputStream将HSSFWorkbook写⼊file⽂件中,然后将⽂件读出来,并且⽤
base64进⾏加密成base64字符串。
⽣成⽂件:
File file=new File("test.xlsx");
HSSFWorkbook wb = port(fileName, files, activityStatisticsVos);
薇诺娜 赖德
FileOutputStream fileOutputStream=new FileOutputStream(file);
wb.write(fileOutputStream);
⽂件⽣成base64字符串
BASE64Encoder encoder = new BASE64Encoder();
FileInputStream fin = null;
爱字组词
BufferedInputStream bin = null;
ByteArrayOutputStream baos = null;
BufferedOutputStream bout = null;
try {
fin = new FileInputStream(document);
bin = new BufferedInputStream(fin);
baos = new ByteArrayOutputStream();
bout = new BufferedOutputStream(baos);
张冬玲阿宝
byte[] buffer = new byte[1024];
身材最好的女明星int len = ad(buffer);
while (len != -1) {
bout.write(buffer, 0, len);
len = ad(buffer);
}
//刷新此输出流并强制写出所有缓冲的输出字节
bout.flush();
byte[] bytes = ByteArray();
deBuffer(bytes).trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fin.close();
bin.close();
杜十五bout.close();
} catch (IOException e) {
e.printStackTrace();
王菲谢霆锋为什么分手}
}
return null;