import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
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;
/**
*
* @author shiwt
*
* java读取excel文件
*
* 一个Excel文件的层次:Excel文件->工作表->行->单元格 对应到POI中,为:workbook->sheet->row->cell
*
*/
public class POItest {
public static String outputFile = "e://test//tels.xls";
public static String fileToBeRead = "e://test//tels.xls";
public void CreateExcel() {
try {
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
// 如要新建一名为"效益指标"的工作表,其语句为:
// HSSFSheet sheet = workbook.createSheet("效益指标");
HSSFSheet sheet = workbook.createSheet();
// 在索引0的位置创建行(最顶端的行)
HSSFRow row = sheet.createRow((short) 0);
//在索引0的位置创建单元格(左上端)
HSSFCell cell = row.createCell((short) 0);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
cell.setCellValue("sweater");
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println("文件生成...");
} catch (Exception e) {
System.out.println("已运行 xlCreate() : " + e);
}
}
/**
*
* 读取excel,遍历各个小格获取其中信息,并判断其是否是手机号码,并对正确的手机号码进行显示
*
*
* 注意: 1.sheet, 以0开始,以workbook.getNumberOfSheets()-1结束 2.row,
* 以0开始(getFirstRowNum),以getLastRowNum结束 3.cell,
* 以0开始(getFirstCellNum),以getLastCellNum结束, 结束的数目不知什么原因与显示的长度不同,可能会偏长
*
*/
public void readExcel() {
//将被表示成1.3922433397E10的手机号转化为13922433397,不一定是最好的转换方法
DecimalFormat df = new DecimalFormat("#");
try {
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
fileToBeRead));
//System.out
// .println("===SheetsNum===" + workbook.getNumberOfSheets());//获取sheet数
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet
//System.out.println("+++getFirstRowNum+++" +
// aSheet.getFirstRowNum());//
//System.out.println("+++getLastRowNum+++" +
// aSheet.getLastRowNum());
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
//System.out.println(">>>getFirstCellNum<<<"+
// aRow.getFirstCellNum());
//System.out.println(">>>getLastCellNum<<<"+
// aRow.getLastCellNum());
for (short cellNumOfRow = 0; cellNumOfRow <= aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);
int cellType = aCell.getCellType();
//System.out.println(cellType);
switch (cellType) {
case 0://Numeric
String strCell = df.format(aCell
.getNumericCellValue());
System.out.println(strCell);
break;
case 1://String
strCell = aCell.getStringCellValue();
System.out.println(strCell);
break;
default:
//System.out.println("格式不对不读");//其它格式的数据
}
}
}
}
}
}
}
} catch (Exception e) {
System.out.println("ReadExcelError" + e);
}
}
public static void main(String[] args) {
POItest poi = new POItest();
//poi.CreateExcel();
poi.readExcel();
}
}
另一个导出例子!
<%@ page language="java"
import="java.util.*,
org.hibernate.criterion.*,
com.poweroa.haima.taglib.ui.util.*,
com.poweroa.haima.service.application.*,
org.apache.poi.hssf.usermodel.HSSFWorkbook,
org.apache.poi.hssf.usermodel.HSSFSheet,
org.apache.poi.hssf.usermodel.HSSFRow,
org.apache.poi.hssf.usermodel.HSSFCell"
pageEncoding="UTF-8"%>
<html>
<head>
<%
response.setContentType( " APPLICATION/OCTET-STREAM " );
response.setHeader( "Content-Disposition" ,"attachment;filename=test.xls" );
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet( " sheet1 " );
// 以下以写表头
// 表头为第一行
HSSFRow row = sheet.createRow(( short ) 0 );
HSSFCell cell1 = row.createCell(( short ) 0 );
HSSFCell cell2 = row.createCell(( short ) 1 );
HSSFCell cell3 = row.createCell(( short ) 2 );
cell1.setEncoding(( short ) 1 );
cell1.setCellType( 1 );
cell2.setEncoding(( short ) 1 );
cell2.setCellType( 1 );
cell3.setEncoding(( short ) 1 );
cell3.setCellType( 1 );
// 定义表头的内容
cell1.setCellValue( " 测试 " );
cell2.setCellValue( " 测试2 " );
cell3.setCellValue( " 测试3 " );
for ( int i = 0 ; i < 4 ; i ++ ) {
// 定义数据从第二行开始
row = sheet.createRow(( short ) i + 1 );
cell1 = row.createCell(( short ) 0 );
cell2 = row.createCell(( short ) 1 );
cell3 = row.createCell(( short ) 2 );
cell1.setEncoding(( short ) 1 );
cell1.setCellType( 1 );
cell2.setEncoding(( short ) 1 );
cell2.setCellType( 1 );
cell3.setEncoding(( short ) 1 );
cell3.setCellType( 1 );
// 填充内容
cell1.setCellValue( " ggg " );
cell2.setCellValue( " 00000 " );
cell3.setCellValue( " adfasdf " );
}
//FileOutputStream output =new OutputStream(new("c:/excel/catalog.xls"));
//wb.write(output);
wb.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
%>
</head>
</html>