public class ExcelUtils {
public static List<String[]> readExcel(String filePath) throws Exception {
return readExcel(filePath, 0);
}
public static List<String[]> 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<String[]> readXLS(File file, int sheetIndex) throws Exception {
List<String[]> list = new ArrayList<String[]>();
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<String[]> readXLSX(File file, int sheet) throws Exception {
List<String[]> list = new ArrayList<String[]>();
ArrayList<String> item = new ArrayList<String>();
String v = null;
boolean flat = false;
List<String> ls = new ArrayList<String>();
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<String>();
}
break;
}
evtTypesheet = xmlParsersheet.next();
}
return list;
}
}
android用jxl读取Excel(2003,2007)
最新推荐文章于 2023-11-08 10:53:25 发布