注意,本文使用的Jar包爲jxl.jar
,只能處理97-03版本xls格式的Excelapi
maven依賴:maven
<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.10</version> </dependency>
jar包maven下載地址excel
//經過文件IO,讀入一個xls文件 Workbook book = Workbook.getWorkbook(new File(path)); //得到第一個工做表對象(ecxel中sheet的編號從0開始,0,1,2,3,....) Sheet sheet = book.getSheet(0); //遍歷每一行內容 for (int i = 0; i <sheet.rows; i++) { //getCell得到單元格,參數爲(列,行)的下標 System.out.println(sheet.getCell(0, i).getContents()); }
File xlsFile = new File("jxl.xls"); // 建立一個工做簿 WritableWorkbook workbook = Workbook.createWorkbook(xlsFile); // 建立一個工做表 WritableSheet sheet = workbook.createSheet("sheet1", 0); //遍歷每一行,寫出數據 for (int row = 0; row < 10; row++){ for (int col = 0; col < 10; col++){ // 向工做表中添加數據 sheet.addCell(new Label(col, row, "data" + row + col)); } } workbook.write(); workbook.close();