POI无法读取 空poi 读取单元格样式吗

poi操作excel文件时遇到某个单元格为空时如何处理?_百度知道> POI通过事件解析Excel,空单元格有关问题
POI通过事件解析Excel,空单元格有关问题
kianlee & &
发布时间: & &
浏览:111 & &
回复:0 & &
悬赏:0.0希赛币
POI通过事件解析Excel,空单元格问题我今天发现代码里面有个这样的问题,就是POI事件解析Excel的时候,空单元格会被忽略,请问下知道的筒子,这种情况怎么处理,注意是事件读取,附代码  Java code  package com.its.fedex.msg.
import java.io.IOE import java.io.InputS import java.util.ArrayL import java.util.I import java.util.L
import org.apache.poi.openxml4j.exceptions.OpenXML4JE import org.apache.poi.openxml4j.opc.OPCP import org.apache.poi.ss.usermodel.BuiltinF import org.apache.poi.ss.usermodel.DataF import org.apache.poi.xssf.eventusermodel.XSSFR import org.apache.poi.xssf.model.SharedStringsT import org.apache.poi.xssf.model.StylesT import org.apache.poi.xssf.usermodel.XSSFCellS import org.apache.poi.xssf.usermodel.XSSFRichTextS import org.xml.sax.A import org.xml.sax.InputS import org.xml.sax.SAXE import org.xml.sax.XMLR import org.xml.sax.helpers.DefaultH import org.xml.sax.helpers.XMLReaderF
import com.its.fedex.msg.exception.ExcelParseE import com.its.fedex.msg.service.IRowH
public class Excel2007Reader extends DefaultHandler {
* 共享字符串表
private SharedStringsT
* 上一次的内容
private String lastC
* 字符串标识
private boolean nextIsS
* 工作表索引
private int sheetIndex = -1;
private List&String& rowlist = new ArrayList&String&();
private int curRow = 0;
private int curCol = 0;
* T元素标识
private boolean isTE
* Excel数据逻辑处理
private IRowHandler rowH
* 异常信息,如果为空则表示没有异常
private String exceptionM
* 单元格数据类型,默认为字符串类型
private CellDataType nextDataType = CellDataType.SSTINDEX;
private final DataFormatter formatter = new DataFormatter();
private short formatI
private String formatS
private StylesTable stylesT
public void setRowReader(IRowHandler rowHandler)
this.rowHandler = rowH
* 遍历工作簿中所有的电子表格
* @param filename
* @throws IOException
* @throws OpenXML4JException
* @throws SAXException
* @throws Exception
public void process(String filename) throws IOException, OpenXML4JException, SAXException
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader xssfReader = new XSSFReader(pkg);
stylesTable = xssfReader.getStylesTable();
SharedStringsTable sst = xssfReader.getSharedStringsTable();
XMLReader parser = this.fetchSheetParser(sst);
Iterator&InputStream& sheets = xssfReader.getSheetsData();
while (sheets.hasNext())
curRow = 0;
sheetIndex++;
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException
XMLReader parser = XMLReaderFactory.createXMLReader(&org.apache.xerces.parsers.SAXParser&);
this.sst =
parser.setContentHandler(this);
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
// c =& 单元格
if (&c&.equals(name))
// 设定单元格类型
this.setNextDataType(attributes);
// 当元素为t时
if (&t&.equals(name))
isTElement =
isTElement =
lastContents = &&;
* 单元格中的数据可能的数据类型
enum CellDataType
BOOL, ERROR, FORMULA, INLINESTR, SSTINDEX, NUMBER, DATE, NULL
* 处理数据类型
* @param attributes
public void setNextDataType(Attributes attributes)
nextDataType = CellDataType.NUMBER;
formatIndex = -1;
formatString =
String cellType = attributes.getValue(&t&);
String cellStyleStr = attributes.getValue(&s&);
if (&b&.equals(cellType))
nextDataType = CellDataType.BOOL;
else if (&e&.equals(cellType))
nextDataType = CellDataType.ERROR;
else if (&inlineStr&.equals(cellType))
nextDataType = CellDataType.INLINESTR;
else if (&s&.equals(cellType))
nextDataType = CellDataType.SSTINDEX;
else if (&str&.equals(cellType))
nextDataType = CellDataType.FORMULA;
if (cellStyleStr != null)
int styleIndex = Integer.parseInt(cellStyleStr);
XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
formatIndex = style.getDataFormat();
formatString = style.getDataFormatString();
if (&m/d/yy& == formatString)
nextDataType = CellDataType.DATE;
formatString = &yyyy-MM-dd hh:mm:ss.SSS&;
if (formatString == null)
nextDataType = CellDataType.NULL;
formatString = BuiltinFormats.getBuiltinFormat(formatIndex);
* 对解析出来的数据进行类型处理
* @param value
单元格的值(这时候是一串数字)
* @param thisStr
一个空字符串
@SuppressWarnings(&deprecation&)
public String getDataValue(String value, String thisStr)
switch (nextDataType)
// 这几个的顺序不能随便交换,交换了很可能会导致数据错误
case BOOL:
char first = value.charAt(0);
thisStr = first == '0'
&FALSE& : &TRUE&;
case ERROR:
thisStr = &\&ERROR:& + value.toString() + '&';
case FORMULA:
thisStr = '&' + value.toString() + '&';
case INLINESTR:
XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
thisStr = rtsi.toString();
case SSTINDEX:
String sstIndex = value.toString();
int idx = Integer.parseInt(sstIndex);
XSSFRichTextString rtss = new XSSFRichTextString(sst.getEntryAt(idx));
thisStr = rtss.toString();
catch (NumberFormatException ex)
thisStr = value.toString();
case NUMBER:
if (formatString != null)
thisStr = formatter.formatRawCellContents(Double.parseDouble(value), formatIndex, formatString)
thisStr = thisStr.replace(&_&, &&).trim();
case DATE:
thisStr = formatter.formatRawCellContents(Double.parseDouble(value), formatIndex, formatString);
// 对日期字符串作特殊处理
thisStr = thisStr.replace(& &, &T&);
thisStr = & &;
return thisS
public void endElement(String uri, String localName, String name) throws SAXException
// 根据SST的索引值的到单元格的真正要存储的字符串
// 这时characters()方法可能会被调用多次
if (nextIsString)
int idx = Integer.parseInt(lastContents);
lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
// t元素也包含字符串
if (isTElement)
// 将单元格内容加入rowlist中,在这之前先去掉字符串前后的空白符
String value = lastContents.trim();
rowlist.add(curCol, value);
isTElement =
else if (&v&.equals(name))
// v =& 单元格的值,如果单元格是字符串则v标签的值为该字符串在SST中的索引
String value = this.getDataValue(lastContents.trim(), &&);
rowlist.add(curCol, value);
// 如果标签名称为 row ,这说明已到行尾,调用 optRows() 方法
if (name.equals(&row&))
rowHandler.getRows(curRow, rowlist);
catch (ExcelParseException e)
exceptionMessage = e.getMessage();
rowlist.clear();
curCol = 0;
public void characters(char[] ch, int start, int length) throws SAXException
// 得到单元格内容的值
lastContents += new String(ch, start, length);
* @return the exceptionMessage
public String getExceptionMessage()
return exceptionM
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&poi事件解析excel 不是 无法解析空单元格 急啊 求助_百度知道java通过POI读取excel2003时,单元格类型取得错误。_百度知道

我要回帖

更多关于 poi合并单元格 居中 的文章

 

随机推荐