java读取excel转换成json

package testLog4j;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class readExcel {
	public static void main(String[] args) {
        Sheet sheet;
        Workbook book;
        Cell cell1, cell2, cell3, cell4, cell5;
        JSONArray array = new JSONArray();
        try {
            //为要读取的excel文件名
            book = Workbook.getWorkbook(new File("D://b.xls"));

            //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
            sheet = book.getSheet(0);

            for (int i = 1; i < sheet.getRows(); i++) {
                //获取每一行的单元格
                cell1 = sheet.getCell(0, i);//(列,行)
                cell2 = sheet.getCell(1, i);
               /* cell3 = sheet.getCell(2, i);
                cell4 = sheet.getCell(3, i);
                cell5 = sheet.getCell(4, i);*/
                if ("".equals(cell1.getContents())) {//如果读取的数据为空
                    break;
                }
                System.out.println(cell1.getContents());
                System.out.println(cell1.getContents());
                JSONObject object = new JSONObject();
                object.put("ID",cell1.getContents());
                object.put("编号",cell2.getContents());
               /* object.put("姓名",cell3.getContents());
                object.put("数量",cell4.getContents());
                object.put("住址",cell5.getContents());*/
                array.add(object);
            }
            System.out.println(array.toString());
            book.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自hello------world.iteye.com/blog/2303361