首页 > 代码库 > java自定义jar包读取Excel(包含2003和2007)数据,并举例说明

java自定义jar包读取Excel(包含2003和2007)数据,并举例说明

用java自定义jar包读取excel数据支持excel2007和excel2003

在http://download.csdn.net/detail/u010792467/8079355下载所需要的包

如果需要excel2003和excel2007文件可以去

http://download.csdn.net/detail/u010792467/8072009下载

在http://download.csdn.net/detail/u010792467/8079345下载工程

自定义jar包运用举例

<span style="font-size:18px;">package com.readExcel;

import java.util.List;

import Excel.ImportExeclTool;

public class exceltest {
	public static void main(String[] args) {
//		String filePath = "D:\\excel\\EXCEL2007测试.xlsx";
		 String filePath="D:\\excel\\EXCEL2003测试.xls";
		ImportExeclTool impexcel = new ImportExeclTool();
		// 全部sheet页内容
		// List<List<String>> list= impexcel.readExcel(filePath);
		// filePath excel文件路径
		// 1 代表第一个sheet页 2 代表第一个sheet页...
		List<List<String>> list = impexcel.readExcel(filePath, 1);
		if (list != null) {

			for (int i = 0; i < list.size(); i++) {

				System.out.print("第" + (i) + "行");

				List<String> listCell = list.get(i);

				for (String s : listCell) {
					System.out.print(" " + s);
				}
				System.out.println();

			}

		}
		System.out.println("运行完成");
	}
}
</span>


自定义jar包源码

<span style="font-size:18px;">package Excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
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;

public class ImportExeclTool {
	private String errorInfo;
	private static int readSheet = 0;
	private static boolean readSheetNum = false;
	private static boolean isGtSheetNum = false;

	public boolean validateExcel(String filePath) {

		if (filePath == null
				|| !(is2003Excel(filePath) || is2007Excel(filePath))) {

			errorInfo = "文件名不是excel格式";

			return false;
		}

		File file = new File(filePath);

		if (file == null || !file.exists()) {

			errorInfo = "excel文件不存在";

			return false;

		}

		return true;

	}

	public List<List<String>> readExcel(String filePath) {

		List<List<String>> dataList = new ArrayList<List<String>>();

		InputStream is = null;

		try {

			if (!validateExcel(filePath)) {

				System.out.println();
				List<String> list3 = new ArrayList<String>();
				list3.add(errorInfo);
				dataList.add(list3);
				return dataList;

			}

			boolean is2003Excel = true;

			if (is2007Excel(filePath)) {

				is2003Excel = false;
			}

			File file = new File(filePath);

			is = new FileInputStream(file);

			dataList = readFile(is, is2003Excel);

			is.close();

		} catch (Exception ex) {

			ex.printStackTrace();

		} finally {

			if (is != null) {

				try {

					is.close();

				} catch (IOException e) {

					is = null;

					e.printStackTrace();

				}

			}

		}

		return dataList;

	}

	public List<List<String>> readFile(InputStream inputStream,
			boolean is2003Excel) {

		List<List<String>> dataLists = null;

		try {

			Workbook wb = null;

			if (is2003Excel) {
				wb = new HSSFWorkbook(inputStream);
			} else {
				wb = new XSSFWorkbook(inputStream);
			}
			int sheetNum = sheetCirculation(wb);
			if (readSheet >= sheetNum) {
				List<String> list2 = new ArrayList<String>();
				list2.add("输入页数大于实际sheet页数!!!");
				List list4 = new ArrayList();
				list4.add(list2);
				this.isGtSheetNum = true;
				return list4;
			}
			List<List<String>> dataList = new ArrayList<List<String>>();
			if (readSheetNum) {
				dataLists = read(dataList, wb, readSheet);
			} else {
				for (int i = 0; i < sheetNum; i++) {
					dataLists = read(dataList, wb, i);
				}
			}

		} catch (IOException e) {

			e.printStackTrace();

		}

		return dataLists;

	}

	private List<List<String>> read(List<List<String>> dataList, Workbook wb,
			int sheets) {
		int totalRows = 0;
		int totalCells = 0;

		Sheet sheet = wb.getSheetAt(sheets);

		totalRows = sheet.getPhysicalNumberOfRows();

		if (totalRows >= 1 && sheet.getRow(0) != null) {

			totalCells = sheet.getRow(0).getPhysicalNumberOfCells();

		}
		for (int r = 0; r < totalRows; r++) {

			Row row = sheet.getRow(r);

			if (row == null) {

				continue;

			}

			List<String> rowDataList = new ArrayList<String>();

			for (int c = 0; c < totalCells; c++) {

				Cell cell = row.getCell(c);

				String cellValue = http://www.mamicode.com/"";>

作者:儱剑阿攵
转载请注明链接:http://blog.csdn.net/awenluck/article/details/
40442207

java自定义jar包读取Excel(包含2003和2007)数据,并举例说明