/**
*
* @param filepath 文件路径
* @param filename 文件名
* @param startrow 开始行号 从0开始
* @param startcol 开始列号 从0开始
* @param sheetnum sheet
* @param format double型小数据点保留位数,如: #.##
* @return
*/
public static List<Object> readExcel(String filepath, String filename, int startrow, int startcol, int sheetnum, String format) throws Exception {
List<Object> varList = new ArrayList<Object>();
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(filepath, filename)));
HSSFSheet sheet = workbook.getSheetAt(sheetnum); //sheet 从0开始
int rowNum = sheet.getLastRowNum() + 1; //取得最后一行的行号
for (int i = startrow; i < rowNum; i++) { //行循环开始
PageData varpd = new PageData();
HSSFRow row = sheet.getRow(i); //行
if(row==null) break; //中间如果有空行,则退出
int cellNum = row.getLastCellNum(); //每行的最后一个单元格位置
for (int j = startcol; j < cellNum; j++) { //列循环开始
HSSFCell cell = row.getCell(Short.parseShort(j + ""));
String cellValue = getCellValue(cell, format);
varpd.put("var"+j, cellValue);
}
varList.add(varpd);
}
workbook.close();
} catch (Exception e) {
logger.error("解析Excel文件出错", e);
throw e;
}
return varList;
}
PageData为一个类封装了页面的参数:如下
/**
* 说明:参数封装Map
* 创建人:
* 修改时间:2014年9月20日
* @version
*/
public class PageData extends HashMap<String,Object> implements Map<String,Object>{
private static final long serialVersionUID = 1L;
protected Map<String,Object> map = null;
private HttpServletRequest request;
public PageData(HttpServletRequest request){
this.request = request;
Map<String,String[]> properties = request.getParameterMap();
Map<String,Object> returnMap = new HashMap<String,Object>();
Iterator<Entry<String,String[]>> entries = properties.entrySet().iterator();
Map.Entry<String,String[]> entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if(null == valueObj){
value = "";
}else if(valueObj instanceof String[]){
// String[] values = (String[])valueObj;
// for(int i=0;i<values.length;i++){
// value = values[i] + ",";
// }
// value = value.substring(0, value.length()-1);
// 多参数列表解析校正 -updated:2017/01/05
StringBuilder sb = new StringBuilder();
String[] values = (String[])valueObj;
for(int i=0;i<values.length;i++){
sb.append(values[i]).append(",");
}
value = sb.length() != 0 ? sb.substring(0, sb.length()-1) : "";
}else{
value = valueObj.toString();
}
returnMap.put(name, value);
}
map = returnMap;
}
public PageData(Map<String,Object> map){
this.map = map;
}
public PageData() {
map = new HashMap<String,Object>();
}
@Override
public Object get(Object key) {
Object obj = null;
if(map.get(key) instanceof Object[]) {
Object[] arr = (Object[])map.get(key);
obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]);
} else {
obj = map.get(key);
}
return obj;
}
public String getString(String key) {
Object obj = get(key);
String value = null;
if(null != obj) {
value = ((String) obj).trim();
}
return value;
}
@Override
public Object put(String key, Object value) {
return map.put(key, value);
}
@Override
public Object remove(Object key) {
return map.remove(key);
}
public void clear() {
map.clear();
}
public boolean containsKey(String key) {
// TODO Auto-generated method stub
return map.containsKey(key);
}
public boolean containsValue(Object value) {
// TODO Auto-generated method stub
return map.containsValue(value);
}
public Set<Entry<String, Object>> entrySet() {
// TODO Auto-generated method stub
return map.entrySet();
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return map.isEmpty();
}
public Set<String> keySet() {
// TODO Auto-generated method stub
return map.keySet();
}
public void putAll(Map<? extends String,? extends Object> t) {
// TODO Auto-generated method stub
map.putAll(t);
}
public int size() {
// TODO Auto-generated method stub
return map.size();
}
public Collection<Object> values() {
// TODO Auto-generated method stub
return map.values();
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。