poi导出xlsx文件后,打开报“因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。”的解决方法

Posted 秋9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poi导出xlsx文件后,打开报“因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。”的解决方法相关的知识,希望对你有一定的参考价值。

【现象】

代码如下:

public static void main(String[] args) throws IOException {
   Date now=new Date();
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
   String localname=dateFormat.format(now);
   String xlsxPath = "d:\\\\"+ localname + ".xlsx";
   HSSFWorkbook workBook = new HSSFWorkbook();
   OutputStream fos = null;
   try {//增加一个Sheet的对象
      HSSFSheet sheet = workBook.createSheet("sheet1");
      HSSFRow row = sheet.createRow(0);
      row.createCell(0).setCellValue("aaaaaaaa");
      fos = new FileOutputStream(xlsxPath);
   } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
   workBook.write(fos);
   fos.close();
}

【解决方法】

 

public static void main(String[] args) throws IOException {
   Date now=new Date();
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
   String localname=dateFormat.format(now);
   String xlsxPath = "d:\\\\"+ localname + ".xlsx";
   XSSFWorkbook workBook = new XSSFWorkbook();
   OutputStream fos = null;
   try {//增加一个Sheet的对象
      XSSFSheet sheet = workBook.createSheet("sheet1");
      XSSFRow row = sheet.createRow(0);
      row.createCell(0).setCellValue("aaaaaaaa");
      fos = new FileOutputStream(xlsxPath);
   } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
   workBook.write(fos);
   fos.close();
}

以上是关于poi导出xlsx文件后,打开报“因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。”的解决方法的主要内容,如果未能解决你的问题,请参考以下文章