public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { // c => cell if(name.equals("c")) { // Print the cell reference System.out.print(attributes.getValue("r") + " - "); // Figure out if the value is an index in the SS String cellType = attributes.getValue("t"); if(cellType != null && cellType.equals("s")) { nextIsString = true; } else { nextIsString = false; } } // Clear contents cache lastContents = ""; }
public void endElement(String uri, String localName, String name) throws SAXException { // Process the last contents as required. // Do now, as characters() may be called more than once
if(nextIsString) { int idx = Integer.parseInt(lastContents); lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString(); } // v => contents of a cell // Output after we've seen the string contents if(name.equals("v")) { System.out.println(lastContents); nextIsString=false; } } 没有row对象 我知道 是7列. 但是我不知道 哪个中间有 空单元格 .
你好. 我看见了. 但是我这个没有row 我用的解析是 public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
XMLReader parser =
XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser"
);
ContentHandler handler = new SheetHandler(sst);
parser.setContentHandler(handler);
return parser;
} /**
* See org.xml.sax.helpers.DefaultHandler javadocs
*/
private static class SheetHandler extends DefaultHandler {
private SharedStringsTable sst;
private String lastContents;
private boolean nextIsString;
private SheetHandler(SharedStringsTable sst) {
this.sst = sst;
}
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// c => cell
if(name.equals("c")) {
// Print the cell reference
System.out.print(attributes.getValue("r") + " - ");
// Figure out if the value is an index in the SS
String cellType = attributes.getValue("t");
if(cellType != null && cellType.equals("s")) {
nextIsString = true;
} else {
nextIsString = false;
}
}
// Clear contents cache
lastContents = "";
}
public void endElement(String uri, String localName, String name)
throws SAXException {
// Process the last contents as required.
// Do now, as characters() may be called more than once
if(nextIsString) {
int idx = Integer.parseInt(lastContents);
lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
} // v => contents of a cell
// Output after we've seen the string contents
if(name.equals("v")) {
System.out.println(lastContents);
nextIsString=false;
}
}
没有row对象 我知道 是7列. 但是我不知道 哪个中间有 空单元格 .
例如spans的列数为7行号为2的那么list的值就是["A2","B2","C2","D2","E2","F2","G2"]
楼主到这里应该有头绪了吧
楼主可以在你的代码里面的startElement事件里面加入
if(name.equals("row")){//解析row节点的spans属性
String[] spans=attributes.getValue("spans").spilt(":");
String rowNum=spans[0];//行号
int cellNum=spans[1];//列数
list.clear();//每次记入时先把list清空下由于频繁的删改所以用LinkedList
//把他们放入list中,后面你name.equals("c")解析到列的时候只要根据这个list就知道哪个列少了
for(int i=0;i<cellNum;i++){
list.add((char)('A'+i)+rowNum);
}
}
例如spans的列数为7行号为2的那么list的值就是["A2","B2","C2","D2","E2","F2","G2"]
楼主到这里应该有头绪了吧
楼主可以在你的代码里面的startElement事件里面加入
if(name.equals("row")){//解析row节点的spans属性
String[] spans=attributes.getValue("spans").spilt(":");
String rowNum=spans[0];//行号
int cellNum=spans[1];//列数
list.clear();//每次记入时先把list清空下由于频繁的删改所以用LinkedList
//把他们放入list中,后面你name.equals("c")解析到列的时候只要根据这个list就知道哪个列少了
for(int i=0;i<cellNum;i++){
list.add((char)('A'+i)+rowNum);
}
}我看见你的第一次回复我就知道可以这么弄了. 但是- - .. 我这113行. 我觉得太麻烦了. ..我先去试试吧 实在没有办法了
管你多少列代码是通用的for循环里面做了又不叫你自己手写113次。你这个excel也太变态了113列有哪个excel一行有113列的看都看不过来了
管你多少列代码是通用的for循环里面做了又不叫你自己手写113次。你这个excel也太变态了113列有哪个excel一行有113列的看都看不过来了周五没怎么弄 脑袋大了. 现在正在弄呢. 113列 14W的数据.. 太恶心了.