android用jxl读取Excel(2003,2007)

public class ExcelUtils {
public static List readExcel(String filePath) throws Exception {
return readExcel(filePath, 0);
}
public static List readExcel(String filePath, int sheet) throws Exception {
if (filePath.trim().toLowerCase().endsWith(".xls")) {
return readXLS(new File(filePath), sheet);
} else {
return readXLSX(new File(filePath), sheet);
}
}
public static List readXLS(File file, int sheetIndex) throws Exception {
List list = new ArrayList ();
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(file);
Sheet sheet = workbook.getSheet(sheetIndex);
int columnCount = sheet.getColumns();
int rowCount = sheet.getRows();
for (int i = 0; i < rowCount; i++) {
String[] item = new String[columnCount];
for (int j = 0; j < columnCount; j++) {
Cell cell = sheet.getCell(j, i);
String str = "";
if (cell.getType() == CellType.NUMBER) {
str = ((NumberCell) cell).getValue() + "";
} else if (cell.getType() == CellType.DATE) {
str = "" + ((DateCell) cell).getDate();
} else {
str = "" + cell.getContents();
}
item[j] = str;
}
list.add(item);
}
} finally {
if (workbook != null)
workbook.close();
}
return list;
}
public static List readXLSX(File file, int sheet) throws Exception {
List list = new ArrayList ();
ArrayList item = new ArrayList ();
String v = null;
boolean flat = false;
List ls = new ArrayList ();
ZipFile xlsxFile = new ZipFile(file);
ZipEntry sharedStringXML = xlsxFile
.getEntry("xl/sharedStrings.xml");
InputStream inputStream = xlsxFile.getInputStream(sharedStringXML);
XmlPullParser xmlParser = Xml.newPullParser();
xmlParser.setInput(inputStream, "utf-8");
int evtType = xmlParser.getEventType();
while (evtType != XmlPullParser.END_DOCUMENT) {
switch (evtType) {
case XmlPullParser.START_TAG:
String tag = xmlParser.getName();
if (tag.equalsIgnoreCase("t")) {
ls.add(xmlParser.nextText());
}
break;
case XmlPullParser.END_TAG:
break;
default:
break;
}
evtType = xmlParser.next();
}
ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/sheet" + (sheet + 1) + ".xml");
InputStream inputStreamsheet = xlsxFile.getInputStream(sheetXML);
XmlPullParser xmlParsersheet = Xml.newPullParser();
xmlParsersheet.setInput(inputStreamsheet, "utf-8");
int evtTypesheet = xmlParsersheet.getEventType();
while (evtTypesheet != XmlPullParser.END_DOCUMENT) {
switch (evtTypesheet) {
case XmlPullParser.START_TAG:
String tag = xmlParsersheet.getName();
if (tag.equalsIgnoreCase("row")) {
} else if (tag.equalsIgnoreCase("c")) {
String t = xmlParsersheet.getAttributeValue(null, "t");
if (t != null) {
flat = true;
System.out.println(flat + "有");
} else {
System.out.println(flat + "没有");
flat = false;
}
} else if (tag.equalsIgnoreCase("v")) {
v = xmlParsersheet.nextText();
if (v != null) {
if (flat) {
item.add(ls.get(Integer.parseInt(v)));
} else {
item.add(v);
}
}
}
break;
case XmlPullParser.END_TAG:
if (xmlParsersheet.getName().equalsIgnoreCase("row")
&;&; v != null) {
list.add(item.toArray(new String[item.size()]));
item = new ArrayList ();
}
break;
}
evtTypesheet = xmlParsersheet.next();
}
return list;
}
}