博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java操作excel,pdf,word等文件的方法
阅读量:6077 次
发布时间:2019-06-20

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

 java 读取pdf、word、Excel文件

用到的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/wqzhang/p/6715946.html

你可能感兴趣的文章
《ELK Stack权威指南(第2版)》一3.6 Java日志
查看>>
反射操作公共成员变量
查看>>
小孩的linux
查看>>
CSS3 transforms 3D翻开
查看>>
java基础---->正则表达式
查看>>
2.2013/06/13_log(n)+1
查看>>
关于加载iframe时进度条不消失的问题
查看>>
poj 3984迷宫问题【广搜】
查看>>
oracle ORA-01840:输入值对于日期格式不够长
查看>>
python基础知识~logger模块
查看>>
SIP入门(二):建立SIPserver
查看>>
Servlet3.0的异步
查看>>
WebService连接postgresql( 失败尝试)
查看>>
从头认识java-13.11 对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题?...
查看>>
Python-MacOSX下SIP引起的pip权限问题解决方案(非取消SIP机制)
查看>>
从MFQ方法到需求分析
查看>>
android.view.WindowManager$BadTokenException: Unable to add window
查看>>
HDU5012:Dice(bfs模板)
查看>>
iphone openssh
查看>>
Linux下MEncoder的编译
查看>>