Java Excel 导入导出
Posted betterLearing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Excel 导入导出相关的知识,希望对你有一定的参考价值。
本文主要描述通过java实现Excel导入导出
一、读写Excel三种常用方式
1.JXL——Java Excel开放源码项目:读取,创建,更新
2.POI——Apache POI ,提供API给Java程序对Microsoft Office格式文档案读和写的功能——使用其HSSF(Horrible SpreadSheet Format, 格式支持97-2003,高版本请使用:XSSF)
3.FASTEXCEL——采用纯java开发的excel文件读写组件,支持Excel97-2003,功能小,一般不用。
二、JXL和POI对比
POI:效率高,操作相对复杂,支持公式、宏、图像图表,一些企业应用上会非常实用,能够修饰单元格属性,支持字体、数字日期操作
JXL:效率低,操作简单,能够修饰单元格属性,格式支持不如POI强大,支持字体、数字、日期操作
三、JXL创建Excel,解析Excel
Eclipse-java Project-properties-java Build Path-Libraries-add Externa JARS-选择jxl.jar
1.创建Excel,添加数据
1 import java.io.File; 2 3 import jxl.Workbook; 4 import jxl.write.Label; 5 import jxl.write.WritableSheet; 6 import jxl.write.WritableWorkbook; 7 8 public class JxlExpExcel { 9 10 public static void main(String[] args) { 11 String[] title = { "id", "name", "sex" }; 12 // 创建Excel文件 13 File file = new File("e:/jxl_test.xls"); 14 try { 15 file.createNewFile(); 16 // 创建工作薄 17 WritableWorkbook workbook = Workbook.createWorkbook(file); 18 // 创建Sheet 19 WritableSheet sheet = workbook.createSheet("sheet", 0); 20 Label label = null; 21 // 第一行设置列名 22 for (int i = 0; i < title.length; i++) { 23 label = new Label(i, 0, title[i]); 24 sheet.addCell(label); 25 } 26 // 追加数据 27 for (int i = 1; i < 10; i++) { 28 label = new Label(0, i, "" + i); 29 sheet.addCell(label); 30 label = new Label(1, i, "user" + i); 31 sheet.addCell(label); 32 label = new Label(2, i, "男"); 33 sheet.addCell(label); 34 } 35 // 写入数据 36 workbook.write(); 37 // 关闭流 38 workbook.close(); 39 } catch (Exception e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 45 }
2.读取Excel中数据,打印在控制台
1 import java.io.File; 2 3 import jxl.Cell; 4 import jxl.Sheet; 5 import jxl.Workbook; 6 7 public class JxlReadExcel { 8 public static void main(String[] args) { 9 File file =new File("e:/jxl_test.xls"); 10 try { 11 //创建Workbook 12 Workbook workbook =Workbook.getWorkbook(file); 13 //创建工作表sheet 14 Sheet sheet=workbook.getSheet(0); 15 //读取数据,先行后列 16 for (int i = 0; i < sheet.getRows(); i++) { 17 for (int j = 0; j < sheet.getColumns(); j++) { 18 Cell cell=sheet.getCell(j, i); 19 System.out.print(cell.getContents()+" "); 20 } 21 System.out.println(); 22 } 23 //关闭流 24 workbook.close(); 25 } catch (Exception e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } 29 } 30 }
四、POI创建、解析EXCEL
Eclipse-java Project-properties-java Build Path-Libraries-add Externa JARS-选择poi.jar和commons-io.jar
1.POI创建Excel,添加数据
1 import java.io.File; 2 import java.io.FileOutputStream; 3 4 import org.apache.commons.io.FileUtils; 5 import org.apache.poi.hssf.usermodel.HSSFCell; 6 import org.apache.poi.hssf.usermodel.HSSFRow; 7 import org.apache.poi.hssf.usermodel.HSSFSheet; 8 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 9 10 public class ExcelPOI { 11 public static void main(String[] args) { 12 String[] title = { "id", "name", "sex" }; 13 // 创建EXCEl工作薄 14 HSSFWorkbook workbook = new HSSFWorkbook(); 15 // 创建一个工作表sheet 16 HSSFSheet sheet = workbook.createSheet(); 17 // 创建第一行 18 HSSFRow row = sheet.createRow(0); 19 // 插入第一行数据,id、name、sex 20 for (int i = 0; i < title.length; i++) { 21 HSSFCell cell = row.createCell(i); 22 cell.setCellValue(title[i]); 23 } 24 // 追加数据 25 for (int i = 1; i <= 10; i++) { 26 HSSFRow nextRow = sheet.createRow(i); 27 28 HSSFCell cell2 = nextRow.createCell(0); 29 cell2.setCellValue("a" + i); 30 31 cell2 = nextRow.createCell(1); 32 cell2.setCellValue("name" + i); 33 34 cell2 = nextRow.createCell(2); 35 cell2.setCellValue("男" + i); 36 } 37 // 创建一个文件 38 File file = new File("e:/poi_test.xls"); 39 // 40 try { 41 file.createNewFile(); 42 // 将EXCEL存盘 43 FileOutputStream stream = FileUtils.openOutputStream(file); 44 workbook.write(stream); 45 // 关闭流 46 stream.close(); 47 } catch (Exception e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 } 52 }
2.POI解析Excel
1 import java.io.File; 2 import java.io.IOException; 3 4 import org.apache.commons.io.FileUtils; 5 import org.apache.poi.hssf.usermodel.HSSFCell; 6 import org.apache.poi.hssf.usermodel.HSSFRow; 7 import org.apache.poi.hssf.usermodel.HSSFSheet; 8 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 9 10 public class POIReadExcel { 11 12 public static void main(String[] args) { 13 File file = new File("e:/poi_test.xls"); 14 try { 15 HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file)); 16 // 获取第一个工作表workbook.getSheet("Sheet0") 17 // HSSFSheet sheet =workbook.getSheet("Sheet0"); 18 // 获取默认第一个工作表 19 HSSFSheet sheet = workbook.getSheetAt(0); 20 int firstRowNum = 0; 21 // 获取最后一行行号 22 int lastRowNum = sheet.getLastRowNum(); 23 for (int i = firstRowNum; i <= lastRowNum; i++) { 24 HSSFRow row = sheet.getRow(i); 25 // 获取当前最后单元格列号 26 int lastCellNum = row.getLastCellNum(); 27 for (int j = 0; j < lastCellNum; j++) { 28 HSSFCell cell = row.getCell(j); 29 String value = cell.getStringCellValue(); 30 System.out.print(value + " "); 31 } 32 System.out.println(); 33 } 34 } catch (IOException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 40 }
3.POI创建高版本EXCEL(把HFFS替换成XFFS,同时引入相应的jar)
1 import java.io.File; 2 import java.io.FileOutputStream; 3 4 import org.apache.commons.io.FileUtils; 5 import org.apache.poi.xssf.usermodel.XSSFCell; 6 import org.apache.poi.xssf.usermodel.XSSFRow; 7 import org.apache.poi.xssf.usermodel.XSSFSheet; 8 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 9 10 public class ExcelPOIXSSF { 11 public static void main(String[] args) { 12 String[] title = { "id", "name", "sex" }; 13 // 创建EXCEl工作薄 14 XSSFWorkbook workbook = new XSSFWorkbook(); 15 // 创建一个工作表sheet 16 XSSFSheet sheet = workbook.createSheet(); 17 // 创建第一行 18 XSSFRow row = sheet.createRow(0); 19 // 插入第一行数据,id、name、sex 20 for (int i = 0; i < title.length; i++) { 21 XSSFCell cell = row.createCell(i); 22 cell.setCellValue(title[i]); 23 } 24 // 追加数据 25 for (int i = 1; i <= 10; i++) { 26 XSSFRow nextRow = sheet.createRow(i); 27 28 XSSFCell cell2 = nextRow.createCell(0); 29 cell2.setCellValue("a" + i); 30 31 cell2 = nextRow.createCell(1); 32 cell2.setCellValue("name" + i); 33 34 cell2 = nextRow.createCell(2); 35 cell2.setCellValue("男" + i); 36 } 37 // 创建一个文件 38 File file = new File("e:/poi_test.xlsx"); 39 // 40 try { 41 file.createNewFile(); 42 // 将EXCEL存盘 43 FileOutputStream stream = FileUtils.openOutputStream(file); 44 workbook.write(stream); 45 // 关闭流 46 stream.close(); 47 } catch (Exception e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 } 52 }
以上是关于Java Excel 导入导出的主要内容,如果未能解决你的问题,请参考以下文章