package com.amazon.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;/** * * @author shiwt * * java读取excel文件 * * 一个excel文件的层次:excel文件->工作表->行->单元格对应到poi中,为:workbook->sheet->row->cell * */public class excelreader { 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) { excelreader poi = new excelreader(); // poi.createexcel(); poi.readexcel(); }}
POI 读excel
最新推荐文章于 2025-08-24 15:22:07 发布