利用Apache POI读取并解析Excel的数据
/*
?* Created on 2010-12-9
?* java讀取excel文件
?*
?*/
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;
/**
?*???????? java讀取excel文件
?*???????? 一個(gè)Excel文件的層次:Excel文件->工作表->行->單元格
?*???????? 對應(yīng)到POI中,為:workbook->sheet->row->cell
?*
?*/
public class HelloWorld {
??? public static String outputFile = "e:\\test\\tels.xls";
??? public static String fileToBeRead = "e:\\src\\test1.xls";
??? public void CreateExcel() {
??????? try {
??????????? // 創(chuàng)建新的Excel 工作簿
??????????? HSSFWorkbook workbook = new HSSFWorkbook();
??????????? // 在Excel工作簿中建一工作表,其名為缺省值
??????????? // 如要新建一名為"效益指標(biāo)"的工作表,其語句為:
??????????? // HSSFSheet sheet = workbook.createSheet("效益指標(biāo)");
??????????? HSSFSheet sheet = workbook.createSheet();
??????????? // 在索引0的位置創(chuàng)建行(最頂端的行)
??????????? HSSFRow row = sheet.createRow((short) 0);
??????????? // 在索引0的位置創(chuàng)建單元格(左上端)
??????????? HSSFCell cell = row.createCell((short) 0);
??????????? // 定義單元格為字符串類型
??????????? cell.setCellType(HSSFCell.CELL_TYPE_STRING);
??????????? // 在單元格中輸入一些內(nèi)容
??????????? //cell.setCellValue("sweater");
??????????? // 新建一輸出文件流
??????????? FileOutputStream fOut = new FileOutputStream(outputFile);
??????????? // 把相應(yīng)的Excel 工作簿存盤
??????????? workbook.write(fOut);
??????????? fOut.flush();
??????????? // 操作結(jié)束,關(guān)閉文件
??????????? fOut.close();
??????????? System.out.println("文件生成...");
??????? } catch (Exception e) {
??????????? System.out.println("已運(yùn)行 xlCreate() : " + e);
??????? }
??? }
??? /**
???? *
???? * 讀取excel,遍歷各個(gè)小格獲取其中信息,并判斷其是否是手機(jī)號碼,并對正確的手機(jī)號碼進(jìn)行顯示
???? * 注意: 1.sheet, 以0開始,以workbook.getNumberOfSheets()-1結(jié)束
???? * 2.row, 以0開始(getFirstRowNum),以getLastRowNum結(jié)束
???? * 3.cell,以0開始(getFirstCellNum),以getLastCellNum結(jié)束, 結(jié)束的數(shù)目不知什么原因與顯示的長度不同,
???? * 可能會(huì)偏長
???? *
???? */
??? public void readExcel() {
??????? // 將被表示成1.3922433397E10的手機(jī)號轉(zhuǎn)化為13922433397,不一定是最好的轉(zhuǎn)換方法
??? //? DecimalFormat df = new DecimalFormat("#");
??????? try {
??????????? // 創(chuàng)建對Excel工作簿文件的引用
??????????? HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
??????????? // System.out
??????????? // .println("===SheetsNum===" +
??????????? // workbook.getNumberOfSheets());//獲取sheet數(shù)
??????????? int iSheetNum = workbook.getNumberOfSheets();
??????????? for (int numSheets = 0; numSheets < iSheetNum; numSheets++) {
??????????????? if (null != workbook.getSheetAt(numSheets)) {
??????????????????? HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 獲得一個(gè)sheet
??????????????????? // System.out.println("+++getFirstRowNum+++" +
??????????????????? // aSheet.getFirstRowNum());//
??????????????????? // System.out.println("+++getLastRowNum+++" +
??????????????????? // aSheet.getLastRowNum());
??????????????????? int iRowNum = aSheet.getLastRowNum();
??????????????????? for (int rowNumOfSheet = 0; rowNumOfSheet <= iRowNum; rowNumOfSheet++) {
??????????????????????? if (null != aSheet.getRow(rowNumOfSheet)) {
??????????????????????????? HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
??????????????????????????? // System.out.println(">>>getFirstCellNum<<<"+
??????????????????????????? // aRow.getFirstCellNum());
??????????????????????????? // System.out.println(">>>getLastCellNum<<<"+
??????????????????????????? // aRow.getLastCellNum());
??????????????????????????? int iCellNum = aRow.getLastCellNum();
??????????????????????????? for (short cellNumOfRow = 0; cellNumOfRow < iCellNum; 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 = String(aCell.getCellNum());
??????????????????????????????????????? short sCell = aCell.getCellNum();
??????????????????????????????????????? System.out.println(sCell);
??????????????????????????????????????? break;
??????????????????????????????????? case 1:// String
??????????????????????????????????????? String strCell2 = aCell.getStringCellValue();
??????????????????????????????????????? System.out.println(strCell2);
??????????????????????????????????????? break;
??????????????????????????????????? default:
??????????????????????????????????????? // System.out.println("格式不對不讀");//其它格式的數(shù)據(jù)
??????????????????????????????????????? String strCell3 = aCell.getStringCellValue();
??????????????????????????????????????? System.out.println(strCell3);
??????????????????????????????????? }
??????????????????????????????? }
??????????????????????????? }
??????????????????????? }
??????????????????? }
??????????????? }
??????????? }
??????? } catch (Exception e) {
??????????? System.out.println("ReadExcelError" + e);
??????? }
??? }
??? public static void main(String[] args) {
??????? HelloWorld poi = new HelloWorld();
??????? // poi.CreateExcel();
??????? poi.readExcel();
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/szadms/archive/2011/01/14/1935425.html
總結(jié)
以上是生活随笔為你收集整理的利用Apache POI读取并解析Excel的数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 招商GQ联名信用卡权益介绍 绅士的最佳选
- 下一篇: UFS 4.0时代降临!安卓手机天花板又