03版excel,需要用到jxl.jar这个jar包
package test.poi; import java.io.File; import java.io.IOException; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; //03版excel public class getExcel { public static void main(String[] args){ show03Excel(); } public static void show03Excel(){ Sheet sheet; Workbook book = null; try { book= Workbook.getWorkbook(new File("F:\\Book1.xls")); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // System.out.println(book); //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....) sheet=book.getSheet(0); int rows = 0; int r=0,l=0; String value=null; while(sheet.getCell(0,r).getContents()!=""&&sheet.getCell(0,r).getContents()!=null){ r++; } while(sheet.getCell(l,0).getContents()!=""&&sheet.getCell(l,0).getContents()!=null){ l++; } for(int i=0;i<(r);i++){ for(int j=0;j<(l);j++) System.out.print(sheet.getCell(j,i).getContents()+"\t\t"); System.out.println(); } } }
07版EXCEL,需要用到poi这个jar包
package test.poi; import java.io.IOException; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class get07Excel { public static void main(String[] arg){ show07excel(); } public static void show07excel(){ XSSFWorkbook xwb =null; try { xwb= new XSSFWorkbook("F:\\Book1.xlsx"); } catch (IOException e) { e.printStackTrace(); } XSSFSheet sheet=xwb.getSheetAt(0); XSSFRow row; String cell; int r=0,l=0; row = sheet.getRow(0); while(row.getCell(l).toString()!=""&&row.getCell(l).toString()!=null){ l++; } while(row.getCell(0).toString()!=""&&row.getCell(0).toString()!=null){ r++; row = sheet.getRow(r); } System.out.println("the rows is "+r+" and the number of one row is "+l); // 循环输出表格中的内容 for (int i = sheet.getFirstRowNum(); i < r; i++) { row = sheet.getRow(i); for (int j = row.getFirstCellNum(); j < l; j++) { cell = row.getCell(j).toString(); System.out.print(cell + "\t"); } System.out.println(""); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了