首页 > 编程语言 > Java实现导入导出Excel文件的方法(poi,jxl)
2020
10-09

Java实现导入导出Excel文件的方法(poi,jxl)

目前,比较常用的实现Java导入、导出Excel的技术有两种Jakarta POI和Java Excel直接上代码:

一,POI

POI是apache的项目,可对微软的Word,Excel,Ppt进行操作,包括office2003和2007,Excl2003和2007。poi现在一直有更新。所以现在主流使用POI。

xls:

pom:

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>3.9</version>
</dependency>
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.2</version>
</dependency>

导出:

public class PoiCreateExcel {

 public static void main(String[] args) {
  // 创建表头
  String[] title = {"id","name","sex"};
  //创建Excel工作薄
  HSSFWorkbook workbook = new HSSFWorkbook();
  //创建一个工作表sheet
  HSSFSheet sheet = workbook.createSheet();
  //创建第一行
  HSSFRow row = sheet.createRow(0);
  HSSFCell cell = null;
  // 插入第一行
  for (int i = 0; i < title.length; i++) {
   cell = row.createCell(i);
   cell.setCellValue(title[i]);
  }
  // 追加数据
  for (int i = 1; i < 10; i++) {// 这里的int 起始是1 也就是第二行开始
   HSSFRow nexTrow = sheet.createRow(i);
   HSSFCell cell2 = nexTrow.createCell(0);
   cell2.setCellValue("a"+i);
   cell2 = nexTrow.createCell(1);
   cell2.setCellValue("user");
   cell2 = nexTrow.createCell(2);
   cell2.setCellValue("男");
  }
  // 创建一个文件
  File file = new File("d:/poi.xls");
  try {
   file.createNewFile();
   // 将内容存盘
   FileOutputStream stream = FileUtils.openOutputStream(file);
   workbook.write(stream);

   stream.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

导入:

public class PoiReadExcel {

 public static void main(String[] args) {

  // 引入需要解析的文件
  File file = new File("d:/poi.xls");
  try {
   // 创建Excel 读取文件内容
   HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
   /**
    * 第一种方式读取Sheet页
    */
//   HSSFSheet sheet = workbook.getSheet("Sheet0");
   /**
    * 第二种方式读取Sheet页
    */
   HSSFSheet sheet = workbook.getSheetAt(0);
   int firstRowNum = 0;// 起始行第0行
   int lasrRowNum = sheet.getLastRowNum();// 一直读到最后一行
   for (int i = 0; i < lasrRowNum; i++) {
    HSSFRow row = sheet.getRow(i);
    // 获取当前最后单元格列号
    int lastCellNum = row.getLastCellNum();
    for (int j = 0; j < lastCellNum; j++) {
     HSSFCell cell = row.getCell(j);
     String value = cell.getStringCellValue();// 注意! 如果Excel 里面的值是String 那么getStringCellValue 如果是其他类型 则需要修改
     System.out.print(value + " ");
    }
    System.out.println();
   }

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

xlsx:

pom:

<!-- poi高版本额外包 -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-examples</artifactId>
	<version>3.9</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-excelant</artifactId>
	<version>3.9</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
 	<artifactId>poi-ooxml</artifactId>
	<version>3.9</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml-schemas</artifactId>
	<version>3.9</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-scratchpad</artifactId>
 	<version>3.9</version>
</dependency>

导出:

public class PoiCreateExcel {

 public static void main(String[] args) {
  // 创建表头
  String[] title = {"id","name","sex"};
  //创建Excel工作薄
  XSSFWorkbook workbook = new XSSFWorkbook();
  //创建一个工作表shheet
  Sheet sheet = workbook.createSheet();
  //创建第一行
  Row row = sheet.createRow(0);
  Cell cell = null;
  // 插入第一行
  for (int i = 0; i < title.length; i++) {
   cell = row.createCell(i);
   cell.setCellValue(title[i]);
  }
  // 追加数据
  for (int i = 1; i < 10; i++) {// 这里的int 起始是1 也就是第二行开始
   Row nexTrow = sheet.createRow(i);
   Cell cell2 = nexTrow.createCell(0);
   cell2.setCellValue("a"+i);
   cell2 = nexTrow.createCell(1);
   cell2.setCellValue("user");
   cell2 = nexTrow.createCell(2);
   cell2.setCellValue("男");
  }
  // 创建一个文件
  File file = new File("d:/poi.xlsx");// 这里可以修改成高版本的
  try {
   file.createNewFile();
   // 将内容存盘
   FileOutputStream stream = FileUtils.openOutputStream(file);
   workbook.write(stream);

   stream.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

导入:

public class PoiReadExcel {
 public List<Double> readExcels(InputStream is)throws Exception{
  List<Double> xlsxList = new ArrayList<Double>();
  try {
   if(is ==null){
    throw new IOException("文件不正确!");
   }
   Workbook workbook = WorkbookFactory.create(is);
   FormulaEvaluator fe = workbook.getCreationHelper().createFormulaEvaluator();
   //获取第一张表
   Sheet sheet = workbook.getSheetAt(0);
   if(sheet == null){
    throw new IOException("传入的excel的第一张表为空!");
   }
   for(int rowNum = 0;rowNum <= sheet.getLastRowNum(); rowNum++){
    Row row = sheet.getRow(rowNum);
    if(row != null){
     //获得当前行的开始列 
     int firstCellNum = row.getFirstCellNum();
     //获得当前行的列数 
     int lastCellNum = row.getPhysicalNumberOfCells();
     String result = "";
     //循环当前行 
     for(int cellNum = firstCellNum; cellNum < lastCellNum;cellNum++){
      Cell cell = row.getCell(cellNum);
      double value = 0;
      String valueString = cell.getStringCellValue();
      if(null!=fe.evaluate(cell)){
       value = fe.evaluate(cell).getNumberValue();
      }
      //result = result + cellNum + ":"+value + "----";
      result = result + cellNum + ":"+valueString + "----";
     }
     System.out.println(result + " ");
    }
   }
   is.close();
  } catch (FileNotFoundException e) {
  throw new Exception("文件不正确!");
 }
  return xlsxList;
 }

 public static void main(String[] args) throws Exception {
  InputStream is = new FileInputStream("d:/poi.xlsx");
  PoiReadExcel re = new PoiReadExcel();
  re.readExcels(is);
 }
}

二,JXL

JXL只能对Excel进行操作,属于比较老的框架,它只支持到Excel 95-2000的版本。现在已经停止更新和维护。

pom:

<!-- jxl -->
<dependency>
	<groupId>net.sourceforge.jexcelapi</groupId>
	<artifactId>jxl</artifactId>
	<version>2.6.10</version>
</dependency>

导出:

public class JxlCreateExcel {

 public static void main(String[] args) {
  // 首先设置表格第一行 表格头名称 也就是列名
  String [] title = {"id","name","sex"};
  // 创建Excel文件 存入路径
  File file = new File("d:/jxl.xls");
  try {
   file.createNewFile();
   // 创建工作薄
   WritableWorkbook workbook = Workbook.createWorkbook(file);
   // 创建sheet
   WritableSheet sheet = workbook.createSheet("sheet1",0);
   // 添加数据
   Label label = null;
   // 第一行设置列名
   for (int i = 0; i < title.length; i++) {
    label = new Label(i,0,title[i]);
    sheet.addCell(label);
   }
   // 追加数据 从第二行开始 i从1开始
   for (int i = 1; i < 9; i++) {
    label = new Label(0,i,"id:"+i);
    sheet.addCell(label);
    label = new Label(1,i,"user");
    sheet.addCell(label);
    label = new Label(2,i,"男");
    sheet.addCell(label);
   }
   // 写入 并在最后关闭流
   workbook.write();
   workbook.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

导入:

public class JxlReadExcel {

 public static void main(String[] args) {
  try {
   // 创建 Workbook
   Workbook workbook = Workbook.getWorkbook(new File("d:/jxl.xls"));
   // 获取工作表sheet
   Sheet sheet = workbook.getSheet(0);
   // 获取数据
   for (int i = 0; i < sheet.getRows(); i++) {// 获取行
    for (int j = 0; j < sheet.getColumns(); j++) {// 获取列
     Cell cell = sheet.getCell(j,i);
     System.out.print(cell.getContents() + " ");// 得到单元格的内容
    }
    System.out.println();
   }
   workbook.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

到此,代码可直接部署运行,希望可以帮助到你~

总结

到此这篇关于Java实现导入导出Excel文件的方法(poi,jxl)的文章就介绍到这了,更多相关java实现导入导出excel文件内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧