POI導出Excel報錯“擴展名與文件的格式不匹配”


下面是我用POI導出Excel的實例:

依賴的jar包

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>

工具類

public class ExportExcel {
// 顯示的導出表的標題
private String title;
// 導出表的列名
private String[] rowName;
private List<Object[]> dataList = new ArrayList<Object[]>();
HttpServletResponse response;

// 構造方法,傳入要導出的數據
public ExportExcel(String title, String[] rowName, List<Object[]> dataList) {
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}
/*
* 導出數據
*/
public void export(OutputStream out) throws Exception {
try {
HSSFWorkbook workbook = new HSSFWorkbook(); // 創建工作簿對象
HSSFSheet sheet = workbook.createSheet(title); // 創建工作表
// 產生表格標題行
HSSFRow rowm = sheet.createRow(0);
// 標題行高
rowm.setHeight((short) 480);
HSSFCell cellTiltle = rowm.createCell(0);
// sheet樣式定義【getColumnTopStyle()/getStyle()均為自定義方法 - 在下面 - 可擴展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);// 獲取列頭樣式對象
HSSFDataFormat df = workbook.createDataFormat();
HSSFCellStyle style = this.getStyle(workbook); // 單元格樣式對象
style.setDataFormat(df.getFormat("@"));
style.setWrapText(false);

sheet.addMergedRegion(new CellRangeAddress(0, 1, 0,
(rowName.length - 1)));
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(title);

// 定義所需列數
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置創建行(最頂端的行開始的第二行)
// 表頭行高
rowRowName.setHeight((short) 500);

// 將列頭設置到sheet的單元格中
for (int n = 0; n < columnNum; n++) {
HSSFCell cellRowName = rowRowName.createCell(n); // 創建列頭對應個數的單元格
cellRowName.setCellType(CellType.STRING); // 設置列頭單元格的數據類型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); // 設置列頭單元格的值
cellRowName.setCellStyle(columnTopStyle); // 設置列頭單元格樣式
}
// 將查詢出的數據設置到sheet對應的單元格中
for (int i = 0; i < dataList.size(); i++) {
Object[] obj = dataList.get(i);// 遍歷每個對象
HSSFRow row = sheet.createRow(i + 3);// 創建所需的行數
// 列數據行高
row.setHeight((short) 480);

for (int j = 0; j < obj.length; j++) {
HSSFCell cell = null; // 設置單元格的數據類型
if (j == 0) {
cell = row.createCell(j, CellType.NUMERIC);
cell.setCellValue(i + 1);
} else {
cell = row.createCell(j, CellType.STRING);
if (obj[j] != null) {
cell.setCellValue(obj[j].toString()); // 設置單元格的值
} else {
cell.setCellValue("");
}
}
cell.setCellStyle(style); // 設置單元格樣式
}
}
// 讓列寬隨着導出的列長自動適應
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 85;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
// 當前行未被使用過
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == CellType.STRING) {
if (currentCell.getRichStringCellValue() != null) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
}
if (colNum == 0) {
sheet.setColumnWidth(colNum, (columnWidth - 2) * 110);
} else {
sheet.setColumnWidth(colNum, (columnWidth + 4) * 195);
}
}
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
    /*
* 列頭單元格樣式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 設置字體
HSSFFont font = workbook.createFont();
// 設置字體大小
font.setFontHeightInPoints((short) 11);
// 字體加粗
font.setBold(true);
// 設置字體名字
font.setFontName("微軟雅黑");
// 設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
// 設置底邊框;
style.setBorderBottom(BorderStyle.THIN);
// 設置底邊框顏色;
style.setBottomBorderColor((short) 16);
// 設置左邊框;
style.setBorderLeft(BorderStyle.THIN);
// 設置左邊框顏色;
style.setBottomBorderColor((short) 16);
// 設置右邊框;
style.setBorderRight(BorderStyle.THIN);
// 設置右邊框顏色;
style.setBottomBorderColor((short) 16);
// 設置頂邊框;
style.setBorderTop(BorderStyle.THIN);
// 設置頂邊框顏色;
style.setBottomBorderColor((short) 16);
// 在樣式用應用設置的字體;
style.setFont(font);
// 設置自動換行;
style.setWrapText(false);
// 設置水平對齊的樣式為居中對齊;
style.setAlignment(HorizontalAlignment.CENTER);
// 設置垂直對齊的樣式為居中對齊;
style.setVerticalAlignment(VerticalAlignment.CENTER);

return style;
}
}

控制層

// 創建ExportExcel對象
ExportExcel ex = new ExportExcel(title, rowsName, dataList);
OutputStream out = null;
try {
out = response.getOutputStream();
response.setContentType("application/msexcel");
response.setHeader("Content-disposition",
"attachment; filename=" + new String("order1".getBytes("gbk"), "iso8859-1") + ".xls");
response.setCharacterEncoding("utf-8");
ex.export(out);
out.close();
} catch (Exception e) {
logger.error("Excel導出異常!", e);
}


以上是我在開發過程中的實例,剛開始我導出的Excel格式后綴名是用“xlsx”,后面測試人員本地電腦用了電腦自帶的office工具,無法打開,我在開發電腦上用的是WPS編輯都正常所以沒發現這個問題,
剛開始想到的是POI包創建工作簿對Excel版本的兼容性問題,所以查看了測試環境,用的是Excel2003、Excel2007。

查看代碼:
HSSFWorkbook workbook = new HSSFWorkbook(); 
查找資料說HSSFWorkbook創建工作簿兼容Excel2003及以上的版本,所以就排除版本問題。
后面查看了“xlsx”和“xls”的區別:
xls 是一個特有的二進制格式,其核心結構是復合文檔類型的結構,而 xlsx 的核心結構是 XML 類型的結構,采用的是基於 XML 的壓縮方式,使其占用的空間更小。
xls是excel2003及以前版本生成的文件格式,xlsx是excel2007及以后版本生成的文件格式(excel 2007之后版本可以打開xls格式的文件)。
最后就把導出的Excel后綴名換成了“xls”,完美解決了該問題,perfect~

 

 

 

 

 
        


 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM