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();
			}
		}
	}
}


相关文章
|
8天前
|
Java 数据库连接 数据库
org.apache.ibatis.session.AutoMappingUnknownColumnBehavior
org.apache.ibatis.session.AutoMappingUnknownColumnBehavior
14 0
|
5月前
|
Java Apache
java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
|
6月前
|
缓存 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
136 0
|
10月前
Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMa
Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMa
131 0
|
10月前
|
SQL
org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration
org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration
66 0
|
Java Apache
Cause: the class org.apache.tools.ant.taskdefs.optional.ANTLR was not found.
Cause: the class org.apache.tools.ant.taskdefs.optional.ANTLR was not found.
83 0
|
数据库 数据安全/隐私保护
|
Java 应用服务中间件 Android开发
使用 JSTL1.2 报错 org.apache.catalina.core.StandardWrapperValve.invoke...
导入所需依赖并引用如下核心标签库后报错,无法使用JSTL的原因及解决方案
使用 JSTL1.2 报错 org.apache.catalina.core.StandardWrapperValve.invoke...
|
Apache
Apache POI实例
Apache POI实例
216 0