After exporting an Excel file using POI, the prompt to open the file prompts that the file cannot be opened because the file format or file extension is invalid.
question
当我们通过POI导出Excel文件后,发现文件出错打不开,如下所示
reason
这是因为我们代码中 这三个的应用应该和我们的office表格的版本有关系。
HSSFWorkBook:适用于Excel03版本的,文件扩展名只能为.xls;
XSSFWorkBook:适用于Excel07版本及以后,文件扩展名为.xlsx或者.xls这两个都行;
SXSSFWorkBook:XSSFWorkBook升级;
所以 ,我们可以通过查看我们电脑的版本来选择,当我们office版本为03时,我们就用HSSFWorkBook创建,
当我们的Office版本为07以后的话我们就用XSSFWorkBook来创建对象。
2003 (HSSF-related methods must be used when exporting (the suffix can only be .xls, if it is set to ".xlsx", it will prompt that the file is damaged and cannot be opened))
@Override
public Workbook exportExcel ( ) {
Workbook workbook = new HSSFWorkBook ( ) ;
Sheet sheet = workbook. createSheet ( "题库" ) ;
Row row = sheet. createRow ( 0 ) ;
row. createCell ( 0 ) . setCellValue ( "题目标题" ) ;
row. createCell ( 1 ) . setCellValue ( "题目解答" ) ;
row. createCell ( 2 ) . setCellValue ( "题目难度等级" ) ;
row. createCell ( 3 ) . setCellValue ( "排序" ) ;
row. createCell ( 4 ) . setCellValue ( "副标题" ) ;
row. createCell ( 5 ) . setCellValue ( "题目类型" ) ;
row. createCell ( 6 ) . setCellValue ( "是否显示" ) ;
List< QuestionEntity> list = this . list ( ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
Row row2 = sheet. createRow ( i + 1 ) ;
row2. createCell ( 0 ) . setCellValue ( list. get ( i) . getTitle ( ) ) ;
row2. createCell ( 1 ) . setCellValue ( list. get ( i) . getAnswer ( ) ) ;
row2. createCell ( 2 ) . setCellValue ( list. get ( i) . getLevel ( ) ) ;
row2. createCell ( 3 ) . setCellValue ( list. get ( i) . getDisplayOrder ( ) ) ;
row2. createCell ( 4 ) . setCellValue ( list. get ( i) . getSubTitle ( ) ) ;
row2. createCell ( 5 ) . setCellValue ( list. get ( i) . getType ( ) ) ;
row2. createCell ( 6 ) . setCellValue ( list. get ( i) . getEnable ( ) ) ;
}
return workbook;
}
@GetMapping ( "exportExcel" )
public void export ( String tableName, HttpServletResponse response) {
System. out. println ( "导出excele" ) ;
Workbook workbook = questionService. exportExcel ( ) ;
if ( workbook != null ) {
String fileName = "uxue_" + new SimpleDateFormat ( "yyyyMMddHHmmss" ) . format ( new Date ( ) ) + ".xlsx" ;
response. setHeader ( "Content-Disposition" , "attachment; filename=" + fileName ) ;
response. setContentType ( "application/binary;charset=ISO8859_1" ) ;
response. setCharacterEncoding ( "GB2312" ) ;
OutputStream outputStream;
try {
outputStream = response. getOutputStream ( ) ;
workbook. write ( outputStream) ;
outputStream. flush ( ) ;
outputStream. close ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
} else {
try {
response. getWriter ( ) . print ( "error" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
2007 (use XSSF-related methods when exporting (the suffix can be .xls or .xlsx))
@Override
public Workbook exportExcel ( ) {
Workbook workbook = new XSSFWorkBook ( ) ;
Sheet sheet = workbook. createSheet ( "题库" ) ;
Row row = sheet. createRow ( 0 ) ;
row. createCell ( 0 ) . setCellValue ( "题目标题" ) ;
row. createCell ( 1 ) . setCellValue ( "题目解答" ) ;
row. createCell ( 2 ) . setCellValue ( "题目难度等级" ) ;
row. createCell ( 3 ) . setCellValue ( "排序" ) ;
row. createCell ( 4 ) . setCellValue ( "副标题" ) ;
row. createCell ( 5 ) . setCellValue ( "题目类型" ) ;
row. createCell ( 6 ) . setCellValue ( "是否显示" ) ;
List< QuestionEntity> list = this . list ( ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
Row row2 = sheet. createRow ( i + 1 ) ;
row2. createCell ( 0 ) . setCellValue ( list. get ( i) . getTitle ( ) ) ;
row2. createCell ( 1 ) . setCellValue ( list. get ( i) . getAnswer ( ) ) ;
row2. createCell ( 2 ) . setCellValue ( list. get ( i) . getLevel ( ) ) ;
row2. createCell ( 3 ) . setCellValue ( list. get ( i) . getDisplayOrder ( ) ) ;
row2. createCell ( 4 ) . setCellValue ( list. get ( i) . getSubTitle ( ) ) ;
row2. createCell ( 5 ) . setCellValue ( list. get ( i) . getType ( ) ) ;
row2. createCell ( 6 ) . setCellValue ( list. get ( i) . getEnable ( ) ) ;
}
return workbook;
}
@GetMapping ( "exportExcel" )
public void export ( String tableName, HttpServletResponse response) {
System. out. println ( "导出excele" ) ;
Workbook workbook = questionService. exportExcel ( ) ;
if ( workbook != null ) {
String fileName = "uxue_" + new SimpleDateFormat ( "yyyyMMddHHmmss" ) . format ( new Date ( ) ) + ".xlsx" ;
response. setHeader ( "Content-Disposition" , "attachment; filename=" + fileName ) ;
response. setContentType ( "application/binary;charset=ISO8859_1" ) ;
response. setCharacterEncoding ( "GB2312" ) ;
OutputStream outputStream;
try {
outputStream = response. getOutputStream ( ) ;
workbook. write ( outputStream) ;
outputStream. flush ( ) ;
outputStream. close ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
} else {
try {
response. getWriter ( ) . print ( "error" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
In this way, our problem is successfully solved! ! ! !