This commit is contained in:
Looly 2020-12-09 00:08:12 +08:00
parent 68da52bf82
commit 30e1eb929c
4 changed files with 49 additions and 6 deletions

View File

@ -23,6 +23,8 @@
* 【core 】 修复TemporalAccessorUtil无法格式化LocalDate带时间问题issue#1289@Github
* 【json 】 修复自定义日期格式的LocalDateTime没有包装引号问题issue#1289@Github
* 【cache 】 get中unlock改为unlockReadissue#1294@Github
* 【db 】 修复表名包含点导致的问题issue#1300@Github
* 【poi 】 修复xdr:row标签导致的问题issue#1297@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -1,5 +1,6 @@
package cn.hutool.db.sql;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.util.ArrayUtil;
@ -98,7 +99,7 @@ public class Wrapper {
//对于Oracle这类数据库表名中包含用户名需要单独拆分包装
if(field.contains(StrUtil.DOT)){
final Collection<String> target = CollectionUtil.filter(StrUtil.split(field, StrUtil.C_DOT), (Editor<String>) t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote));
final Collection<String> target = CollUtil.filter(StrUtil.split(field, StrUtil.C_DOT, 2), (Editor<String>) t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote));
return CollectionUtil.join(target, StrUtil.DOT);
}

View File

@ -66,6 +66,8 @@ public class Excel07SaxReader extends DefaultHandler implements ExcelSaxReader<E
private XSSFCellStyle xssfCellStyle;
// 单元格存储的格式化字符串nmtFmt的formatCode属性的值
private String numFmtString;
// 是否处于sheetData标签内sax只解析此标签内的内容其它标签忽略
private boolean isInSheetData;
// 上一次的内容
private final StrBuilder lastContent = StrUtil.strBuilder();
@ -195,7 +197,17 @@ public class Excel07SaxReader extends DefaultHandler implements ExcelSaxReader<E
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
final ElementName name = ElementName.of(localName);
if("sheetData".equals(qName)){
this.isInSheetData = true;
return;
}
if(false == this.isInSheetData){
// 非sheetData标签忽略解析
return;
}
final ElementName name = ElementName.of(qName);
this.curElementName = name;
if(null != name){
@ -217,12 +229,24 @@ public class Excel07SaxReader extends DefaultHandler implements ExcelSaxReader<E
*/
@Override
public void endElement(String uri, String localName, String qName) {
if("sheetData".equals(qName)){
// sheetData结束不再解析别的标签
this.isInSheetData = false;
return;
}
if(false == this.isInSheetData){
// 非sheetData标签忽略解析
return;
}
this.curElementName = null;
if (ElementName.c.match(localName)) { // 单元格结束
if (ElementName.c.match(qName)) { // 单元格结束
endCell();
} else if (ElementName.row.match(localName)) {// 行结束
} else if (ElementName.row.match(qName)) {// 行结束
endRow();
}
// 其它标签忽略
}
/**
@ -230,6 +254,11 @@ public class Excel07SaxReader extends DefaultHandler implements ExcelSaxReader<E
*/
@Override
public void characters(char[] ch, int start, int length) {
if(false == this.isInSheetData){
// 非sheetData标签忽略解析
return;
}
final ElementName elementName = this.curElementName;
if(null != elementName){
switch (elementName){
@ -303,7 +332,10 @@ public class Excel07SaxReader extends DefaultHandler implements ExcelSaxReader<E
* @param attributes 属性列表
*/
private void startRow(Attributes attributes) {
this.rowNumber = Long.parseLong(AttributeName.r.getValue(attributes)) - 1;
final String rValue = AttributeName.r.getValue(attributes);
if(null != rValue){
this.rowNumber = Long.parseLong(rValue) - 1;
}
}
/**
@ -399,7 +431,7 @@ public class Excel07SaxReader extends DefaultHandler implements ExcelSaxReader<E
len++;
}
while (len-- > 0) {
addCellValue(curCell++, "");
addCellValue(curCell++, StrUtil.EMPTY);
}
}
}

View File

@ -175,4 +175,12 @@ public class ExcelSaxReadTest {
ExcelUtil.getReader(file).read().forEach(Console::log);
}
@Test
@Ignore
public void readXlsmTest(){
ExcelUtil.readBySax("d:/test/WhiteListTemplate.xlsm", -1, (sheetIndex, rowIndex, rowlist) -> {
Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
});
}
}