栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java读取各种文件内容

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


@RequestMapping(“/readFile”)
@RestController
@Api(value = “读取文件”, tags = “读取文件”)
public class ReadFileConverter {


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;
}

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;

}


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();

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号