java读取office文件

在网上百度,然后自己终结写了这篇文章
package cn.rjxh.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

public class ControlOfficeFile {

	// 读取world文档里面的内容
	public String getDocContext(String filePath) throws Exception {
		File f = new File(filePath);
		FileInputStream fis = new FileInputStream(f);
		HWPFDocument doc = new HWPFDocument(fis);
		Range rang = doc.getRange();
		String text = rang.text();
		fis.close();
		return text;
	}

	// 读取excel里面的内容
	public String getExcelFile(String filePath) throws Exception {
		StringBuffer buff = new StringBuffer();
		try {
			// 创建对Excel工作簿文件的引用
			HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
			// 创建对工作表的引用。
			for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) { // 读取sheet的个数
				if (null != wb.getSheetAt(numSheets)) { // 如果sheet有内容的话
					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();
	}
	private static final  int N= 10;  //规定的分多少份
	//*********************************************************************
	//拆分pdf文件
		public static void partitionPdfFile(String filepath) throws Exception {
			ControlOfficeFile controlOfficeFile = new ControlOfficeFile();
			Document document = null;
			PdfCopy copy = null;
			try {
				PdfReader reader = new PdfReader(filepath);
				int n = reader.getNumberOfPages();  //获取一共有多少页
				System.out.println("n))))))))))))))))))))))))"+n);
				if (n < N) {
					System.out.println(controlOfficeFile.getPdfContext(filepath));  //将里面的数据显示出来
					return;
				}
				int size = n / N;
				String staticpath = filepath.substring(0, filepath
						.lastIndexOf("\\") + 1);  //获取路径,不包含pdf文件名的路径,到它的上面一层
				System.out.println("staticpath:>>>>>>>>>>>>>>>>>>>>"+staticpath);
				String savepath = null;
				ArrayList<String> savepaths = new ArrayList<String>();
				for (int i = 1; i <= N; i++) {  //处理pdf文件分割后的文件名
					if (i < 10) {
						savepath = filepath.substring(
								filepath.lastIndexOf("\\") + 1,
								filepath.length() - 4);
						savepath = staticpath + savepath + "0" + i + ".pdf";
						savepaths.add(savepath);
					} else {
						savepath = filepath.substring(
								filepath.lastIndexOf("\\") + 1,
								filepath.length() - 4);
						savepath = staticpath + savepath + i + ".pdf";
						savepaths.add(savepath);
					}
				}
				for (int i = 0; i < N - 1; i++) {
					document = new Document(reader.getPageSize(1));
					copy = new PdfCopy(document, new FileOutputStream(savepaths
							.get(i)));//给对应的文件赋值
					document.open();
					for (int j = size * i + 1; j <= size * (i + 1); j++) {
						document.newPage();
						PdfImportedPage page = copy.getImportedPage(reader, j);
						copy.addPage(page);
					}
					document.close();
					System.out.println("savepaths.get(i)>>>>>>>>>>>>>>>>>>>"+savepaths.get(i));
					System.out.println(controlOfficeFile.getPdfContext(savepaths.get(i)));
					File delFile = new File(savepaths.get(i));
					delFile.delete();
				}
				//这是为最后一个赋值                         
				document = new Document(reader.getPageSize(1));
				copy = new PdfCopy(document, new FileOutputStream(savepaths
						.get(N - 1)));
				document.open();
				for (int j = size * (N - 1) + 1; j <= n; j++) {
					document.newPage();
					PdfImportedPage page = copy.getImportedPage(reader, j);
					copy.addPage(page);
				}
				document.close();
				System.out.println(controlOfficeFile.getPdfContext(savepaths.get(N-1)));
				File delFile = new File(savepaths.get(N-1));
				delFile.delete();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (DocumentException e) {
				e.printStackTrace();
			}
		}
	
	
	
	// 读取pdf里面的内容
	public String getPdfContext(String filePath) throws Exception {
		File f = new File(filePath);
		FileInputStream fis = new FileInputStream(f);
		PDFParser p = new PDFParser(fis);
		p.parse();   
		PDDocument pdd = p.getPDDocument();
		PDFTextStripper ts = new PDFTextStripper();
		String c = ts.getText(pdd);
		pdd.close();
		fis.close();
		return c;
	}

	
	
	//*****************************************************************
	
	
	// 读取txt文件里面的内容
	public String getTxtContext(String filePath) throws Exception {
		FileReader fr = new FileReader(filePath);
		BufferedReader br = new BufferedReader(fr);
		StringBuffer buff = new StringBuffer();
		String temp = null;
		while ((temp = br.readLine()) != null) {
			buff.append(temp + "\r\n");
		}
		br.close();
		return buff.toString();
	}

	// 读取ppt里面的内容
	// 直接抽取幻灯片的全部内容
	public String getPPT1(String path) throws Exception {
		File file = new File(path);
		FileInputStream fin = new FileInputStream(file);
		PowerPointExtractor extractor = new PowerPointExtractor(fin);
		return extractor.getText();
	}

	// 一张幻灯片一张幻灯片地读取
	public void getPPT2(String path) throws Exception {
		File file = new File(path);
		FileInputStream fin = new FileInputStream(file);
		SlideShow ss = new SlideShow(new HSLFSlideShow(fin));
		Slide[] slides = ss.getSlides();
		for (int i = 0; i < slides.length; i++) {
			System.out.println(")))))))))))))))" + slides.length);
			// 读取一张幻灯片的标题
			String title = slides[i].getTitle();
			System.out.println("标题:" + title);
			// 读取一张幻灯片的内容(包括标题)
			TextRun[] runs = slides[i].getTextRuns();
			for (int j = 0; j < runs.length; j++) {
				System.out.println(runs[j].getText());
			}
		}
	}

	// 读取RTF文件
	public String getRTFContent(String filePath) {
		String result = null;
		File file = new File(filePath);
		try {
			DefaultStyledDocument styledDoc = new DefaultStyledDocument();
			InputStream is = new FileInputStream(file);
			new RTFEditorKit().read(is, styledDoc, 0);
			result = new String(styledDoc.getText(0, styledDoc.getLength())
					.getBytes("ISO8859_1"));
			// 提取文本,读取中文需要使用ISO8859_1编码,否则会出现乱码
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return result;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值