博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 读取pdf、word、Excel文件
阅读量:7129 次
发布时间:2019-06-28

本文共 4176 字,大约阅读时间需要 13 分钟。

用到的jar:

itextpdf-5.5.8.jar   (PDF)

poi.jar

public class FileUtils {	/**	 * 判断文件是否存在	 * 	 * @Title: isExcite	 * @param @param filePath	 * @param @return	 * @return boolean 返回类型	 * @throws	 */	public static boolean isExcite(String filePath) {		File file = new File(filePath);		// 如果文件夹不存在则创建		if (!file.exists() && !file.isDirectory()) {			return false;		} else {			return true;		}	}	/**	 * 	 * @Title: getPdfFileText	 * @Description: 获取指定位置pdf的文件内容	 * @param @param fileName	 * @param @return	 * @param @throws IOException	 * @return String 返回类型	 * @throws	 */	public static String getPdfFileText(String fileName) throws IOException {		PdfReader reader = new PdfReader(fileName);		PdfReaderContentParser parser = new PdfReaderContentParser(reader);		StringBuffer buff = new StringBuffer();		TextExtractionStrategy strategy;		for (int i = 1; i <= reader.getNumberOfPages(); i++) {			strategy = parser.processContent(i,					new SimpleTextExtractionStrategy());			buff.append(strategy.getResultantText());		}		return buff.toString();	}	/**	 * 获取doc文档	 * 	 * @Title: getTextFromWord	 * @param @param filePath	 * @param @return	 * @return String 返回类型	 * @throws	 */	public static String getTextFromWord(String filePath) {		String result = null;		File file = new File(filePath);		try {			FileInputStream fis = new FileInputStream(file);			WordExtractor wordExtractor = new WordExtractor(fis);			result = wordExtractor.getText();		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}		return result;	}	/**	 * 读取excel内容	 * 	 * @Title: getTextFromExcel	 * @param @param filePath	 * @param @return	 * @return String 返回类型	 * @throws	 */	public static String getTextFromExcel(String filePath) {		StringBuffer buff = new StringBuffer();		try {			// 创建对Excel工作簿文件的引用			HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));			// 创建对工作表的引用。			for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) {				if (null != wb.getSheetAt(numSheets)) {					HSSFSheet aSheet = wb.getSheetAt(numSheets);// 获得一个sheet					for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet							.getLastRowNum(); rowNumOfSheet++) {						if (null != aSheet.getRow(rowNumOfSheet)) {							HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一个行							for (int cellNumOfRow = 0; cellNumOfRow <= aRow									.getLastCellNum(); cellNumOfRow++) {								if (null != aRow.getCell(cellNumOfRow)) {									HSSFCell aCell = aRow.getCell(cellNumOfRow);// 获得列值									switch (aCell.getCellType()) {									case HSSFCell.CELL_TYPE_FORMULA:										break;									case HSSFCell.CELL_TYPE_NUMERIC:										buff												.append(														aCell																.getNumericCellValue())												.append('\t');										break;									case HSSFCell.CELL_TYPE_STRING:										buff.append(aCell.getStringCellValue())												.append('\t');										break;									}								}							}							buff.append('\n');						}					}				}			}		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}		return buff.toString();	}	/**	 * 替换文件内容	 * @Title: replaceContentToFile	 * @param @param path  文件路径	 * @param @param str   要替换的内容	 * @param @param con   替换称的内容	 * @return void 返回类型	 * @throws	 */	public static void replaceContentToFile(String path, String str, String con) {		try {			if (isExcite(path)) {				FileReader read = new FileReader(path);				BufferedReader br = new BufferedReader(read);				StringBuilder content = new StringBuilder();				while (br.ready() != false) {					content.append(br.readLine());					content.append("\r\n");				}				int dex = content.indexOf(str);				if (dex != -1) {					System.out.println("找到标记!");				} else {					System.out.println("指定标记不存在!");				}				content.replace(dex, dex, con);				br.close();				read.close();				FileOutputStream fs = new FileOutputStream(path);				fs.write(content.toString().getBytes());				fs.close();			} else {				System.out.println("文件不存在!");			}		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}	}}

  留着以后直接拿过来用。

转载于:https://www.cnblogs.com/wudi521/p/5302808.html

你可能感兴趣的文章
新书问答:Agile Management
查看>>
在sublime中实现代码检测
查看>>
活在伟大的Scrum团队是什么感觉
查看>>
Swift 5进入发布倒计时
查看>>
一套代码称霸5大端口,移动金融应用还能这样开发?
查看>>
MIT开发Polaris,使网页载入加快34%
查看>>
微软对macOS和Linux开放量子开发工具集
查看>>
一份关于Angular的倡议清单
查看>>
没有估算,你仍然可以用这些决策策略
查看>>
通过调研开源基准测试集,解读大数据的应用现状和开源未来
查看>>
译文-调整G1收集器窍门
查看>>
时序数据库InfluxDB 2.0 alpha 发布:主推新的Flux查询语言,TICK栈将成为整体
查看>>
开源是项“全民工程”,揭秘开源团队的管理运作
查看>>
基于Gitflow分支模型自动化Java项目工作流
查看>>
ES6学习之一
查看>>
专访何红辉:谈谈Android源码中的设计模式
查看>>
超2亿中国用户简历曝光!MongoDB又一重大安全事故
查看>>
网易云信周梁伟专访:亿级架构IM平台的技术难点解析
查看>>
独家揭秘腾讯千亿级参数分布式机器学习系统无量
查看>>
Dubbo Mesh在闲鱼生产环境的落地实践
查看>>