温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java如何导入导出Excel文件

发布时间:2020-08-20 09:44:43 来源:亿速云 阅读:150 作者:小新 栏目:开发技术

小编给大家分享一下Java如何导入导出Excel文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

目前,比较常用的实现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文件的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI