我正在尝试使用jXLS 库读取 excel 文件,并且我想使用哈希图使其动态化,但我被困在那里。下面提供了相关的类。
请帮助使用哈希图而不是数组列表。
JXLS解析器
package com.metricstream.service.xlsparser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jxls.reader.ReaderBuilder;
import net.sf.jxls.reader.XLSReadStatus;
import net.sf.jxls.reader.XLSReader;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.xml.sax.SAXException;
public class JXLSParser {
private static final String CHAR_S = "s";
public <T> List<T> parseXLS(InputStream xmlStream, InputStream xlsStream,
Class<T> tClass) throws AppException {
List<T> tList = new ArrayList<T>();
XLSReader mainReader = null;
try {
mainReader = ReaderBuilder.buildFromXML(xmlStream);
} catch (IOException ioException) {
throw new AppException(ioException);
} catch (SAXException saxException) {
throw new AppException(saxException);
}
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(tClass.getSimpleName().toLowerCase().concat(CHAR_S), tList);
XLSReadStatus readStatus = null;
try {
readStatus = mainReader.read(xlsStream, beans);
} catch (InvalidFormatException invalidFormatException) {
throw new AppException(invalidFormatException);
} catch (IOException ioException) {
throw new AppException(ioException);
}
if (readStatus.isStatusOK()) {
System.out.println("Read Status: ok");
} else {
throw new AppException("xls cannot be read properly");
}
return tList;
}
}
主要的
package com.metricstream.service.xlsparser;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.formula.functions.T;
import com.metricstream.exercise.employee.Employee;
public class Exercise {
public static void main(String args[]) throws AppException {
String xmlConfig = "F:\\workspace/XLSReader/XLSReader/XLSReader/employee.xml";
String dataXLS = "F:\\workspace/XLSReader/XLSReader/XLSReader/employee.xlsx";
JXLSParser employeeParser = new JXLSParser();
InputStream inputXML;
try {
inputXML = new BufferedInputStream(new FileInputStream(xmlConfig));
InputStream inputXLS = new BufferedInputStream(new FileInputStream(
dataXLS));
List<Employee> employee = employeeParser.parseXLS(inputXML,
inputXLS, Employee.class);
// Map<String,Object> map = new HashMap<String,Object>(employee.size());
/* for( Employee emp: employee)
{
map.put(emp.getKey(),emp.getValue());
System.out.println(emp);
}*/
System.out.println(employee);
} catch (FileNotFoundException e) {
throw new AppException(e);
}
}
}