溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

詳解java封裝實(shí)現(xiàn)Excel建表讀寫(xiě)操作

發(fā)布時(shí)間:2020-10-24 08:51:31 來(lái)源:腳本之家 閱讀:139 作者:yumiaoxa 欄目:編程語(yǔ)言

對(duì) Excel 進(jìn)行讀寫(xiě)操作是生產(chǎn)環(huán)境下常見(jiàn)的業(yè)務(wù),網(wǎng)上搜索的實(shí)現(xiàn)方式都是基于POI和JXL第三方框架,但都不是很全面。小編由于這兩天剛好需要用到,于是就參考手寫(xiě)了一個(gè)封裝操作工具,基本涵蓋了Excel表(分有表頭和無(wú)表頭)的創(chuàng)建,并對(duì)它們進(jìn)行讀寫(xiě)操作。為方便大家,有需要者可以點(diǎn)擊文后點(diǎn)解下載直接使用哦,當(dāng)然也可以根據(jù)自己需求舉一反三自己定制,相信對(duì)于聰明的你也不是什么難事。話不多說(shuō),直接貼源碼

pom.xml 文件:

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>
 </properties>

 <dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
 </dependency>
 <dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.17</version>
 </dependency>
 <dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.0</version>
  <scope>provided</scope>
 </dependency>
 <dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.8.0-beta2</version>
  <scope>test</scope>
 </dependency>
 <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
 </dependency>
 <dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.8.0-beta2</version>
 </dependency>
 </dependencies>

建表工具類(lèi):ExcelBuider.java

/**
   * 建表工具類(lèi)
   * @author Sherman
   * email:1253950375@qq.com
   * created in 2018/8/24
   */
  @Slf4j
  public class ExcelBuilder {

   private static HSSFSheet sheet;
   private static HSSFWorkbook wb;
   private static boolean hasHeader;

   /**
    * 初始化
    * @param excellName 表名
    */
   public ExcelBuilder(String excellName) {
    wb = new HSSFWorkbook();
    sheet = wb.createSheet(excellName);
   }

   /**
    * 設(shè)置表頭,裝配表頭數(shù)據(jù)
    * @param value 字符串?dāng)?shù)組,用來(lái)作為表頭的值
    *
    */
   public ExcelBuilder header(String... value) {
    if (value != null && value.length != 0) {
     //設(shè)置表頭樣式
     HSSFCellStyle cellStyle = wb.createCellStyle();
     cellStyle.setFont(font("黑體", true, 12));
     HSSFRow row = sheet.createRow(0);
     for (int i = 0; i < value.length; i++) {
      HSSFCell cell = row.createCell(i);
      cell.setCellValue(value[i]);
      cell.setCellStyle(cellStyle);
     }
     hasHeader = true;
    }
    return this;
 }

 /**
  * excel 表內(nèi)容裝配
  * @param content 待裝配表格內(nèi)容的二維數(shù)組
  * @return
  */
 public ExcelBuilder content(List<List<Object>> content) {
  if (content != null && !content.isEmpty()) {
   int index;
   for (int i = 0; i < content.size(); i++) {
    index = hasHeader == false ? i : i + 1;
    HSSFRow row = sheet.createRow(index);
    for (int j = 0; j < content.get(i).size(); j++) {
     String r = "";
     Object value = content.get(i).get(j);
     //根據(jù)數(shù)據(jù)類(lèi)型裝配
     if (value instanceof String) {
      r = (String) value;
     } else if (value instanceof Number) {
      r = String.valueOf(value);
     } else if (value instanceof BigDecimal) {
      r = String.valueOf(value);
     } else {
      if (!(value instanceof Date) && !(value instanceof Timestamp)) {
       if (!(value instanceof ZonedDateTime) && !(value instanceof LocalDateTime)) {
        if (value instanceof Enum) {
         r = ((Enum) value).name();
        } else if (value != null) {

         log.info("Error of create row, Unknow field type: " + value.getClass().getName());
        }
       } else {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        r = formatter.format((TemporalAccessor) value);
       }
      } else {
       DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       r = sdf.format(value);
      }
     }

     row.createCell(j).setCellValue(r);
    }
   }
  }
  return this;
 }

 /**
  * 自動(dòng)調(diào)整列寬大小
  */
 public ExcelBuilder autoColumnWidth() {
  for (int j = 0; j < sheet.getRow(0).getLastCellNum(); j++) {
   int maxLength = 0;
   for (int i = 0; i <= sheet.getLastRowNum(); i++) {
    String value = sheet.getRow(i).getCell(j).getStringCellValue();
    int length = 0;
    if (value != null) {
     length = value.getBytes().length;
    }
    if (length > maxLength) {
     maxLength = length;
    }
   }
   sheet.setColumnWidth(j, maxLength > 30 ? (30 * 256 + 186) : (maxLength * 256 + 186));
  }
  return this;
 }

 /**
  * 實(shí)例化
  * @param hasHeader 是否有表頭
  * @return Excel表格
  */
 public AbstractExcel build(Boolean hasHeader) {
  return hasHeader ? new HeaderExcel(sheet) : new NoHeaderExcel(sheet);
 }

 /**
  *
  * @param fontName 字體名字
  * @param isBold 是否粗體
  * @param fontSize 字體大小
  * @return 字體
  */
 private HSSFFont font(String fontName, boolean isBold, int fontSize) {
  HSSFFont font = wb.createFont();
  if (fontName != null) font.setFontName(fontName);
  else font.setFontName("黑體");
  font.setBold(isBold);
  font.setFontHeightInPoints((short) fontSize);
  return font;
 }

}

excel的抽象父類(lèi):

/**
 * @author Sherman
 * created in 2018/8/24
 */

public abstract class AbstractExcel {
 private final HSSFSheet sheet;

 public AbstractExcel() {
  HSSFWorkbook wb = new HSSFWorkbook();
  sheet = wb.createSheet();
 }

 public AbstractExcel(String sheetName){
  HSSFWorkbook wb = new HSSFWorkbook();
  sheet = wb.createSheet(sheetName);
 }

 public AbstractExcel(HSSFSheet sheet) {
  this.sheet = sheet;
 }



 public abstract List<Map<String, String>> getPayload();


 public void write(OutputStream op) throws IOException {
  sheet.getWorkbook().write(op);
  sheet.getWorkbook().close();
 }

 public String getStringFormatCellValue(HSSFCell cell) {
  String cellVal = "";
  DecimalFormat df = new DecimalFormat("#");
  switch (cell.getCellTypeEnum()) {
   case STRING:
    cellVal = cell.getStringCellValue();
    break;
   case NUMERIC:
    String dataFormat = cell.getCellStyle().getDataFormatString();
    if (DateUtil.isCellDateFormatted(cell)) {
     cellVal = df.format(cell.getDateCellValue());
    } else if ("@".equals(dataFormat)) {
     cellVal = df.format(cell.getNumericCellValue());
    } else {
     cellVal = String.valueOf(cell.getNumericCellValue());
     df = new DecimalFormat("#.#########");
     cellVal = df.format(Double.valueOf(cellVal));
    }
    break;
   case BOOLEAN:
    cellVal = String.valueOf(cell.getBooleanCellValue());
    break;
   case FORMULA:
    cellVal = String.valueOf(cell.getCellFormula());
    break;
   default:
    cellVal = "";
  }
  return cellVal;
 }


}

有表頭實(shí)現(xiàn)類(lèi)

/**
 * @author Sherman
 * created in 2018/8/24
 */

public class HeaderExcel extends AbstractExcel {
 private final static boolean hasHeader = true;
 private final HSSFSheet sheet;

 public HeaderExcel(HSSFSheet sheet) {
  super(sheet);
  this.sheet = sheet;
 }

 public HeaderExcel(String sheetName, String excelPath) {
  HSSFWorkbook wb = null;
  try {
   wb = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(excelPath)));
  } catch (IOException e) {
   e.printStackTrace();
  }
  sheet = sheetName == null || sheetName.isEmpty() ? wb.getSheetAt(0) : wb.getSheet(sheetName);
 }

 @Override
 public List<Map<String, String>> getPayload() {
  List<Map<String, String>> payLoad = new ArrayList<>();
  HSSFRow headRow = sheet.getRow(0);
  for (int i = 1; i <= sheet.getLastRowNum(); i++) {
   HSSFRow currentRow = sheet.getRow(i);
   Map<String, String> map = new HashMap<>();
   for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) {
    map.put(getStringFormatCellValue(headRow.getCell(j)), getStringFormatCellValue(currentRow.getCell(j)));
   }
   payLoad.add(map);
  }
  return payLoad;
 }


}

無(wú)表頭實(shí)現(xiàn)類(lèi)

/**
 * @author Sherman
 * created in 2018/8/24
 */

public class NoHeaderExcel extends AbstractExcel {
 private final static boolean hasHeader = false;
 private HSSFSheet sheet;

 public NoHeaderExcel(HSSFSheet sheet) {
  super(sheet);
  this.sheet = sheet;
 }

 public NoHeaderExcel(String sheetName, String excelPath) {
  HSSFWorkbook wb = null;
  try {
   wb = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(excelPath)));
  } catch (IOException e) {
   e.printStackTrace();
  }
  sheet = sheetName == null || sheetName.isEmpty() ? wb.getSheetAt(0) : wb.getSheet(sheetName);
 }


 @Override
 public List<Map<String, String>> getPayload() {
  List<Map<String, String>> payLoad = new ArrayList<>();
  for (int i = 0; i < sheet.getLastRowNum(); i++) {
   HSSFRow currentRow = sheet.getRow(i);
   Map<String, String> map = new HashMap<>();
   for (int j = 0; j <= sheet.getRow(i).getLastCellNum(); j++) {
    map.put(String.valueOf(j), getStringFormatCellValue(currentRow.getCell(j)));
   }
   payLoad.add(map);
  }
  return payLoad;
 }


}

測(cè)試工具類(lèi):

/**
 * Unit test for simple App.
 */
public class AppTest 
{
 /**
  * 測(cè)試建表,寫(xiě)表操作
  */
 @Test
 public void testExportExcel()
 {
  //測(cè)試數(shù)據(jù)
  String[] headers = new String[]{"A","B","C","D","E"};
   List<List<Object>> valueList = new LinkedList<>();
  for (char i = 'A'; i <= 'E' ; i++) {
   List<Object> rowList = new LinkedList<>();
   for (int j = 0; j <= 4; j++) {
    rowList.add(i+String.valueOf(j));
   }
   valueList.add(rowList);
  }

 AbstractExcel excel = new ExcelBuilder("報(bào)名表")
   .header(headers)
   .content(valueList)
   .autoColumnWidth()
   .build(true);

  try {
   File file = new File("E:\\excel\\test.xls");
   FileOutputStream op = new FileOutputStream(file);
   excel.write(op);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 測(cè)試讀取表數(shù)據(jù)操作
  */
 @Test
 public void testImportExcel(){
  AbstractExcel excel = new HeaderExcel(null,"E:/excel/test.xls");
  List<Map<String,String>> values = excel.getPayload();
  values.forEach(stringStringMap -> {
   stringStringMap.entrySet().forEach(stringStringEntry -> {
    System.out.println(stringStringEntry.getKey()+"---->"+stringStringEntry.getValue());
   });

  });
 }

}

附圖:

測(cè)試1

詳解java封裝實(shí)現(xiàn)Excel建表讀寫(xiě)操作

測(cè)試二:

詳解java封裝實(shí)現(xiàn)Excel建表讀寫(xiě)操作

看起來(lái)效果還不錯(cuò),當(dāng)然還有很多不完善的地方,有需要的朋友可以在此基礎(chǔ)上擴(kuò)展定制,例如讀取表數(shù)據(jù)結(jié)構(gòu)方式,實(shí)現(xiàn)行數(shù)增刪改查據(jù)或者創(chuàng)建表標(biāo)題等等。

或者有朋友有更好的實(shí)現(xiàn)方案,歡迎前來(lái)交流!

最后的最后,當(dāng)然忘不了附上笨工具的源碼啦!

https://github.com/yumiaoxia/excel-commom-demo.git

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI