最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件的方法(poi,jxl)

 更新時(shí)間:2020年08月19日 10:37:08   作者:少年錦陽(yáng)  
這篇文章主要介紹了Java實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件的方法(poi,jxl),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

目前,比較常用的實(shí)現(xiàn)Java導(dǎo)入、導(dǎo)出Excel的技術(shù)有兩種Jakarta POI和Java Excel直接上代碼:

一,POI

POI是apache的項(xiàng)目,可對(duì)微軟的Word,Excel,Ppt進(jìn)行操作,包括office2003和2007,Excl2003和2007。poi現(xiàn)在一直有更新。所以現(xiàn)在主流使用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>

導(dǎo)出:

public class PoiCreateExcel {

 public static void main(String[] args) {
  // 創(chuàng)建表頭
  String[] title = {"id","name","sex"};
  //創(chuàng)建Excel工作薄
  HSSFWorkbook workbook = new HSSFWorkbook();
  //創(chuàng)建一個(gè)工作表sheet
  HSSFSheet sheet = workbook.createSheet();
  //創(chuàng)建第一行
  HSSFRow row = sheet.createRow(0);
  HSSFCell cell = null;
  // 插入第一行
  for (int i = 0; i < title.length; i++) {
   cell = row.createCell(i);
   cell.setCellValue(title[i]);
  }
  // 追加數(shù)據(jù)
  for (int i = 1; i < 10; i++) {// 這里的int 起始是1 也就是第二行開(kāi)始
   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("男");
  }
  // 創(chuàng)建一個(gè)文件
  File file = new File("d:/poi.xls");
  try {
   file.createNewFile();
   // 將內(nèi)容存盤
   FileOutputStream stream = FileUtils.openOutputStream(file);
   workbook.write(stream);

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

導(dǎo)入:

public class PoiReadExcel {

 public static void main(String[] args) {

  // 引入需要解析的文件
  File file = new File("d:/poi.xls");
  try {
   // 創(chuàng)建Excel 讀取文件內(nèi)容
   HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
   /**
    * 第一種方式讀取Sheet頁(yè)
    */
//   HSSFSheet sheet = workbook.getSheet("Sheet0");
   /**
    * 第二種方式讀取Sheet頁(yè)
    */
   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);
    // 獲取當(dāng)前最后單元格列號(hào)
    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>

導(dǎo)出:

public class PoiCreateExcel {

 public static void main(String[] args) {
  // 創(chuàng)建表頭
  String[] title = {"id","name","sex"};
  //創(chuàng)建Excel工作薄
  XSSFWorkbook workbook = new XSSFWorkbook();
  //創(chuàng)建一個(gè)工作表shheet
  Sheet sheet = workbook.createSheet();
  //創(chuàng)建第一行
  Row row = sheet.createRow(0);
  Cell cell = null;
  // 插入第一行
  for (int i = 0; i < title.length; i++) {
   cell = row.createCell(i);
   cell.setCellValue(title[i]);
  }
  // 追加數(shù)據(jù)
  for (int i = 1; i < 10; i++) {// 這里的int 起始是1 也就是第二行開(kāi)始
   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("男");
  }
  // 創(chuàng)建一個(gè)文件
  File file = new File("d:/poi.xlsx");// 這里可以修改成高版本的
  try {
   file.createNewFile();
   // 將內(nèi)容存盤
   FileOutputStream stream = FileUtils.openOutputStream(file);
   workbook.write(stream);

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

導(dǎo)入:

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){
     //獲得當(dāng)前行的開(kāi)始列 
     int firstCellNum = row.getFirstCellNum();
     //獲得當(dāng)前行的列數(shù) 
     int lastCellNum = row.getPhysicalNumberOfCells();
     String result = "";
     //循環(huán)當(dāng)前行 
     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只能對(duì)Excel進(jìn)行操作,屬于比較老的框架,它只支持到Excel 95-2000的版本。現(xiàn)在已經(jīng)停止更新和維護(hù)。

pom:

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

導(dǎo)出:

public class JxlCreateExcel {

 public static void main(String[] args) {
  // 首先設(shè)置表格第一行 表格頭名稱 也就是列名
  String [] title = {"id","name","sex"};
  // 創(chuàng)建Excel文件 存入路徑
  File file = new File("d:/jxl.xls");
  try {
   file.createNewFile();
   // 創(chuàng)建工作薄
   WritableWorkbook workbook = Workbook.createWorkbook(file);
   // 創(chuàng)建sheet
   WritableSheet sheet = workbook.createSheet("sheet1",0);
   // 添加數(shù)據(jù)
   Label label = null;
   // 第一行設(shè)置列名
   for (int i = 0; i < title.length; i++) {
    label = new Label(i,0,title[i]);
    sheet.addCell(label);
   }
   // 追加數(shù)據(jù) 從第二行開(kāi)始 i從1開(kāi)始
   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);
   }
   // 寫入 并在最后關(guān)閉流
   workbook.write();
   workbook.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

導(dǎo)入:

public class JxlReadExcel {

 public static void main(String[] args) {
  try {
   // 創(chuàng)建 Workbook
   Workbook workbook = Workbook.getWorkbook(new File("d:/jxl.xls"));
   // 獲取工作表sheet
   Sheet sheet = workbook.getSheet(0);
   // 獲取數(shù)據(jù)
   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() + " ");// 得到單元格的內(nèi)容
    }
    System.out.println();
   }
   workbook.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

到此,代碼可直接部署運(yùn)行,希望可以幫助到你~

總結(jié)

到此這篇關(guān)于Java實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件的方法(poi,jxl)的文章就介紹到這了,更多相關(guān)java實(shí)現(xiàn)導(dǎo)入導(dǎo)出excel文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 虛擬機(jī)深入了解

    java 虛擬機(jī)深入了解

    這篇文章主要介紹了java 虛擬機(jī)深入了解的相關(guān)資料,ava虛擬機(jī)有自己完善的硬體架構(gòu),如處理器、堆棧、寄存器等,還具有相應(yīng)的指令系統(tǒng),需要的朋友可以參考下
    2017-03-03
  • Spring Data JPA結(jié)合Mybatis進(jìn)行分頁(yè)查詢的實(shí)現(xiàn)

    Spring Data JPA結(jié)合Mybatis進(jìn)行分頁(yè)查詢的實(shí)現(xiàn)

    本文主要介紹了Spring Data JPA結(jié)合Mybatis進(jìn)行分頁(yè)查詢的實(shí)現(xiàn)
    2024-03-03
  • Springmvc應(yīng)用Mongodb分頁(yè)實(shí)現(xiàn)

    Springmvc應(yīng)用Mongodb分頁(yè)實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Springmvc應(yīng)用Mongodb分頁(yè)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Spring自定義注解的實(shí)現(xiàn)與使用方式

    Spring自定義注解的實(shí)現(xiàn)與使用方式

    注解是Java中用于類、方法、參數(shù)、包的裝飾標(biāo)志,本身不具備功能,但可定義參數(shù),Java包含內(nèi)建注解和元注解,如@Target、@Retention等,描述注解的使用范圍和生命周期,Spring的AOP(面向切面編程)可以結(jié)合注解實(shí)現(xiàn)功能,如權(quán)限控制和日志記錄
    2024-09-09
  • Java線程三種命名方法詳解

    Java線程三種命名方法詳解

    這篇文章主要介紹了Java線程三種命名方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • vue 使用vuex在頁(yè)面跳轉(zhuǎn)的實(shí)現(xiàn)方式

    vue 使用vuex在頁(yè)面跳轉(zhuǎn)的實(shí)現(xiàn)方式

    這篇文章主要介紹了vue 使用vuex在頁(yè)面跳轉(zhuǎn)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java NIO和IO的區(qū)別

    Java NIO和IO的區(qū)別

    這篇文章主要介紹了Java NIO和IO的區(qū)別,需要的朋友可以參考下
    2014-06-06
  • java程序員如何編寫更好的單元測(cè)試的7個(gè)技巧

    java程序員如何編寫更好的單元測(cè)試的7個(gè)技巧

    測(cè)試是開(kāi)發(fā)的一個(gè)非常重要的方面,可以在很大程度上決定一個(gè)應(yīng)用程序的命運(yùn)。良好的測(cè)試可以在早期捕獲導(dǎo)致應(yīng)用程序崩潰的問(wèn)題,但較差的測(cè)試往往總是導(dǎo)致故障和停機(jī)。本文主要介紹java程序員編寫更好的單元測(cè)試的7個(gè)技巧。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • idea導(dǎo)入項(xiàng)目框架的詳細(xì)操作方法

    idea導(dǎo)入項(xiàng)目框架的詳細(xì)操作方法

    大家使用idea開(kāi)發(fā)工具時(shí)經(jīng)常會(huì)需要導(dǎo)入項(xiàng)目框架,糾結(jié)該怎么操作呢,今天小編給大家分享一篇圖文教程,幫助大家解決idea導(dǎo)入項(xiàng)目框架的問(wèn)題,感興趣的朋友一起看看吧
    2021-05-05
  • Java中的異步回調(diào)問(wèn)題

    Java中的異步回調(diào)問(wèn)題

    這篇文章主要介紹了Java中的異步回調(diào)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評(píng)論

大理市| 三门峡市| 望都县| 白山市| 临夏县| 忻城县| 壤塘县| 莱西市| 蒙阴县| 宁化县| 镇沅| 黄平县| 六枝特区| 郧西县| 清镇市| 澄城县| 贵州省| 深水埗区| 昭苏县| 石泉县| 海城市| 南京市| 益阳市| 怀仁县| 舒兰市| 长宁县| 夹江县| 怀化市| 法库县| 泸溪县| 洛扎县| 通河县| 荃湾区| 宜黄县| 光山县| 包头市| 巴南区| 潍坊市| 平邑县| 冷水江市| 海丰县|