`

解析office文件(doc,xls,ppt,等)

阅读更多
//doc文件
	public static String readDOC(String path) {
		// 创建输入流读取doc文件
		FileInputStream in;
		String text = null;
		path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+path;
		try {
			in = new FileInputStream(new File(path));
			int a= in.available();
			WordExtractor extractor = null;
			// 创建WordExtractor
			extractor = new WordExtractor();
			// 对doc文件进行提取
			text = extractor.extractText(in);
			System.out.println("解析得到的东西"+text);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (text == null) {
			text = "解析文件出现问题";
		}
		return text;
	}



xls

//xls文件
	public static String readXLS(String path) {
		String str = "";
		path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+path;
		try {
			Workbook workbook = null;
			workbook = Workbook.getWorkbook(new File(path));
			Sheet sheet = workbook.getSheet(0);
			Cell cell = null;
			int columnCount = sheet.getColumns();
			int rowCount = sheet.getRows();
			for (int i = 0; i < rowCount; i++) {
				for (int j = 0; j < columnCount; j++) {
					cell = sheet.getCell(j, i);
					String temp2 = "";
					if (cell.getType() == CellType.NUMBER) {
						temp2 = ((NumberCell) cell).getValue() + "";
					} else if (cell.getType() == CellType.DATE) {
						temp2 = "" + ((DateCell) cell).getDate();
					} else {
						temp2 = "" + cell.getContents();
					}
					str = str + "  " + temp2;
				}
				str += "\n";
			}
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (str == null) {
			str = "解析文件出现问题";
		}
		System.out.println("解析得到的东西"+str);
		return str;
	}


docx

// 解析docx
	public static String readDOCX(String path) {
		String river = "";
		path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+path;
		try {
			ZipFile xlsxFile = new ZipFile(new File(path));
			ZipEntry sharedStringXML = xlsxFile.getEntry("word/document.xml");
			InputStream inputStream = xlsxFile.getInputStream(sharedStringXML);
			XmlPullParser xmlParser = Xml.newPullParser();
			xmlParser.setInput(inputStream, "utf-8");
			int evtType = xmlParser.getEventType();
			while (evtType != XmlPullParser.END_DOCUMENT) {
				switch (evtType) {
				case XmlPullParser.START_TAG:
					String tag = xmlParser.getName();
					System.out.println(tag);
					if (tag.equalsIgnoreCase("t")) {
						river += xmlParser.nextText() + "\n";
					}
					break;
				case XmlPullParser.END_TAG:
					break;
				default:
					break;
				}
				evtType = xmlParser.next();
			}
		} catch (ZipException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		if (river == null) {
			river = "解析文件出现问题";
		}
		return river;
	}


xlsx

// 解析xlsx
	public static String readXLSX(String path) {
		String str = "";
		String v = null;
		boolean flat = false;
		List<String> ls = new ArrayList<String>();
		try {
			ZipFile xlsxFile = new ZipFile(new File(path));
			ZipEntry sharedStringXML = xlsxFile
					.getEntry("xl/sharedStrings.xml");
			InputStream inputStream = xlsxFile.getInputStream(sharedStringXML);
			XmlPullParser xmlParser = Xml.newPullParser();
			xmlParser.setInput(inputStream, "utf-8");
			int evtType = xmlParser.getEventType();
			while (evtType != XmlPullParser.END_DOCUMENT) {
				switch (evtType) {
				case XmlPullParser.START_TAG:
					String tag = xmlParser.getName();
					if (tag.equalsIgnoreCase("t")) {
						ls.add(xmlParser.nextText());
					}
					break;
				case XmlPullParser.END_TAG:
					break;
				default:
					break;
				}
				evtType = xmlParser.next();
			}
			ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/sheet1.xml");
			InputStream inputStreamsheet = xlsxFile.getInputStream(sheetXML);
			XmlPullParser xmlParsersheet = Xml.newPullParser();
			xmlParsersheet.setInput(inputStreamsheet, "utf-8");
			int evtTypesheet = xmlParsersheet.getEventType();
			while (evtTypesheet != XmlPullParser.END_DOCUMENT) {
				switch (evtTypesheet) {
				case XmlPullParser.START_TAG:
					String tag = xmlParsersheet.getName();
					if (tag.equalsIgnoreCase("row")) {
					} else if (tag.equalsIgnoreCase("c")) {
						String t = xmlParsersheet.getAttributeValue(null, "t");
						if (t != null) {
							flat = true;
							System.out.println(flat + "有");
						} else {
							System.out.println(flat + "没有");
							flat = false;
						}
					} else if (tag.equalsIgnoreCase("v")) {
						v = xmlParsersheet.nextText();
						if (v != null) {
							if (flat) {
								str += ls.get(Integer.parseInt(v)) + "  ";
							} else {
								str += v + "  ";
							}
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if (xmlParsersheet.getName().equalsIgnoreCase("row")
							&& v != null) {
						str += "\n";
					}
					break;
				}
				evtTypesheet = xmlParsersheet.next();
			}
			System.out.println(str);
		} catch (ZipException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		if (str == null) {
			str = "解析文件出现问题";
		}
		return str;
	}


pptx

public static String readPPTX(String path) {
                List<String> ls = new ArrayList<String>();
                String river = "";
                ZipFile xlsxFile = null;
                try {
                        xlsxFile = new ZipFile(new File(path));// pptx按照读取zip格式读取
                } catch (ZipException e1) {
                        e1.printStackTrace();
                } catch (IOException e1) {
                        e1.printStackTrace();
                }
                try {
                        ZipEntry sharedStringXML = xlsxFile.getEntry("[Content_Types].xml");// 找到里面存放内容的文件
                        InputStream inputStream = xlsxFile.getInputStream(sharedStringXML);// 将得到文件流
                        XmlPullParser xmlParser = Xml.newPullParser();// 实例化pull
                        xmlParser.setInput(inputStream, "utf-8");// 将流放进pull中
                        int evtType = xmlParser.getEventType();// 得到标签类型的状态
                        while (evtType != XmlPullParser.END_DOCUMENT) {// 循环读取流
                                switch (evtType) {
                                case XmlPullParser.START_TAG: // 判断标签开始读取
                                        String tag = xmlParser.getName();// 得到标签
                                        if (tag.equalsIgnoreCase("Override")) {
                                                String s = xmlParser
                                                                .getAttributeValue(null, "PartName");
                                                if (s.lastIndexOf("/ppt/slides/slide") == 0) {
                                                        ls.add(s);
                                                }
                                        }
                                        break;
                                case XmlPullParser.END_TAG:// 标签读取结束
                                        break;
                                default:
                                        break;
                                }
                                evtType = xmlParser.next();// 读取下一个标签
                        }
                } catch (ZipException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (XmlPullParserException e) {
                        e.printStackTrace();
                }
                for (int i = 1; i < (ls.size() + 1); i++) {// 假设有6张幻灯片
                        river += "第" + i + "张················" + "\n";
                        try {
                                ZipEntry sharedStringXML = xlsxFile.getEntry("ppt/slides/slide"
                                                + i + ".xml");// 找到里面存放内容的文件
                                InputStream inputStream = xlsxFile
                                                .getInputStream(sharedStringXML);// 将得到文件流
                                XmlPullParser xmlParser = Xml.newPullParser();// 实例化pull
                                xmlParser.setInput(inputStream, "utf-8");// 将流放进pull中
                                int evtType = xmlParser.getEventType();// 得到标签类型的状态
                                while (evtType != XmlPullParser.END_DOCUMENT) {// 循环读取流
                                        switch (evtType) {
                                        case XmlPullParser.START_TAG: // 判断标签开始读取
                                                String tag = xmlParser.getName();// 得到标签
                                                if (tag.equalsIgnoreCase("t")) {
                                                        river += xmlParser.nextText() + "\n";
                                                }
                                                break;
                                        case XmlPullParser.END_TAG:// 标签读取结束
                                                break;
                                        default:
                                                break;
                                        }
                                        evtType = xmlParser.next();// 读取下一个标签
                                }
                        } catch (ZipException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        } catch (XmlPullParserException e) {
                                e.printStackTrace();
                        }
                }
                if (river == null) {
                        river = "解析文件出现问题";
                }
                return river;
        }



Activity

//得到高度
		Display display = this.getWindowManager().getDefaultDisplay();
		height = display.getHeight();
		width = display.getWidth();

String url = "aa.doc";
//打开文件
		this.setContentView(R.layout.show); 		
		lay_show = (LinearLayout) this.findViewById(R.id.show_layout);		
		//设置阅读背景(平铺)
		Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.rebg);  
		BitmapDrawable bd1 = new BitmapDrawable(bitmap1);  
		bd1.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT );  
		bd1.setDither(true);  
		lay_show.setBackgroundDrawable(bd1);  
		//添加线性参数
		lp_show = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);		
		lp_show.height=height;
		lp_show.weight=width;
		TextView tv = new TextView(this);
		tv.setTextSize(14);
		tv.setTextColor(Color.WHITE);
		tv.setWidth(width);
		lay_show.addView(tv,lp_show);
tv.setText(readDOC(url));


以上方法只能读取字符流,不能读取图片。

方法二,利用手机已安装的软件读取。

//android获取一个用于打开HTML文件的intent 
public static Intent getHtmlFileIntent( String param ) 
{ 
Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build(); 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.setDataAndType(uri, "text/html"); 
return intent; 
} 
//android获取一个用于打开图片文件的intent 
public static Intent getImageFileIntent( String param ) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addCategory("android.intent.category.DEFAULT"); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
Uri uri = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri, "image/*"); 
return intent; 
} 
//android获取一个用于打开PDF文件的intent 
public static Intent getPdfFileIntent( String param ) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addCategory("android.intent.category.DEFAULT"); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
Uri uri = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri, "application/pdf"); 
return intent; 
} 
//android获取一个用于打开文本文件的intent 
public static Intent getTextFileIntent( String param, boolean paramBoolean) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addCategory("android.intent.category.DEFAULT"); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
if (paramBoolean) 
{ 
Uri uri1 = Uri.parse(param ); 
intent.setDataAndType(uri1, "text/plain"); 
} 
else 
{ 
Uri uri2 = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri2, "text/plain"); 
} 
return intent; 
} 
//android获取一个用于打开音频文件的intent 
public static Intent getAudioFileIntent( String param ) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.putExtra("oneshot", 0); 
intent.putExtra("configchange", 0); 
Uri uri = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri, "audio/*"); 
return intent; 
} 
//android获取一个用于打开视频文件的intent 
public static Intent getVideoFileIntent( String param ) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.putExtra("oneshot", 0); 
intent.putExtra("configchange", 0); 
Uri uri = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri, "video/*"); 
return intent; 
} 
//android获取一个用于打开CHM文件的intent 
public static Intent getChmFileIntent( String param ) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addCategory("android.intent.category.DEFAULT"); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
Uri uri = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri, "application/x-chm"); 
return intent; 
} 
//android获取一个用于打开Word文件的intent 
public static Intent getWordFileIntent( String param ) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addCategory("android.intent.category.DEFAULT"); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
Uri uri = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri, "application/msword"); 
return intent; 
} 
//android获取一个用于打开Excel文件的intent 
public static Intent getExcelFileIntent( String param ) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addCategory("android.intent.category.DEFAULT"); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
Uri uri = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri, "application/vnd.ms-excel"); 
return intent; 
} 
//android获取一个用于打开PPT文件的intent 
public static Intent getPptFileIntent( String param ) 
{ 
Intent intent = new Intent("android.intent.action.VIEW"); 
intent.addCategory("android.intent.category.DEFAULT"); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
Uri uri = Uri.fromFile(new File(param )); 
intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); 
return intent; 
} 



用下边代码触发

Intent it =  getWordFileIntent(url);
startActivity(it);

 

 

 

推荐下开的淘宝小店筱夜宫主(http://xiaoyegongzhu.taobao.com/)DR.HU 胡博士美妆~坐在电脑旁的汉子姑娘注意脸部护理呀,做了程序媛,也要美丽啊。。。小店有优惠码:YE4450,报优惠码,抵两元券,(⊙o⊙)… 

分享到:
评论

相关推荐

    android 操作office文档 doc docx xls xlsx ppt pptx pdf

    在Android平台上,处理Office文档(如doc、docx、xls、xlsx、ppt、pptx)以及PDF文件是一项常见的需求。Android本身并不直接支持这些文件格式的处理,因此开发者需要借助第三方库或者Google Drive等在线服务来实现。...

    Android解析并显示doc,docx,xls,xlsx文件

    在Android平台上,解析并显示Microsoft Office格式的文件(如doc, docx, xls, xlsx)是一项常见的需求,尤其在移动应用开发中。这个任务通常涉及到将这些文档转换为更易于处理的格式,例如HTML,以便在Android的...

    使用POI将office(doc/docx/ppt/pptx/xls/xlsx)文件转html格式

    1. **初始化POI**: 首先,我们需要导入Apache POI相关的库,并创建适当的处理对象,如`XSSFWorkbook`(处理.xlsx文件)、`HWPFDocument`(处理.doc文件)或`HSLFSlideShow`(处理.ppt文件)。 2. **读取文件**: ...

    C#的搜索doc\xls\ppt文件中关键字的程序

    在这个程序中,我们需要遍历指定目录下的所有doc、xls和ppt文件,这可以通过`Directory.GetFiles()`方法实现。 2. **Office文档格式**: - **doc**:Microsoft Word的文档格式,可以使用`Microsoft.Office.Interop...

    office办公文档doc、docx、xls、xlsx、ppt、pptx在线预览java代码

    在IT行业中,尤其是在Web开发领域,常常需要处理各种类型的办公文档,例如Microsoft Office的doc、docx、xls、xlsx、ppt、pptx等格式。这些文件通常用于存储文本、表格、图表、幻灯片等内容,而在Web应用中,提供...

    读取各类文件内容(doc,docx,ppt,pptx,xls,xlsx,pdf,txt等)

    本篇文章将详细讲解如何使用Apache POI和PDFBox库来读取doc, docx, ppt, pptx, xls, xlsx, pdf以及txt等各类文件的内容。 首先,Apache POI是一个流行的Java API,专门用于处理Microsoft Office格式的文件,如Word...

    Microsoft Office Binary (doc, xls, ppt) File Formats for PPT

    微软Office PowerPoint的二进制文件格式(.ppt)是IT领域中一个重要的知识点,它不仅代表了微软在办公软件开发方面的技术成就,也是理解和操作PPT文件的基础。通过深入了解这一格式的规范,用户和开发者可以更好地...

    Office File Format_doc_xls_ppt_xlsd二进制文件格式

    `PowerPoint97-2007BinaryFileFormat(ppt)Specification.pdf`提供了详细指南,解释了如何解析和构建.ppt文件。 理解这些二进制文件格式对于开发者来说至关重要,特别是那些需要处理Office文件的程序或库。通过分析...

    Microsoft Office Binary (doc, xls, ppt) File Formats for Excel 97-2007

    ### Microsoft Office Binary (doc, xls, ppt) 文件格式详解 #### 概述 Microsoft Office 的二进制文件格式,特别是 Excel 97-2007 版本的 .xls 文件格式,是一种由 Microsoft 设计并广泛使用的专有文件格式。这种...

    office(doc、ppt、excel)转html poi实现

    在IT领域,转换Office文档(如DOC、PPT和XLS)到HTML是一种常见的需求,尤其是在数据共享、网络发布和跨平台兼容性方面。Apache POI是一个强大的Java库,专门用于处理Microsoft Office格式的文件,其中包括了将...

    office文件(含doc,docx,xls,xlsx,ppt,pptx等)转PDF生成(C#程序)

    2. **读取Office文件**:使用NPOI,你可以逐个读取doc、docx、xls、xlsx、ppt和pptx文件。例如,对于Word文档,可以使用`HSSFWorkbook`(针对旧版的doc)或`XSSFWorkbook`(针对docx)来读取内容。 3. **解析内容**...

    pdf,doc,xls,ppt文档转换文档和源码(c#)

    对于PPT文件的处理,可以借助Microsoft.Office.Interop.PowerPoint,这同样需要系统安装PowerPoint。然而,如果仅需转换而无需编辑,可以考虑使用Aspose.Slides或NPOI这样的库,它们提供API来读取、写入和转换PPT...

    asp.net doc ppt xls jpg pdf等转swf

    - **DOC、PPT转SWF**:可以借助Microsoft Office的COM接口,结合自动化技术,先将DOC或PPT文件导出为图片序列,再利用Flash软件或其他工具将图片序列转化为SWF。 - **JPG转SWF**:由于JPG本身就是图像格式,转换...

    .pdf/.doc/.docx/.xls/.xlsx/.ppt/.pptx 文件网页预览 ASP.NET MVC 项目

    在IT行业中,尤其是在Web开发领域,常常需要处理各种类型的文件,如PDF、Word文档(.doc/.docx)、Excel表格(.xls/.xlsx)以及PowerPoint演示文稿(.ppt/.pptx)。这些文件格式广泛应用于日常工作和学习,因此提供...

    office文档转swf用于在线预览

    标签中的“office doc xls ppt文档转sw”进一步明确了转换的文档类型和目标格式:doc对应Word文档,xls对应Excel电子表格,ppt则代表PowerPoint演示文稿。而“sw”是SWF文件的简称,说明这个系统支持转换这三种类型...

    Lucene.Net 文件检索doc,xls,ppt,txt,pdf文件(实例)

    本文将深入探讨如何使用Lucene.Net进行文件检索,特别是针对doc、xls、ppt、txt和pdf等常见文件类型的检索。通过实例化和理解Lucene.Net的关键概念,你可以构建出强大的文件搜索解决方案。 首先,我们需要了解...

    java实现ppt,xls,doc在线预览

    对于PPT文件,我们可以使用Apache POI库,这是一个Java API,用于处理Microsoft Office格式的文件。通过解析PPT文件,我们可以提取幻灯片内容并将其转换为HTML或其他适合浏览器的格式。另一种方法是利用第三方服务...

    Microsoft Office Binary (doc, xls, ppt) File Formats for Excel2007 Only

    它通过减小文件大小、提高数据读取速度以及增强数据完整性和安全性等特性,为用户提供了更高效的使用体验。尽管这种格式主要用于Excel 2007及以后版本,但在许多情况下,它的优势仍然使得它成为一个值得考虑的选择,...

    poi.jar,java解析office 文件

    1. **HSLF**(Horrible Slide Layout Format):处理老版的.PPT文件,支持PowerPoint 97-2003。 2. **XSLF**:处理.PPTX文件,兼容PowerPoint 2007及以后版本。 #### 常用操作 - 创建新的PowerPoint演示文稿,添加...

    android,poi不用服务器,android支持浏览doc,docx,xlsx,xls,ppt,pptx,可以解析图片

    在Android开发中,有时我们需要处理各种办公文档,如Word(doc、docx)、Excel(xlsx、xls)和PowerPoint(ppt、pptx)文件。在标题和描述中提到的,是利用Apache POI库实现一个Android应用,允许用户在本地设备上...

Global site tag (gtag.js) - Google Analytics