这是一个URL它提供了Java项目所推荐的处理此项目所用的类库
http://www.oschina.net/project/tag/258/excel-tools?company=0&sort=score&tag=258&lang=19&recommend=false
Java用Apache的POI库对Excel进行操作;
其他Excel开发库: http://www.oschina.net/project/tag/258/excel-tools
Apache POI 下载地址: http://poi.apache.org/download.html
转载链接:http://www.cnblogs.com/wunaozai/p/4179270.html
Excel 2003 (xls后缀)
读:
1 import java.io.FileInputStream; 2 3 import org.apache.poi.hssf.usermodel.HSSFRow; 4 import org.apache.poi.hssf.usermodel.HSSFSheet; 5 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 6 7 public class ReadExcel2003 { 8 public static void main(String[] args) { 9 try { 10 FileInputStream file = new FileInputStream("C:\dd.xls"); 11 12 HSSFWorkbook workbook = new HSSFWorkbook(file); 13 HSSFSheet sheet = workbook.getSheetAt(0); 14 HSSFRow row = null; 15 16 for(int i=0;sheet.getRow(i)!=null;i++){ 17 row = sheet.getRow(i); 18 for(int j=0;row.getCell(j)!=null;j++){ 19 System.out.print(row.getCell(j)+""); 20 } 21 System.out.println(); 22 } 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 }
写:
1 import java.io.FileOutputStream; 2 3 import org.apache.poi.hssf.usermodel.HSSFCell; 4 import org.apache.poi.hssf.usermodel.HSSFRow; 5 import org.apache.poi.hssf.usermodel.HSSFSheet; 6 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 7 8 public class WriteExcel2003 { 9 public static void main(String[] args) { 10 try { 11 HSSFWorkbook workbook = new HSSFWorkbook(); 12 HSSFSheet sheet = workbook.createSheet("test"); //页签 13 HSSFRow row = sheet.createRow(1); 14 HSSFCell cell = row.createCell(2); 15 cell.setCellValue("test"); 16 for (int i=0;i<100;i++){ 17 row = sheet.createRow(i); 18 for(int j=0;j<100;j++){ 19 cell = row.createCell(j); 20 cell.setCellValue("("+i+","+j+")"); 21 } 22 } 23 FileOutputStream os = null; 24 os = new FileOutputStream("C:\dd.xls"); 25 workbook.write(os); 26 os.flush(); 27 os.close(); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 System.out.println("Ok"); 32 } 33 }
Excel 2007 (xlsx 后缀)
读:
1 import java.io.FileInputStream; 2 3 import org.apache.poi.xssf.usermodel.XSSFRow; 4 import org.apache.poi.xssf.usermodel.XSSFSheet; 5 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 6 7 public class ReadExcel2007 { 8 public static void main(String[] args) { 9 try { 10 FileInputStream file = new FileInputStream("C:\bb.xlsx"); 11 XSSFWorkbook workbook = new XSSFWorkbook(file); 12 XSSFSheet sheet = workbook.getSheetAt(0); 13 14 XSSFRow row = null; 15 16 for(int i=0;sheet.getRow(i)!=null;i++){ 17 row = sheet.getRow(i); 18 for(int j=0;row.getCell(j)!=null;j++){ 19 System.out.print(row.getCell(j)+" "); 20 } 21 System.out.println(); 22 } 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 }
写:
1 import java.io.FileOutputStream; 2 3 import org.apache.poi.xssf.usermodel.XSSFCell; 4 import org.apache.poi.xssf.usermodel.XSSFRow; 5 import org.apache.poi.xssf.usermodel.XSSFSheet; 6 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 7 8 public class WriteExcel2007 { 9 public static void main(String[] args) { 10 try { 11 XSSFWorkbook workbook = new XSSFWorkbook(); 12 XSSFSheet sheet = workbook.createSheet("test"); //页签 13 XSSFRow row = sheet.createRow(1); 14 XSSFCell cell = row.createCell(2); 15 cell.setCellValue("test"); 16 for (int i=0;i<100;i++){ 17 row = sheet.createRow(i); 18 for(int j=0;j<100;j++){ 19 cell = row.createCell(j); 20 cell.setCellValue("("+i+","+j+")"); 21 } 22 } 23 FileOutputStream os = null; 24 os = new FileOutputStream("C:\dd.xlsx"); 25 workbook.write(os); 26 os.flush(); 27 os.close(); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 System.out.println("Ok"); 32 } 33 }