import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.dayangit.evias.urp.core.ExceptionHandle;
/**
* 通用读取Excel文件
* @author leedpan
*
*/
public class ReadExcelUtil {
/**
* 解析并读取EXCEL文件fileName中相应指定位置值
* @param fileName
* @param catalogKeyString 编目类扩展属性中设置的编目属性以及其对应的EXCEL中位置
* @return catalogData 编目属性以及值
* @throws IOException
*/
public JSONObject readExcelForImport(String fileName , String catalogKeyString) throws IOException{
JSONObject catalogData = new JSONObject();
JSONObject catalogKey = JSONObject.fromObject(catalogKeyString);
// 读取EXCEL 文件
Workbook workBook = null;
try {
workBook = new XSSFWorkbook(new FileInputStream(fileName));
} catch (Exception ex) {
workBook = new HSSFWorkbook(new FileInputStream(fileName));
}
Sheet sheet = workBook.getSheetAt(0);
if(sheet == null)
return null;
else{
// 解析keyValue
Iterator keyIter = catalogKey.keys();
while(keyIter.hasNext()){
Object key = keyIter.next();
int rowNum = 0;
int columnNum = 0;
Cell cell = null;
String cellValue = "";
String position = catalogKey.get(key) == null ? "" : String.valueOf(catalogKey.get(key));
if(!"".equals(position)){
String rowColumn[] = position.split("-");
if(!"".equals(rowColumn[0]) && !"".equals(rowColumn[1])){
rowNum = Integer.parseInt(rowColumn[0]) - 1;
char col = rowColumn[1].charAt(0) ;
columnNum = col - 65;
Row row = sheet.getRow(rowNum);
cell = row.getCell(columnNum);
if (cell == null) {
continue;
}
}
cellValue = readCellValue(cell);
if(!"".equals(cellValue)){
catalogData.accumulate((String)key, cellValue);
}else{
continue;
}
}
}
}
return catalogData;
}
public ArrayList<ArrayList<String>> readExcel(String fileName,String path) {
ArrayList<ArrayList<String>> Row =new ArrayList<ArrayList<String>>();
try {
Workbook workBook = null;
try {
workBook = new XSSFWorkbook(new FileInputStream(path+"\\"+fileName));
} catch (Exception ex) {
workBook = new HSSFWorkbook(new FileInputStream(path+"\\"+fileName));
}
for (int numSheet = 0; numSheet < workBook.getNumberOfSheets(); numSheet++) {
Sheet sheet = workBook.getSheetAt(numSheet);
if (sheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row row = sheet.getRow(rowNum);
if (row == null) {
continue;
}
// 循环列Cell
ArrayList<String> arrCell =new ArrayList<String>();
for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell == null) {
continue;
}
String cellValue = readCellValue(cell);
System.out.println(rowNum + "-" + cellNum + ":" + cellValue);
arrCell.add(cellValue);
}
Row.add(arrCell);
}
}
} catch (IOException e) {
ExceptionHandle.handle(e);
}
return Row;
}
private String readCellValue(Cell cell) {
int cellType = cell.getCellType();
Object objValue = null;
switch(cellType){
case Cell.CELL_TYPE_BOOLEAN:
objValue = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
objValue = cell.getNumericCellValue();
break;
default :
objValue = cell.getStringCellValue();
break;
}
return String.valueOf(objValue);
}
public static void main(String[] args) {
ReadExcelUtil s= new ReadExcelUtil();
// ArrayList<ArrayList<String>> row=s.readExcel("工作簿1.xls","D:/");
// System.out.println("size:"+row.size());
// for (ArrayList<String> cell : row) {
// for (String str : cell) {
// System.out.println(str);
// }
// }
String catalogKeyString = "{'文档编号':'4-B','文档名称':'4-D','资料分类':'4-F','数量':'4-H'," +
"'时间长度':'5-H','题名':'6-B','关键词':'7-B','内容简介':'8-B','编目归类':'9-B'," +
"'密级':'10-B','保管期限':'10-F','创作日期':'11-B','作者':'11-F','填表人':'12-B'," +
"'电话':'12-F','审核人':'13-B','备注':'14-B'}";
JSONObject obj = null;
try {
obj = s.readExcelForImport("D:/工作簿1.xls", catalogKeyString);
// Resource resource = ResourceManager.loadResource("49CC854F727B44C4BAFBE5BEBE864DD7");
// CatalogService2 service = new CatalogService2();
// service.updateDynamicData2(resource, obj);
// resource.getDynamicData().setValueByStyle(Attribute.STYLE_CATALOGMAN, "Administrator");
// ResourceManager.updateResource(null, resource);
System.out.println(obj);
} catch (IOException e) {
e.printStackTrace();
} /*catch (URPException e) {
e.printStackTrace();
}*/
}
}