0
点赞
收藏
分享

微信扫一扫

java读取各种文件内容

晒大太阳了 2022-04-22 阅读 62
java

package com.xahengpin.cqfk.tjfx.common;

import com.xahengpin.common.modules.utils.StringUtils;
import com.xahengpin.xahp.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
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.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**

  • @author ssp

  • @className ReadFileConverter

  • @description TODO

  • @create 2021/11/16 10:43

  • 读取文件返回文件内容
    **/
    @RequestMapping(“/readFile”)
    @RestController
    @Api(value = “读取文件”, tags = “读取文件”)
    public class ReadFileConverter {

    /**

    • 上传文件

    • @param file

    • @return
      /
      @PostMapping(value = “/uploadFile”, consumes = "multipart/
      ", headers = “content-type=multipart/form-data”)
      @Inner(value = false)
      public String readFile(@RequestPart(“file”) MultipartFile file) {

      if (file.getOriginalFilename().endsWith(“.txt”) || file.getOriginalFilename().endsWith(“.log”)) {

       return readText(file);
      

      } else if (file.getOriginalFilename().endsWith(“.doc”) || file.getOriginalFilename().endsWith(“.docx”)) {
      return readWord(file);
      } else if (file.getOriginalFilename().endsWith(“.xls”) || file.getOriginalFilename().endsWith(“.xlsx”)) {

       return readExcel(file);
      

      } else {
      return “请选择有效的txt,log,doc,docx,xls,xlsx的文件”;
      }
      }

    /**

    • 读txt.log文件

    • @param file

    • @return
      */
      public static String readText(MultipartFile file) {
      String content = null;
      StringBuffer fsb = new StringBuffer();
      try {
      InputStream stream = file.getInputStream();

       InputStreamReader reader = new InputStreamReader(stream,"GB2312");
       BufferedReader buffReader = new BufferedReader(reader);
       while((content = buffReader.readLine())!=null){
           fsb.append(content);
       }
       buffReader.close();
      

      } catch (Exception e) {
      e.printStackTrace();
      }
      content = fsb.toString();

      return content;
      }

    /**

    • 读word文件
    • @param file
    • @return
      */

    public static String readWord(MultipartFile file) {
    String buffer = “”;
    try {
    if (file.getOriginalFilename().endsWith(“.doc”)) {
    InputStream stream = file.getInputStream();
    WordExtractor ex = new WordExtractor(stream);
    buffer = ex.getText();
    stream.close();
    } else if (file.getOriginalFilename().endsWith(“docx”)) {
    InputStream stream = file.getInputStream();
    XWPFDocument document = new XWPFDocument(stream);
    XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(document);
    buffer = xwpfWordExtractor.getText();
    stream.close();
    } else {
    System.out.println(“此文件不是word文件!”);
    }

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

    }

    /**

    • 读取excel文件内容

    • 支持单sheet

    • @param file

    • @return
      */
      public static String readExcel(MultipartFile file) {
      InputStream fileInput = null;//创建文件输入流
      StringBuilder builder = new StringBuilder();
      try {

       fileInput = file.getInputStream();
       if (file.getOriginalFilename().endsWith(".xls")) {
           Workbook wb = new HSSFWorkbook(fileInput);
           Sheet sheetAt = wb.getSheetAt(0);
           //rowBegin代表要开始读取的行号,下面这个循环的作用是读取每一行内容
           for (int i = 0; i <= sheetAt.getLastRowNum(); ++i) {
               Row row = sheetAt.getRow(i);//获取每一行
               int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号,即总列数
               for (int j = 0; j < columnNum; ++j) {
                   Cell cell = row.getCell(j);//获取每个单元格
                   cell.setCellType(Cell.CELL_TYPE_STRING);
                   builder.append(cell.getStringCellValue() + " ");
               }
           }
           System.out.println(builder.toString());
           fileInput.close();
       } else {
           XSSFWorkbook wb = new XSSFWorkbook(fileInput);//由输入流文件得到工作簿对象
           XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
           int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
           //rowBegin代表要开始读取的行号,下面这个循环的作用是读取每一行内容
           for (int i = 0; i <= lastRowNum; ++i) {
               XSSFRow row = sheet.getRow(i);//获取每一行
               int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号,即总列数
               for (int j = 0; j < columnNum; ++j) {
                   XSSFCell cell = row.getCell(j);//获取每个单元格
                   if (StringUtils.isNotEmpty(cell)){
                       cell.setCellType(Cell.CELL_TYPE_STRING);
                       builder.append(cell.getStringCellValue() + " ");
                   }
      
               }
           }
           System.out.println(builder.toString());
           fileInput.close();
       }
      

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

      return builder.toString();

    }

}

举报

相关推荐

0 条评论