这次的任务是实现数据的导入/导出功能,就是用Java操作Excel,上网找了一下,就Java来说用Apache的POI库的比较多,功能也相对比较强大。其他Excel开发库: http://www.oschina.net/project/tag/258/excel-tools Apache POI 下载地址: http://poi.apache.org/download.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 }
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 }
Excel 2003 (xls 后缀)
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.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 }
这里要说一下Excel的xls和xlsx两种后缀里面的数据存储方式区别太大,所以导致编写代码的时候要使用两个完全不同的库。这个只要把后缀改成zip,然后看压缩包里面有什么数据就大概知道为什么不一样了。xlsx是使用xml来保存的,这种格式应该是比较好的描述数据吧,所以才会使用完成不一样的格式来升级。
从官网上下载POI包,里面有API,更多的操作可以查看API。
下面介绍几个常用的API
1 HSSFSheet sheet = workbook.createSheet("test"); //页签 2 HSSFRow row = sheet.createRow(1); 3 HSSFCell cell = row.createCell(1); 4 cell.setCellValue("test"); 5 cell = row.createCell(3); 6 cell.setCellFormula("B2"); //使用公式 7 sheet.setColumnWidth(2, 300); //设置列宽 8 row.setHeight((short) 1000); //设置行高
效果
字体属性
1 HSSFRow row = sheet.createRow(1); 2 HSSFCell cell = row.createCell(1); 3 cell.setCellValue("TEST"); //设置文字 4 HSSFFont font = workbook.createFont(); 5 font.setFontHeightInPoints((short)13); //设置字体大小 6 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体加粗 7 font.setFontName("黑体"); //设置字体为黑体 8 HSSFColor color=new HSSFColor.BLUE(); 9 font.setColor(color.getIndex()); //设置设置颜色 10 HSSFCellStyle style = workbook.createCellStyle(); 11 style.setFont(font); //封装成一个样式 12 cell.setCellStyle(style); //设置样式
效果
其实常用的也就是对文字的导入和导出功能,其他的样式描述功能,没有什么大的用处,实际用的比较少。
参考资料: http://my.oschina.net/zimingforever/blog/101142
http://www.cnblogs.com/hongten/archive/2012/02/22/java2poi.html
http://my.oschina.net/baochanghong/blog/351592
本文地址: http://www.cnblogs.com/wunaozai/p/4179270.html