The supplied data appears to be in the Office 2007+ XML
2017-04-16 18:19
1401 查看
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
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
相关文章推荐
- The supplied data appears to be in the Office 2007+ XML
- org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML.
- poi 3.8 使用xssf 仍然报错:The supplied data appears to be in the Office 2007+ XML. Yo
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- 断电或强制退出后VMware虚拟机出现The virtual machine appears to be in use无法启动问题解决
- 断电或强制退出后VMware虚拟机出现The virtual machine appears to be in u...
- namenode启动报错:There appears to be a gap in the edit log. We expected txid 1, but got txid 16
- 【转】断电或强制退出后VMware虚拟机出现The virtual machine appears to be in use无法启动问题解决
- hadoop错误FATAL org.apache.hadoop.hdfs.server.namenode.NameNode Exception in namenode join java.io.IOException There appears to be a gap in the edit log
- how to do with the special characters in the xml data
- Embedding Documents in Word 2007 by Using the Open XML SDK 2.0 for Microsoft Office
- 断电或强制退出后VMware虚拟机出现The virtual machine appears to be in use无法启动问题解决
- 【Visual Studio】The project appears to be under source control, but the associated source control plug-in is not installed on this computer
- hadoop2.0报错“There appears to be a gap in the edit log”
- The virtual machine appears to be in use (VMWare)
- Problem 17 - How many letters would be needed to write all the numbers in words from 1 to 1000?
- Could not store transport type data for Receive Location 'Recv.Loc' to config store. Primary SSO Server 'Sql-server' failed. The external credentials in the SSO database are more recent.
- A person just needs three things to be truly happy in the world ....
- vs2005下出现“Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive”的解决方法
- Cannot open virtual machine:This virtual machine appears to be in use