POI读取Excel通用代码---支持2003,2007

package com.citics.crm.customerwidget.utils;

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

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 org.apache.poi2.hssf.usermodel.HSSFCell;
import org.apache.poi2.hssf.usermodel.HSSFRow;
import org.apache.poi2.hssf.usermodel.HSSFSheet;
import org.apache.poi2.hssf.usermodel.HSSFWorkbook;

public class ExcelUtil {

private ExcelUtil(){};
private static List<String> columns;//要解析excel中的列名
private static int sheetNum = 0;//要解析的sheet下标

/**
* poi读取excle
* @return
*/
public static String readExcel(File file){
StringBuilder retJson = new StringBuilder();
InputStream inStream = null;
try {
inStream = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(inStream);
HSSFSheet sheet = workbook.getSheetAt(sheetNum);//获得表
int lastRowNum = sheet.getLastRowNum();//最后一行
retJson.append("[");
for(int i = 0 ;i < lastRowNum;i++){
HSSFRow row = sheet.getRow(i);//获得行
String rowJson = readExcelRow(row);
retJson.append(rowJson);
if(i<lastRowNum-1)
retJson.append(",");
}
retJson.append("]");
} catch (Exception e) {
try {
inStream = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(inStream);
XSSFSheet sheet = workbook.getSheetAt(sheetNum);
int lastRowNum = sheet.getLastRowNum();//最后一行
retJson.append("[");
for(int i = 0 ;i < lastRowNum;i++){
XSSFRow row = sheet.getRow(i);//获得行
String rowJson = readExcelRow(row);
retJson.append(rowJson);
if(i<lastRowNum-1)
retJson.append(",");
}
retJson.append("]");
} catch (IOException e1) {
e1.printStackTrace();
}
}finally{
close(null,inStream);
}
return retJson.toString();
}
/**
* 读取行值
* @return
*/
private static String readExcelRow(HSSFRow row){
StringBuilder rowJson = new StringBuilder();
int lastCellNum = ExcelUtil.columns.size();//最后一个单元格
rowJson.append("{");
for(int i = 0 ;i<lastCellNum;i++){
HSSFCell cell = row.getCell(i);
String cellVal = readCellValue(cell);
rowJson.append(toJsonItem(columns.get(i), cellVal));
if(i<lastCellNum-1)
rowJson.append(",");
}
rowJson.append("}");
return rowJson.toString();
}
/**
* 读取行值
* @return
*/
private static String readExcelRow(XSSFRow row){
StringBuilder rowJson = new StringBuilder();
int lastCellNum = ExcelUtil.columns.size();//最后一个单元格
rowJson.append("{");
for(int i = 0 ;i<lastCellNum;i++){
XSSFCell cell = row.getCell(i);
String cellVal = readCellValue(cell);
rowJson.append(toJsonItem(columns.get(i), cellVal));
if(i<lastCellNum-1)
rowJson.append(",");
}
rowJson.append("}");
return rowJson.toString();
}

/**
* 读取单元格的值
* @param hssfCell
* @return
*/
@SuppressWarnings("static-access")
private static String readCellValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getRichStringCellValue());
}
}
/**
* 读取单元格的值
* @param hssfCell
* @return
*/
@SuppressWarnings("static-access")
private static String readCellValue(XSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getRichStringCellValue());
}
}
/**
* 转换为json对
* @return
*/
private static String toJsonItem(String name,String val){
return "\""+name+"\":\""+val+"\"";
}
/**
* 关闭io流
* @param fos
* @param fis
*/
private static void close(OutputStream out, InputStream in) {
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println("InputStream关闭失败");
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
System.out.println("OutputStream关闭失败");
e.printStackTrace();
}
}
}
public static List<String> getColumns() {
return ExcelUtil.columns;
}
public static void setColumns(List<String> columns) {
ExcelUtil.columns = columns;
}
public static int getSheetNum() {
return sheetNum;
}
public static void setSheetNum(int sheetNum) {
ExcelUtil.sheetNum = sheetNum;
}

}

http://javasam.iteye.com/blog/2117777
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值