package com.sino.test;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.sino.publicclass.database.ConnectionPool;
import com.sino.rrs.oper.RcNewAreaOper;

public class NewAreaInsert {
	public static List<List<String>> readExcel() throws Exception{
		File file=new File("assets/","newArea.xlsx");
		DecimalFormat     df = new DecimalFormat("0");// 格式化 number String 字符  
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
		DecimalFormat     nf = new DecimalFormat("0");// 格式化数字  
		
		XSSFWorkbook hwb=new XSSFWorkbook(new FileInputStream(file));
		XSSFSheet sheet=hwb.getSheetAt(0);
		int rows=sheet.getLastRowNum();
		
		List<List<String>> list=new ArrayList<List<String>>();
		List<String> rowList;
		String value=null;
		XSSFRow row=null;
		XSSFCell cell=null;
		int cols,j;
		
		for(int i=0;i<=rows;i++){
			row=sheet.getRow(i);
			if(row==null){
				list.add(null);
				continue;   //结束本次循环，进入下一个循环 
			}
			
			cols=row.getLastCellNum();   //获取有效列数
			rowList=new ArrayList<String>();
			for(j=0;j<cols;j++){
				cell=row.getCell(j);
				if(cell==null){
					rowList.add(null);
					continue;
				}
				
				 switch (cell.getCellType()) {  
				   case Cell.CELL_TYPE_STRING:  
					   value = cell.getStringCellValue();  
					   break;  
				   case Cell.CELL_TYPE_NUMERIC:  
					   if ("@".equals(cell.getCellStyle().getDataFormatString())){  
						   value = df.format(cell.getNumericCellValue());  
					   } else if ("General".equals(cell.getCellStyle().getDataFormatString())){  
						   value = nf.format(cell.getNumericCellValue());  
					   } else {  
						   value = sdf.format(DateUtil.getJavaDate(cell.getNumericCellValue()));  
					   }  
					   break;  
				   case Cell.CELL_TYPE_BOOLEAN:  
					   value = cell.getBooleanCellValue() ? "true" : "false";  
					   break;  
				   case Cell.CELL_TYPE_BLANK:  
					   value = "";  
					   break;  
				   default:  
					   value = cell.toString();  
				   }
				 rowList.add(value);
			}
			list.add(rowList);
		}
		System.out.println(list.get(0)+"==="+list.get(1));
		return list;
	}
	
	public static void main(String[] args) throws Exception{
		List<List<String>>dataList = NewAreaInsert.readExcel();
		Connection conn=null;
		try {
			conn=ConnectionPool.getConnection();
			RcNewAreaOper oper=new RcNewAreaOper(conn);
			for(List<String> data:dataList){
				//oper.insertNewArea(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			conn.close();
		}
	}
}
