Excel Handle by org.apache.poi.xssf.usermodel.*

简介: <p></p><pre name="code" class="java">package shuai.study.excel.xssf;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException

package shuai.study.excel.xssf;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

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;

/**
 * @ClassName: ExcelHandle
 * @Description: Excel Handle
 * @author Zhou Shengshuai
 * @date 2014年9月17日 下午12:57:22
 * 
 */
public class ExcelHandle {
	private String filePath = null;

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public void initialize() {
		this.excelRead();
	}

	public void destroy() {
	}

	public void excelRead() {
		File file = new File(filePath);

		if (file != null && file.exists()) {
			this.excelRead(file);
		}
	}

	private void excelRead(File file) {
		InputStream inputStream = null;

		try {
			inputStream = new FileInputStream(file);
			XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new BufferedInputStream(inputStream, 65536));

			this.excelRead(xssfWorkbook);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void excelRead(XSSFWorkbook xssfWorkbook) {
		for (int index = 0; index < xssfWorkbook.getNumberOfSheets(); index++) {
			XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(index);

			this.excelRead(xssfSheet);
		}
	}

	private void excelRead(XSSFSheet xssfSheet) {
		XSSFCell xssfCell = null;
		XSSFRow xssfRow = null;

		for (int row = 1; row < xssfSheet.getLastRowNum(); row++) {
			xssfRow = xssfSheet.getRow(row);

			if (xssfRow != null) {
				for (int cell = 0; cell < xssfRow.getLastCellNum(); cell++) {
					xssfCell = xssfRow.getCell(cell);

					if (xssfCell != null) {
						System.out.print(xssfCell.toString() + "\t");
					} else {
						System.out.print("--" + "\t");
					}
				}

				System.out.println();
			}
		}
	}
}


相关文章
|
5月前
|
SQL 流计算 OceanBase
这个错误提示表明在运行时找不到`org.apache.flink.table.api.ValidationException`类
这个错误提示表明在运行时找不到`org.apache.flink.table.api.ValidationException`类
506 4
|
4月前
|
Java API Apache
Apache POI(Poor Obfuscation Implementation
Apache POI(Poor Obfuscation Implementation
44 0
|
4月前
|
IDE Java 应用服务中间件
解决org.apache.jasper.JasperException异常
解决org.apache.jasper.JasperException异常
|
5月前
|
Java 数据库连接 数据库
org.apache.ibatis.session.AutoMappingUnknownColumnBehavior
org.apache.ibatis.session.AutoMappingUnknownColumnBehavior
48 0
|
12月前
|
缓存 Java 编译器
【Java异常】Error:(19, 21) java: 无法访问org.apache.poi.xwpf.usermodel.ParagraphAlignment 找不到org.apache.po
【Java异常】Error:(19, 21) java: 无法访问org.apache.poi.xwpf.usermodel.ParagraphAlignment 找不到org.apache.po
311 0
|
Java 数据库连接 程序员
异常:org.apache.ibatis.reflection.ReflectionException
异常:org.apache.ibatis.reflection.ReflectionException
186 0
|
数据库 数据安全/隐私保护
|
Java 应用服务中间件 Android开发
使用 JSTL1.2 报错 org.apache.catalina.core.StandardWrapperValve.invoke...
导入所需依赖并引用如下核心标签库后报错,无法使用JSTL的原因及解决方案
使用 JSTL1.2 报错 org.apache.catalina.core.StandardWrapperValve.invoke...