The supplied data appears to be in the Office 2007+ XML

1、错误描述

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
	at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:152)
	at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
	at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:302)
	at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:87)
	at com.you.print.ExcelUtils.replaceModel(ExcelUtils.java:20)
	at com.you.print.TestReplaceExcel.main(TestReplaceExcel.java:28)

2、错误原因

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
	at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:152)
	at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
	at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:302)
	at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:87)
	at com.you.print.ExcelUtils.replaceModel(ExcelUtils.java:20)
	at com.you.print.TestReplaceExcel.main(TestReplaceExcel.java:28)



/**
 * 
 */
package com.you.print;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 *
 */
public class TestReplaceExcel 
{
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		List<ReplaceExcelData> list = new ArrayList<ReplaceExcelData>();
		ReplaceExcelData red = new ReplaceExcelData();
		red.setColumn(2);
		red.setRow(6);
		red.setKey("1");
		red.setValue("111111");
		list.add(red);
		
		ExcelUtils.replaceModel(list, "D:\\myeclipseworkspces\\Print\\model\\WeightBill.xlsx", "D:\\myeclipseworkspces\\Print\\model\\Bill.xlsx");
	}

}


package com.you.print;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelUtils 
{
	public static boolean replaceModel(List<ReplaceExcelData> datas, String sourceFilePath, String targetFilePath) 
	{  
        boolean bool = true;  
        try 
        {  
            POIFSFileSystem fs  =new POIFSFileSystem(new FileInputStream(sourceFilePath));     
            HSSFWorkbook wb = new HSSFWorkbook(fs);  
            HSSFSheet sheet = wb.getSheetAt(0);  
              
            for (ReplaceExcelData data : datas) {  
                //获取单元格内容  
                HSSFRow row = sheet.getRow(data.getRow());     
                HSSFCell cell = row.getCell((short)data.getColumn());  
                String str = cell.getStringCellValue();  
                  
                //替换单元格内容  
                str = str.replace(data.getKey(), data.getValue());  
                  
                //写入单元格内容  
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
                cell.setCellValue(str);     
            }  
  
            // 输出文件     
            FileOutputStream fileOut = new FileOutputStream(targetFilePath);     
            wb.write(fileOut);     
            fileOut.close();     
  
        } 
        catch (Exception e) 
        {  
            bool = false;  
            e.printStackTrace();  
        }  
        return bool;  
    }  
}
关键问题的原因还是excel2003和excel2007版本的问题

3、解决办法

(1)判断文件后缀名是xls,还是xlsx

(2)如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页