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



