POI 操作Excel 异常org.apache.poi.openxml4j.exceptions.invalidformatexception: package should contain a c
2014-09-12 18:29:49
POI 操作Excel 出现如下异常
org.apache.poi.openxml4j.exceptions.invalidformatexception: package should contain a content type part
public boolean parseToExcel(String oldFileName,String newFileName){
Map<Object,Object> map = new HashMap<Object,Object>();
map.put(0,"FYMC");
map.put(1,"GXFYS");
map.put(2,"LS_YSJGYS");
map.put(4,"LS_ZDHJAJS");
map.put(5,"LS_CGRKAJS");
map.put(6,"LS_RKSBAJS");
map.put(7,"LS_XSAJS");
map.put(8,"LS_YJAJS");
map.put(9,"LS_FYS");
map.put(10,"SS_XSAJS");
map.put(11,"SS_YJAJS");
map.put(12,"SS_FYS");
List<Map<String,String>> parseList = getDwgroupdtbgParse();
//读取模板路径
File filePath = new File(oldFileName);
try {
//读取Excel模板
Wrokbook dd = WorkbookFactory.create(new FileInputStream(filePath)); //此句抛出异常
// Workbook wb = createworkbook(new FileInputStream(filePath));
//读取模板内所有sheet内容
Sheet sheet = wb.getSheetAt(0);
for (int i = EXCEL_START_ROW; i < EXCEL_ROW_NUMS; i++) {
Row row = sheet.getRow(i);
Row row = sheet.createRow(i);
for (int j = 0; j < EXCEL_CELL_NUMS; j++) {
Cell cell = row.createCell(j+1);
if(j!=NO_CREATE_CELL){
cell.setCellValue(String.valueOf(parseList.get(i-EXCEL_START_ROW).get(map.get((Object)j))));
//修改模板内容导出新模板
FileOutputStream out = new FileOutputStream(newFileName);
wb.write(out);
out.close();
return true;
} catch (InvalidFormatException e) {
e.printStackTrace();
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
* @throws InvalidFormatException
public static Workbook createworkbook(InputStream inp) throws IOException,InvalidFormatException {
if (!inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
if (POIFSFileSystem.hasPOIFSHeader(inp)) {
return new HSSFWorkbook(inp);
if (POIXMLDocument.hasOOXMLHeader(inp)) {
return new XSSFWorkbook(OPCPackage.open(inp));
throw new IllegalArgumentException("你的excel版本目前poi解析不了");
org
.
apache
.
poi
JAR
包
,解决个人的
import
org
.
apache
.commons.beanutils.PropertyUtilsBean;
import
org
.
apache
.commons.lang.StringUtils;
import
org
.
apache
.
poi
.hssf.usermodel.HSSFCell;
import
org
.
apache
.
poi
.hssf.usermodel.HSSFCellStyle;
import
org
.
apache
.
poi
.hssf.usermodel.HSSFFont;
import
org
.
apache
.
poi
.hssf.usermodel.HSSFPalette;
import
org
.
apache
.
poi
.hssf.usermodel.HSSFRichTextString;
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
.hssf.util.HSSFColor;
import
org
.
apache
.
poi
.hssf.util.Region;
import
org
.
apache
.
poi
.
poi
fs.filesystem.
POI
FSFileSystem;
import
org
.
apache
.
poi
.ss.util.CellRangeAddress;
"The import
org
.
apache
.
poi
cannot be resolved"的问题
使用
POI
时的错误小记
rg.
apache
.
poi
.
openxml
4j
.
exception
s.
OpenXML
4J
Exception
: The part /docProps/core.
xml
fail to be saved in the stream with marshaller
org
.
apache
.
poi
.
openxml
4j
.opc.internal.marshallers.ZipPacka...
异常
信息:
Caused by:
org
.
apache
.
poi
.
openxml
4j
.
exception
s.
Invalid
Format
Exception
:
Package
should
contain
a cont
en
t type part [M1.13]
异常
原因:文件类型信息读取错误
我建的
Excel
文件是 xxx.xls,而新建的Workbook工作簿是XSSFWorkbook类型,两者不匹配...
问题描述:
org
.
apache
.
poi
.
openxml
4j
.
exception
s.OLE2NotOffice
Xml
File
Exception
: The supplied data appears to be in the OLE2
Format
. You are calling the part of
POI
that deals with OO
XML
(Office Op
en
XML
) Docum
en
ts. ...
org
.
apache
.
poi
.
openxml
4j
.
exception
s.
Invalid
Format
Exception
:
Package
should
contain
a cont
en
t type part [M1.13]
POI
的jar跑出
异常
:
1.当出现如下
异常
时
org
.
apache
.
poi
.
openxml
4j
.
exception
s.
Invalid
Format
Exception
:
Package
should
contain
a cont
en
t type part [M1.13]
这是因为使用的word模版的名称有问题, word模版名称可能
包
含
中
划线. 例如: xxx-合同.docx,
poi
无法识别
中
划线. 若必须要加以区分某某文件可以使用下划线,像: xxx_合同书.docx.
2.当使用
poi
在有${name}这种形式变量的
问题描述:
org
.
apache
.
poi
.
openxml
4j
.
exception
s.OLE2NotOffice
Xml
File
Exception
: The supplied data appears to be in the OLE2
Format
. You are calling the part of
POI
that deals with OO
XML
(Office Op
en
XML
) Docum
en
ts. You need to call a differ
en
t part of
POI
to pro
Exception
in thread "main"
org
.
apache
.
poi
.
openxml
4j
.
exception
s.
Invalid
Operation
Exception
: Can't op
en
the specified file: 'D:/template.docx'
at
org
.
apache
.
poi
.
openxml
4j
.opc.Zip
Package
.<init>(Zip
Package
.java:108)
at
org
.
apache
.
poi
.
openxml
4j
.opc.OPCP.
org
.
apache
.
poi
.
POI
XML
Exception
:
org
.
apache
.
poi
.
openxml
4j
.
exception
s.
Invalid
Format
Exception
:
Package
should
contain
a cont
en
t type part [M1.13]
org
.
apache
.
poi
.
openxml
4j
.
exception
s.OLE2NotOffice
Xml
File
Exception
: The supplied data appears to be in the OLE2
Format
.
You are calling the part of
POI
that deals with OO
XML
(Office Op
en
XML
) D.
这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居
中
、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入
欢迎使用Ma...
原因: 主要是因为xls和xlsx的解析方式不一样, 导致使用的类不同. xls返回的是HSSFWorkbook , 而xlsx返回的是XSSFWorkbook. 解决方法:1. 新建个方法, 返回HSSFWorkbook和XSSFWorkbook所实现的接口XlsImpUtilpublic static Workbook create(InputStream inp) throws IOEx...