POI处理Word、Excel、PowerPoint

news/2024/7/21 5:00:14 标签: Excel, Java, Apache, F#, C#

第一:下载POI,在http://jakarta.apache.org/poi/中,下载poi-bin-3.5-beta4-20081128.zip,解压后把jar包引入项目工程。

第二:处理Word(Word.java)


import org.apache.poi.hwpf.extractor.WordExtractor;

import java.io.File;

import java.io.InputStream;

 

public class Word {

    public static void main(String[] args) throws Exception {

       System.out.println(getContent("c:\\11.doc"));

    }

 

    public static String getContent(String s) throws Exception {

       return getContent(new java.io.FileInputStream(s));

    }

 

    public static String getContent(File f) throws Exception {

       return getContent(new java.io.FileInputStream(f));

    }

 

    public static String getContent(InputStream is) throws Exception {

       String bodyText = null;

       WordExtractor ex = new WordExtractor(is);

       bodyText = ex.getText();

       return bodyText;

    }

}

 
 


第三:处理Excel(Excel.java)


import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFCell;

import java.io.File;

import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

 

public class Excel {

    public static void main(String[] args) throws Exception {

       System.out.println(getContent("c:\\22.xls"));

    }

 

    public static String getContent(String s) throws Exception {

       return getContent(new java.io.FileInputStream(s));

    }

 

    public static String getContent(File f) throws Exception {

       return getContent(new java.io.FileInputStream(f));

    }

 

    public static String getContent(InputStream is) throws Exception {

       StringBuffer content = new StringBuffer();

       HSSFWorkbook workbook = new HSSFWorkbook(is);

       for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {

           HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet

           content.append("\n");

           if (null == aSheet) {

              continue;

           }

           for (int rowNum = 0; rowNum <= aSheet.getLastRowNum(); rowNum++) {

              content.append("\n");

              HSSFRow aRow = aSheet.getRow(rowNum);

              if (null == aRow) {

                  continue;

              }

              for (short cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++) {

 

                  HSSFCell aCell = aRow.getCell(cellNum);

                  if (null == aCell) {

                     continue;

                  }

                  if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {

                     content.append(aCell.getRichStringCellValue()

                            .getString());

                  } else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {

                     boolean b = HSSFDateUtil.isCellDateFormatted(aCell);

                     if (b) {

                         Date date = aCell.getDateCellValue();

                         SimpleDateFormat df = new SimpleDateFormat(

                                "yyyy-MM-dd");

                         content.append(df.format(date));

                     }

                  }

              }

           }

       }

       return content.toString();

    }

}

 
 


第四:处理PowerPoint(PowerPoint.java)

import java.io.File;

import java.io.InputStream;

import org.apache.poi.hslf.HSLFSlideShow;

import org.apache.poi.hslf.model.TextRun;

import org.apache.poi.hslf.model.Slide;

import org.apache.poi.hslf.usermodel.SlideShow;

 

public class PowerPoint {

    public static void main(String[] args) throws Exception {

       System.out.println(getContent("c:\\33.ppt"));

    }

 

    public static String getContent(String s) throws Exception {

       return getContent(new java.io.FileInputStream(s));

    }

 

    public static String getContent(File f) throws Exception {

       return getContent(new java.io.FileInputStream(f));

    }

 

    public static String getContent(InputStream is) throws Exception {

       StringBuffer content = new StringBuffer("");

       SlideShow ss = new SlideShow(new HSLFSlideShow(is));

       Slide[] slides = ss.getSlides();

       for (int i = 0; i < slides.length; i++) {

           TextRun[] t = slides[i].getTextRuns();

           for (int j = 0; j < t.length; j++) {

              content.append(t[j].getText());

           }

           content.append(slides[i].getTitle());

       }

       return content.toString();

    }

}

 
 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tim_zhang8888/archive/2009/02/07/3865980.aspx


http://www.niftyadmin.cn/n/872529.html

相关文章

Eclipse3.4之Copy Qualified Name复制类全名解决办法

Eclipse3.4之Copy Qualified Name复制类全名解决办法 Eclipse3.4用Copy Qualified Name复制类全名时总是这样的/hb03/src/org/self/hb/entity/Account.java很不方便可以这样解决^下载下边插件解压到Eclipse安装目录下http://www.jave.de/eclipse/copyfully/copyfully_1.2.0.zip…

分类 和 聚类

简单地说&#xff0c;分类(Categorization or Classification)就是按照某种标准给对象贴标签(label)&#xff0c;再根据标签来区分归类。 简单地说&#xff0c;聚类是指事先没有“标签”而通过某种成团分析找出事物之间存在聚集性原因的过程。 区别是&#xff0c;分类是事先定…

主要分类方法介绍

主要分类方法介绍解决分类问题的方法很多[40-42] &#xff0c;单一的分类方法主要包括&#xff1a;决策树、贝叶斯、人工神经网络、K-近邻、支持向量机和基于关联规则的分类等&#xff1b;另外还有用于组合单一分类方法的集成学习算法&#xff0c;如Bagging和Boosting等。 &am…

docker镜像安装报错pull access denied for XXXX

使用Ubuntu 系统学习docker的时候&#xff0c;安装镜像一直报错&#xff1a;如图 即使登录上docker&#xff0c;也无法解决。 想来想去应该是下载速度的问题。百度后&#xff0c;发现确实是这个问题。别人都是直接就下载成功&#xff0c;那还在这里bb。所以加入阿里云的加速地…

数据挖掘能做什么

数据挖掘不仅能对过去的数据进行查询和遍历&#xff0c;并且能够对将来的趋势和行为进行预测&#xff0c;并自动探测以前未发现的模式&#xff0c;从而很好地支持人们的决策。被挖掘出来的信息&#xff0c;能够用于信息管理、查询处理、决策支持、过程控制以及许多其它应用。数…

数据挖掘中分类算法总结

数据仓库&#xff0c;数据库或者其它信息库中隐藏着许多可以为商业、科研等活动的决策提供所需要的知识。分类与预测是两种数据分析形式&#xff0c;它们可以用来抽取能够描述重要数据集合或预测未来数据趋势的模型。分类方法&#xff08;Classification&#xff09;用于预测数…

xcode 项目build的执行文件设置到项目目录下

第一次使用的xcode 的小伙伴可能找不到build的执行文件&#xff0c;这是因为xcode的构建路径和别的IDE 很不同&#xff0c;默认不是构建在workspace下&#xff0c;而是在/Users/jun/Library/Developer/Xcode/DerivedData 目录下。 设置位置在 preferences ->location ->…

spring boot 事务行为

Transactional propagation 定义事务的生命周期 REQUIRED&#xff08;默认值&#xff09; 方法A调用时没有事务新建一个事务&#xff0c;当在方法A调用另一个方法B的时候&#xff0c;方法B将使用相同的事务&#xff1b;如果方法B发生异常需要数据回滚的时候&#xff0…