java解析excel文档和JSONObject
package com.company.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
public class ExcelParser {
public static final String OUTPUTFILE = "F:/DownLoads/temp/aaaaaaa.xls";
public static final String FILETOBEREAD = "F:/DownLoads/a.xls";
/**
* 创建excel查看相应api
*/
public void createExcel() {
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
FileOutputStream fOut = new FileOutputStream(OUTPUTFILE);
workbook.write(fOut);
fOut.flush();
fOut.close();
System.out.println("文件生成...");
} catch (Exception e) {
e.printStackTrace();
}
}
public void readExcel() throws Exception {
String arr = "";
JSONArray jsonArray = new JSONArray();
List list = new ArrayList();
Object[] objects = new Object[21];
int index = 0;
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(FILETOBEREAD));
int sheetNum = workbook.getNumberOfSheets();
for (int i = 0; i < sheetNum; i++) {
HSSFSheet sheet = workbook.getSheetAt(i);
int rowNum = sheet.getLastRowNum();
for (int r = 0; r <= rowNum; r++) {
HSSFRow row = sheet.getRow(r);
JSONObject jsonObject = new JSONObject();
int k = 0;
for (Iterator<Cell> it = row.cellIterator(); it.hasNext(); ) {
switch (k ++) {
case 0:
jsonObject.put("gwwl_other_rdjg", it.next().toString());
break;
case 7:
jsonObject.put("ycoordinate", it.next().toString());
break;
case 8:
jsonObject.put("xcoordinate", it.next().toString());
break;
default:
jsonObject.put("key_" + k, it.next().toString());
break;
}
}
//arr += (jsonObject.toString() + ", ");
jsonArray.add(jsonObject.toString());
list.add(jsonObject);
objects[index ++] = jsonObject;
}
}
//System.out.println(arr);
//System.out.println(jsonArray);
//System.out.println(jsonArray.size());
//printJsonArray(jsonArray);
//printList(list);
printArray(objects);
}
private void printArray(Object[] objects) {
System.out.println(objects.length);
for (Object o : objects) {
//System.out.println(o);
JSONObject jsonObject = (JSONObject) o;
//System.out.println(jsonObject.getString("gwwl_other_rdjg"));
}
}
private void printList(List list) {
// System.out.println(list);
System.out.println(list.toString());
for (Iterator it = list.iterator(); it.hasNext(); ) {
//JSONObject jsonObject = (JSONObject) it.next();
//System.out.println(jsonObject.getString("gwwl_other_rdjg"));
//System.out.println(it.next());
}
}
private void printJsonArray(JSONArray jsonArray) {
for (Iterator<?> it = jsonArray.iterator(); it.hasNext(); ) {
JSONObject json = (JSONObject) it.next();
System.out.println(json.getString("gwwl_other_rdjg"));
}
}
public