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

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值