poi操作Excel

package com;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;

public class UploadFee {

	public static void main(String[] args) {
		try {
			UploadFee u = new UploadFee();
			File file = new File("D:\\My Documents\\Downloads\\昆大08级2.xls");
			FileInputStream input = new FileInputStream(file);
			Map feeMap = new HashMap();// 以学生为id为key,Value为费用类型id键Value为费用的
			// map
			Map childMap = new HashMap();
			Map dateMap = new HashMap();
			Map markMap = new HashMap();
			List students = new ArrayList();
			HSSFWorkbook workbook = new HSSFWorkbook(input);
			HSSFSheet sheet = workbook.getSheetAt(0);// Excel标签
			HSSFRow row = sheet.getRow(2);// 行
			HSSFCell cell = row.getCell((short) 0);// 列
			int j = 3;
			int num = 0;
			while (cell.getStringCellValue() != null
					&& !cell.getStringCellValue().equals("")
					&& j <=sheet.getPhysicalNumberOfRows()) {
				
				String str = "";
				Long fee = null;
				for (int i = 0; i <= 5; i++) {
					if (row.getCell((short) i) != null) {
						cell = row.getCell((short) i);
						switch (cell.getCellType()) {
						case HSSFCell.CELL_TYPE_ERROR:
							break;
						case HSSFCell.CELL_TYPE_FORMULA:
							break;
						case HSSFCell.CELL_TYPE_STRING:
							if (i == 0) {
								num = num + 1;
								System.out.println("---------num:" + num +"===rownum:" + sheet.getPhysicalNumberOfRows());
							}
							str = cell.getStringCellValue();
							System.out.println("---------------:" + str);
							break;
						case HSSFCell.CELL_TYPE_BLANK:
							break;
						case HSSFCell.CELL_TYPE_NUMERIC:
							fee = (long) cell.getNumericCellValue();
							System.out.println("----------fee:" + fee);
						default:
							str += "t";
						}

					}

				}
				row = sheet.getRow(j++);
				cell = row.getCell((short) 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String strToDate(String str) {
		// System.out.println("------------start----------");
		String strValue = new String();
		String year = new String();
		String month = new String();
		String day = new String();
		String testDate = null;
		strValue = str;
		if (strValue.length() == 0)
			System.out.println("日期不能为空! ");
		String temp = null;
		if (strValue.length() <= 10 && strValue.length() > 0) {
			for (int i = 0; i < str.length(); i++) {
				if (str.charAt(i) == '.') {
					temp = strValue.substring(0, i);
				} else {
					continue;
				}
				for (int j = 0; j < temp.length(); j++) {
					if ((temp.length() == 2 && temp.charAt(j) != '.')) {
						if (year.length() == 2) {
							year = "20" + year;
						} else {
							year = temp;
						}
					} else if (temp.charAt(j) == '.') {
						month = temp.substring(j + 1, temp.length());
						day = strValue.substring(temp.length() + 1, strValue
								.length());
					}
				}

			}
		}
		testDate = year + "-" + month + "-" + day;
		// System.out.println("==========================返回日期:" + testDate);
		return testDate;
	}

}


你可能感兴趣的:(java,apache,Excel,J#)