if (null != workbook.getSheetAt(numSheets)) {
XSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet
for (int rowNumOfSheet = 0; rowNumOfSheet = aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
XSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一个行
for (short cellNumOfRow = 0; cellNumOfRow = aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
XSSFCell aCell = aRow.getCell(cellNumOfRow);// 获得列值
if (this.convertCell(aCell).length()0) {
content.append(this.convertCell(aCell));
}
}
content.append("\n");
}
}
}
}
}
return content.toString();
}
private String convertCell(Cell cell) {
NumberFormat formater = NumberFormat.getInstance();
formater.setGroupingUsed(false);
String cellValuehttps://www.04ip.com/post/= "";
if (cell == null) {
return cellValue;
}
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
cellValue = https://www.04ip.com/post/formater.format(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
cellValue = https://www.04ip.com/post/cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
cellValue = https://www.04ip.com/post/cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue = https://www.04ip.com/post/Boolean.valueOf(cell.getBooleanCellValue()).toString();
break;
case HSSFCell.CELL_TYPE_ERROR:
cellValue = https://www.04ip.com/post/String.valueOf(cell.getErrorCellValue());
break;
default:
cellValuehttps://www.04ip.com/post/= "";
}
return cellValue.trim();
}
// 读取pdf文件
public String readPDF(String file) throws IOException {
String result = null;
FileInputStream is = null;
PDDocument document = null;
try {
is = new FileInputStream(file);
PDFParser parser = new PDFParser(is);
parser.parse();
document = parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
result = stripper.getText(document);
} finally {
if (is != null) {
is.close();
}
if (document != null) {
document.close();
}
}
return result;
}
// 读取doc文件
public String readWORD(String file) throws Exception {
String returnStr = "";
try {
WordExtractor wordExtractor = new WordExtractor(new FileInputStream(new File(file)));
returnStr = wordExtractor.getText();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return returnStr;
}
// 读取docx文件
public String readWORD2007(String file) throws Exception {
return new XWPFWordExtractor(POIXMLDocument.openPackage(file)).getText();
}
// 读取txt文件
public String readTXT(String file) throws IOException {
String encoding = ReadFileUtils.get_charset(new File(file));
if (encoding.equalsIgnoreCase("GBK")) {
return FileUtils.readFileToString(new File(file), "gbk");
} else {
return FileUtils.readFileToString(new File(file), "utf8");
}
}
private static String get_charset(File file) throws IOException {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
BufferedInputStream bis = null;
try {
boolean checked = false;
bis = new BufferedInputStream(new FileInputStream(file));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFFfirst3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
推荐阅读
- 新媒体如何推动旅游,新媒体下旅游
- 红米6怎么连接手机u盘,红米6pro怎么usb连接电脑
- 苹果电脑如何打开excel,苹果电脑如何打开终端输入命令
- python如何在用户端输入列表,python中如何让用户输入一个列表
- php数据太多查询不显示 php 显示数据库最新10条
- 视频号等级怎么分,视频号级别怎么升
- 菠萝区块链,菠萝币区块链合法吗
- vb.net图片框 vb在图像框中显示图片
- 妙笔生花安卓画画教程视频,妙笔生花绘画软件新手教程手机