乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      POI和easyExcel

       印度阿三17 2020-06-10

      ?1.常用場(chǎng)景

      (1)將用戶數(shù)據(jù)導(dǎo)出為excel表格(導(dǎo)出數(shù)據(jù))

      (2)將Excel表中的數(shù)據(jù)導(dǎo)出到網(wǎng)站數(shù)據(jù)庫(kù)中

      操作Excel最常用的就是Apache的POI和阿里巴巴的easyExcel。

      2.官網(wǎng)地址

      Apache POI的官網(wǎng):https://poi./

      easyExcel的官網(wǎng):https://github.com/alibaba/easyexcel

      3.poi和easyExecel的區(qū)別:

      以下是我在官網(wǎng)截的圖:

      ?

      ?

      ?

      ?4.Excel中的對(duì)象

      ? ? 在java中萬(wàn)物皆對(duì)象,Excel也不例外,Excel中的對(duì)象有:

      ? ? 工作簿:Workbook(是一個(gè)接口)

      ? ? (使用時(shí)需要導(dǎo)入Workbook:import org.apache.poi.ss.usermodel.Workbook;)

      ? ?Workbook的三個(gè)實(shí)現(xiàn)類:

      (1)HSSFWorkbook:HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,擴(kuò)展名是.xls;
      (2)XSSFWorkbook:是操作Excel2007的版本,擴(kuò)展名是.xlsx;
      (3)SXSSFworkbook:是XSSFWorkbook的升級(jí)版,當(dāng)數(shù)據(jù)量超出65536條后,在使用HSSFWorkbook或XSSFWorkbook,程序會(huì)報(bào)OutOfMemoryError:Javaheap space;內(nèi)存溢出錯(cuò)誤。這時(shí)應(yīng)該用SXSSFworkbook。
       工作表:Sheet (使用時(shí)需要導(dǎo)入Sheet:import org.apache.poi.ss.usermodel.Sheet;)

      ? ? 行:Row? ? ?(使用時(shí)需要導(dǎo)入Row:import org.apache.poi.ss.usermodel.Row;)

      ? ? 列:Cell??(使用時(shí)需要導(dǎo)入Cell??:import org.apache.poi.ss.usermodel.Cell;)

      ?5.03版Excel表的生成代碼:

      package com.lqz.controller;
      
      import org.apache.poi.hssf.usermodel.HSSFWorkbook;
      import org.apache.poi.ss.usermodel.Cell;
      import org.apache.poi.ss.usermodel.Row;
      import org.apache.poi.ss.usermodel.Sheet;
      import org.apache.poi.ss.usermodel.Workbook;
      import org.junit.Test;
      
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      /**
       * @Author:liqinzhen
       * @Date:2020/6/9
       * @Description:
       */
      public class ExcelWriteTest {
          String  path = "E:\\java\\project\\springmvc-ajax-json";
          @Test
          public void test() throws IOException {
              //1.創(chuàng)建一個(gè)工作簿
              Workbook workbook = new HSSFWorkbook();
              //2.創(chuàng)建一個(gè)工作表,因?yàn)楣ぷ鞅碓诠ぷ鞑局?,所以用工作簿?chuàng)建工作表
              Sheet sheet = workbook.createSheet("學(xué)生表");
              //3.創(chuàng)建一個(gè) 行
              Row row1 = sheet.createRow(0);
              //4.創(chuàng)建一個(gè)單元格
              Cell cell11 = row1.createCell(0);
              //給單元格設(shè)置值
              cell11.setCellValue("陳咬金");;
              //創(chuàng)建第二個(gè)單元格
              Cell cell12 = row1.createCell(1);
              cell12.setCellValue("1021");
              //創(chuàng)建第二行
              Row row2 = sheet.createRow(1);
              //創(chuàng)建第二行的第一個(gè)單元格
              Cell cell21 = row2.createCell(0);
              cell21.setCellValue("羋月");
              //創(chuàng)建第二行的第二個(gè)單元格
              Cell cell22 = row2.createCell(1);
              cell22.setCellValue("1023");
              //生成一張表(io流),03版的Excel使用.xls結(jié)尾
              FileOutputStream fileOutputStream = new FileOutputStream(path "學(xué)生表03.xls");
              //輸出
              workbook.write(fileOutputStream);
              //關(guān)閉流
              fileOutputStream.close();
              System.out.println("學(xué)生表03.xls表生成完畢");
          }
      
      }

      6.07版的Excel生成代碼:

      //(1)導(dǎo)入少量數(shù)據(jù)
      package com.lqz.controller; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.junit.Test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * @Author:liqinzhen * @Date:2020/6/9 * @Description: */ public class ExcelWriteTest { String path = "E:\\java\\project\\springmvc-ajax-json"; @Test public void test() throws IOException { //1.創(chuàng)建一個(gè)工作簿 Workbook workbook = new XSSFWorkbook(); //2.創(chuàng)建一個(gè)工作表,因?yàn)楣ぷ鞅碓诠ぷ鞑局?,所以用工作簿?chuàng)建工作表 Sheet sheet = workbook.createSheet("學(xué)生表"); //3.創(chuàng)建一個(gè) 行 Row row1 = sheet.createRow(0); //4.創(chuàng)建一個(gè)單元格 Cell cell11 = row1.createCell(0); //給單元格設(shè)置值 cell11.setCellValue("陳咬金");; //創(chuàng)建第二個(gè)單元格 Cell cell12 = row1.createCell(1); cell12.setCellValue("1021"); //創(chuàng)建第二行 Row row2 = sheet.createRow(1); //創(chuàng)建第二行的第一個(gè)單元格 Cell cell21 = row2.createCell(0); cell21.setCellValue("羋月"); //創(chuàng)建第二行的第二個(gè)單元格 Cell cell22 = row2.createCell(1); cell22.setCellValue("1023"); //生成一張表(io流),03版的Excel使用.xls結(jié)尾 FileOutputStream fileOutputStream = new FileOutputStream(path "學(xué)生表07.xls"); //輸出 workbook.write(fileOutputStream); //關(guān)閉流 fileOutputStream.close(); System.out.println("學(xué)生表07.xls表生成完畢"); } }

      //(2)批量數(shù)據(jù)導(dǎo)入
      @Test
      public void test02() throws IOException {
          String  path = "E:\\java\\project\\springmvc-ajax-json";
          //1.創(chuàng)建一個(gè)工作簿
          HSSFWorkbook workbook = new HSSFWorkbook();
          //2.創(chuàng)建一個(gè)工作表
          HSSFSheet sheet = workbook.createSheet();
          for (int rowNum = 0;rowNum<65536;rowNum  ){
              //3.創(chuàng)建行
              Row row = sheet.createRow(rowNum);
              for (int cellNum = 0;cellNum <5;cellNum  ){
                  Cell cell = row.createCell(cellNum);
                  cell.setCellValue(cellNum);
              }
          }
          System.out.println("寫入完畢");
          FileOutputStream fileOutputStream = new FileOutputStream(path   "工作表03.xls");
          workbook.write(fileOutputStream);
          //關(guān)閉流
          fileOutputStream.close();
      }

      //(3)讀取Excel文件
      @Test public void readExcelTest() throws IOException { //獲取文件流 FileInputStream fileInputStream = new FileInputStream(path "學(xué)生表03.xls"); //創(chuàng)建一個(gè)工作簿 HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream); //得到表 Sheet sheet = workbook.getSheetAt(0); //得到行 Row row = sheet.getRow(0); //得到列 Cell cell = row.getCell(0); System.out.println(cell.getStringCellValue());
              fileInputStream.close();
      }
      //讀取不同數(shù)據(jù)類型 
      @Test public void testCellType() throws IOException { //獲取文件流 FileInputStream fileInputStream = new FileInputStream(path "學(xué)生表03.xls"); //創(chuàng)建一個(gè)工作簿 HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream); HSSFSheet sheet = workbook.getSheetAt(0); //獲取標(biāo)題,表的第一行 Row rowTile = sheet.getRow(0); //如果第一行不為空 if (rowTile != null) { //讀取第一行列的數(shù)量 int numberOfCells = rowTile.getPhysicalNumberOfCells(); //循環(huán)遍歷每一列 for (int i = 0; i < numberOfCells; i ) { //獲得列 Cell cell = rowTile.getCell(i); //如果列不為空 if (cell != null) { //獲取列的類型 CellType cellType = cell.getCellType(); String stringCellValue = cell.getStringCellValue(); System.out.print(stringCellValue "|"); } } } System.out.println(); //獲取行內(nèi)容,行的數(shù)量 int numberOfRows = sheet.getPhysicalNumberOfRows(); //循環(huán)獲取每一行 for (int i = 1; i < numberOfRows; i ) { //獲得行 Row row = sheet.getRow(i); if (row != null) { //獲取列 int numberOfCells1 = rowTile.getPhysicalNumberOfCells(); for (int j = 0; j < numberOfCells1; j ) { System.out.print("[" (j 1) (numberOfCells1 1) "]"); Cell cell = rowTile.getCell(j); //匹配列的數(shù)據(jù)類型 if (cell != null) { CellType cellType = cell.getCellType(); String cellValue = ""; switch (cellType) { case STRING: System.out.println("[STRING]"); cellValue = cell.getStringCellValue(); break; case BOOLEAN: System.out.println("[BOOLEAN]"); cellValue = String.valueOf(cell.getStringCellValue()); break; case BLANK://空 System.out.println("[BOOLEAN]"); break; case NUMERIC://空 System.out.println("[NUMERIC]");//數(shù)字(普通數(shù)字,日期) if (HSSFDateUtil.isCellDateFormatted(cell)) { System.out.println("[日期]"); Date date = cell.getDateCellValue(); cellValue = new DateTime(date).toString("yyyy-MM-dd"); } else { System.out.println("轉(zhuǎn)換為字符串輸出"); cell.setCellType(STRING); cellValue = cell.toString(); } break; case ERROR://空 System.out.println("[數(shù)據(jù)類型錯(cuò)誤]"); break; } System.out.println(cellValue); } } } } //關(guān)閉流 fileInputStream.close(); }

      ?




      ?

      來(lái)源:https://www./content-4-708351.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多