数学公式是如何实现的?

数学公式实现大方向

青优网


有个软件可以做到转换公式:word2tex和tex2word

后台上传文件–>系统识别出公式,将其转换为Latex代码–>前台用Mathjax进行渲染。


梯子网


Aspose,它是美国开发商的商业包,具体详情登录外网才能知道。google搜到了官网,看了下产品信息,发现它近乎完美支持Java/.net生成word文档,涵括了字体大小、主标题副标题、表格、颜色等等等等。

分析了一下局势,原来是和微软的word不开源有关系的。虽然我们在word里写了一段话、传了一张图片、编辑了一个公式,但是它底层到底经过了哪些步骤,是以什么形式存储的,这些大家都不知道。所以虽然有工具可以将word公式转成tex以及转回去,但是有时候还是会生成乱码,说明稳定性还是不容乐观。具体问题我就不深入研究了。


猿题库:


猿题库主打学生端的在线刷题app,虽然它也有题库系统,但是仅从手机app端来说很难看出来它是怎么设计和维护的。不过我们可以从手机app端能了解到他们的题的构成。

将题目分享到微信朋友之后,再用微信电脑端打开,我们就可以在pc端拿到手机app上猿题库的试题链接了!

这样一看就明显了:猿题库的公式都是用图片进行存储的。

解析word并切图并不复杂,是可以实现的,具体就不赘述了


最理想的题库管理方案是:支持在线上传以及word上传,支持在线编辑,支持下载,并且下载的word公式是可以进行编辑的。

然而现实却不能完全实现,所以只能在设计自己的题库时有所取舍。

Mathjax可以解析Latex、mathml等代码。

word2tex可以将word里的公式转换为tex代码。

Latex是基于tex的,理论上是相通的。

Aspose是商业包,近乎完美地支持Java/.NET在线编辑生成word文档。

题库是在线K12教育的基础,而题的实现则是题库的基础。在设计自家题库之初要想到所有的可能性以及解决方案,有了方向做起来就不难了。

好吧,其实后面的打赏显示是手敲上去的,简书目前并没有支持打赏显示隐藏信息。


既然大家都对如何实现比较好奇,那么我简单地说一下吧。

首先要看你的公司支不支持一个专门录题团队。有钱,有精力,这样生成的题质量是最好的。

其次是不是有必要下载成word。目前网上有很多卖题的,题里面带有html标签,很不规范,下载下来的题也不能看。

再次下载成word的公式有没有必要可以编辑。也有卖word版的,他们的题的公式都是图片。

还有网上买的题有一小部分的题需要人工审核修改,后期有一定的工作量,不能保证买完全部能用。而且买的代码标签格式的题,不能下载成word。

我家题库大概就是通过上面几点然后做的决策。具体细节涉及商业机密不便透露了。


三种方式:1.输出公式图片、2.输出公式html、3.输出Latex让前端渲染

我们曾经敲出一块服务器资源专门做公式的后台渲染,然后把渲染出来的公式html保存到数据库,客户端请求时直接推送过去,连渲染都省了,比图片要轻便。 唯一的坏处是公式太多时,文本因为包含了太多公式标签导致体积过大。


数学公式实现之——解析world

android 解析docx文件

总结

使用poi解析docx文件
流程:
调用接口将docx转化为html,然后app中通过webview加载该html即可显示

在自己的应用中实现word文档查阅功能,集成app阅读word功能也可以通过几种方式实现
例如:购买专门的sdk包,像Aspose等(money啊)或者服务器端处理成图片或者html,然后android端去请求访问等方式。对于大部分个人开发者而言,这两种方式就显得比较重量级了。
下面介绍两种专门解析docx文件的方式:docx4j 以及poi

Docx4j

Docx4j
github地址:https://github.com/plutext/AndroidDocxToHtml
这个是官网demo,基本可以直接使用,解析出来的格式比较全,样式也比较接近原文档,就是解析速度令人不敢恭维,手机上测试的话,一般一份儿docx文档都需要30s以上甚至更多,有时候测试文档明明就只有几十k大小而已,对于比较大,比较复杂的文档,时间就更是让人崩溃。解析速度不是令人满意。

解析测试中遇到的bug
1.表格丢失,内容丢失:内嵌表格(表格中还有表格的这种)的内容和样式会有部分丢失现象
2.表格(又是我?)样式:假如文档中的表格在word文档中排版时超出了该文档的边界线,你会发现超出边界的内容又不见了
3.目录乱码:如果文档中有目录,目录会被加上一些超链接,需要手工处理去掉
4.图片无法解析:有一些格式的图片无法解析,比如EMF,WMF这种类型的
5.批注无法显示:目前没有找到批注显示的地方,暂且算丢失吧,后面在试试
6.。。。其它暂时还没被发现的问题

POI

POI
poi是apache的一个开源项目,不多说,直接上官网去下载就可以
官网地址:http://poi.apache.org/
如果你是android studio用户:那就很简单了
只需要引入依赖(版本号不一定哦,gradle会自己把相关依赖包下载到位):

compile 'fr.opensagres.xdocreport:org.apache.poi.xwpf.converter.xhtml:1.0.5'

那如果你是eclipse用户(伙计,赶紧用studio吧)
需要手工引入以下jar包,包括:

poi , poi-ooxml , ooxml-schema,org.apache.poi.xwpf.converter.xhtml,org.apache.poi.xwpf.converter.core
实现代码如下

 {    
    InputStream is = new FileInputStream(file);
    XWPFDocument docx = new 
          XWPFDocument(is);
    OutputStream os = new ByteArrayOutputStream();
    String imgDesPath = "/sdcard/img";
    File imgFile = new File("/sdcard/img");
    this.baseUrl = this.getDir("image", Context.MODE_PRIVATE).toURL().toString();
    if (!imgFile.exists()) {
        file.mkdirs();
    }

poi解析的问题
速度比docx4j要稍快一点,会有文档内容解析不全样式丢失的情况

流程:
调用接口将docx转化为html,然后app中通过webview加载该html即可显示
转化代码如下(我就想问下,这代码格式到底该怎么调啊~好烦躁):

try { 
    InputStream is = new FileInputStream(file);
    XWPFDocument docx = new 
          XWPFDocument(is);
    OutputStream os = new ByteArrayOutputStream();
    String imgDesPath = "/sdcard/img";
    File imgFile = new File("/sdcard/img");
    this.baseUrl = this.getDir("image", Context.MODE_PRIVATE).toURL().toString();
    if (!imgFile.exists()) {
        file.mkdirs();
    }
    XHTMLOptions options = XHTMLOptions.create().URIResolver(new BasicURIResolver(imgDesPath));
    options.setExtractor(new FileImageExtractor(imgFile));
    options.setIgnoreStylesIfUnused(false);
    options.setFragment(true);
    XHTMLConverter.getInstance().convert(docx, os, options);
    **os.write("/sdcard/xxx/html文件")**
     } catch (Exception e) {
        Log.d(TAG, "catch " + e.getMessage());
    }

webview 里面直接load 上面生成的html文件就可以了

poi将word2007转化成html

1:导入jar包依赖

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.10.1</version>
        </dependency>
       <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
       </dependency>
       <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
            <version>1.0.6</version>
       </dependency>
       <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
            <version>1.0.6</version>
        </dependency>

2:创建工具类,便于后面直接调用即可

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordtoHtml07 {

    public static void word07ToHtml(String fileName ,String imageFile , String htmFile) throws IOException{
        File f = new File(fileName);
        if (!f.exists()) {
            System.out.println("sorry file does not exists");
        }else{
            if (f.getName().endsWith(".docx")|| f.getName().endsWith(".DOCX") || f.getName().endsWith(".doc")) {
                //1:加载文档到XWPFDocument
                InputStream in = new FileInputStream(f);
                XWPFDocument document = new XWPFDocument(in);
                //2:加载图片到指定文件夹
                File imgFile = new File(imageFile);
                XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imgFile));
                options.setExtractor(new FileImageExtractor(imgFile));

                //3:转换XWPFDocument to XHTML 
                  OutputStream out = new FileOutputStream(new File(htmFile));  
                    XHTMLConverter.getInstance().convert(document, out, options);  
            }else{
                  System.out.println("Enter only MS Office 2007+ files");              
            }            
        }            
    }      
    public static void main(String args[]) {  
        try {             
            word07ToHtml("F:/51/1.doc","F:/51/media","F:/51/1.htm");
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }      
}

解析doc并显示

package com.lattice.sodocument.attachment;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lattice.sodocument.main.BaseActivity;
import com.lattice.sodocument.main.R;
import com.lattice.sodocument.util.CompressionOfImage;
import com.lattice.sodocument.util.ConversionUtil;
import com.lattice.sodocument.util.FileUtil;
import com.lattice.sodocument.util.FunctionUtil;
import com.lattice.sodocument.util.LogUtil;

//doc和docx有问题
@SuppressLint({ "SetJavaScriptEnabled", "HandlerLeak" })
public class AttachmentView extends BaseActivity {
    ScrollView scrollView;
    TextView textView;
    ImageView imageView;
    Bitmap bitmap;
    Handler mHandler;
    ProgressBar mBar;
    Runnable runnable = new Runnable() {

        public void run() {
            // 最终版
            bitmap = ConversionUtil.string2Bitmap(attachmentString);
            // 测试版
            // bitmap = BitmapFactory.decodeResource(
            // AttachmentView.this.getResources(), R.drawable.welcome);
            bitmap = CompressionOfImage.comp(bitmap);
            Message message = new Message();
            mHandler.sendMessage(message);
        }
    };
    StringBuffer text = new StringBuffer();
    private Range range = null;
    private HWPFDocument hwpf = null;

    private String htmlPath;
    private String picturePath;

    @SuppressWarnings("rawtypes")
    private List pictures;
    private TableIterator tableIterator;
    private int presentPicture = 0;
    private int screenWidth;
    private FileOutputStream output;
    private File myFile;
    String xml;
    protected WebView mWebView;
    public static final int ERROR = 0;
    public static final int DOC = 1;
    public static final int DOCX = 2;
    public static final int IMAGE = 3;
    public static final int TXT = 4;
    private int attachmentType = 0;
    private String attachmentString = "0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAAQAAAAAAAAAAEAAAAgAAAAEAAAD+AAAAAAAAAAD9BQAAAP7///8EAAAABgAAAP7///8HAAAACAAAAAkAAAAKAAAACwAAAAwAAAANAAAADgAAAA8AAAAQAAAAEQAAABIAAAD+/1IAbwBvAHQAIABFAG4AdAByAHkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWAAUA//8BAAAABgkCAAAAAADAAAAAAAAARgAAAAAAAAAAAAAAAODafPelbc8BAwAAAEAcAAAAAAAABQBTAHUAbQBtAGEAcgB5AEkAbgBmAG8AcgBtAGEAdABpAG8AbgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAAgEEAAAAAgAAAP8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/AEAAAAAAAAFAEQAbwBjAHUAbQBlAG4AdABTAHUAbQBtAGEAcgB5AEkAbgBmAG8AcgBtAGEAdABpAG8AbgAAAAAAAAAAAAAAOAACAf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAADwAQAAAAAAAFcAbwByAGQARABvAGMAdQBtAGUAbgB0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAADMOAAAAAAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAP7///8JAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAA/v///xEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAB0AA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AAAQAAAAAABAAAAAAAmAAAAJ4AAACeAAAAngAAAJ4AAACeAAAAngAAAJ4AAACeAAAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAAdgIAAHYCAAB2AgAAdgIAAHYCAAB2AgAAdgIAAHYCAAB2AgAANgYAADYGAAA2BgAABAAAAAQAAAA2BgAAPgIAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAAKgAAAA2BgAANgYAAAQAAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAALgAAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAABoAQAASAEAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAAA2BgAANgYAADYGAABGAABA8f8CAEYAAAAAAAAAAAAAAAIAY2uHZQAACwAAAGEkAwMkAzEkAAAcAENKFQBQSgEAS0gCAG1ICQRzSAkEbkgECHRIBAgAAAAAAAAAAAAAAAAAAAAAAAAkAEFA8v+hACQAAAgAAAAAAAAAAAYA2J6ki7VrPYRXW1NPAAAAAAAAAAAAAAAAjgAfQAEA8gCOAAAIAAAAAAAAAAACAHWYCXcAAGMADwASZPAAAQBhJAMDJANHJABAJgkkZAAAAAFOxggAAAD/AAABACVkAAAABE/GCAAAA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AAAQQAAAAOAABDAAvwGAAAAH8AAACNAX8BIAAgAP8BAAAIAD8DAQABAP9AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attachment);
        initViews();
        initEvents();
        // Intent intent = getIntent();
        // attachmentType = intent.getIntExtra("attachmentType", 0);
        // attachmentString = intent.getStringExtra("attachmentString");
        switch (attachmentType) {
        case ERROR:
//            toastShow("附件打开错误,请重试");
//            finish();
//            overridePendingTransition(R.anim.activity_in_from_left,
//                    R.anim.activity_out_to_right);
            dialogChoose(new String[]{"1","2","3"});
            break;
        case DOC:
            // 可以用一个handle统一处理所有消息,也可以分开
            final Handler handler2 = new Handler() {
                public void handleMessage(Message msg) {
                    if (msg.what == 1) {
                        mWebView.loadUrl(xml);
                        dialogDismiss();
                    }
                }
            };
            mWebView.setVisibility(View.VISIBLE);
            final byte[] buf = FunctionUtil.stringToByte(attachmentString);
            dialogShow("正在加载中");
            new Thread() {
                public void run() {
                    try {
                        // 测试长的doc文档显示
                        test();
                        // getRange(buf);
                    } catch (Exception e) {
                        e.printStackTrace();
                        toastShow("附件打开错误,请重试");
                        finish();
                        overridePendingTransition(R.anim.activity_in_from_left,
                                R.anim.activity_out_to_right);
                    }
                    makeFile();
                    readAndWrite();
                    xml = "file://" + htmlPath;
                    Message msg = new Message();
                    msg.what = 1;
                    handler2.sendMessage(msg);
                }
            }.start();

            break;
        case DOCX:
            break;

        case IMAGE:
            imageView.setVisibility(View.VISIBLE);
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    imageView.setImageBitmap(bitmap);
                }
            };
            new Thread(runnable).start();
            break;
        case TXT:
            scrollView.setVisibility(View.VISIBLE);
            textView.setVisibility(View.VISIBLE);
            String fileName = android.os.Environment
                    .getExternalStorageDirectory() + "/DCIM/README.txt";
            try {
                FileInputStream fin = new FileInputStream(fileName);
                String string = ConversionUtil.stream2String(fin);
                textView.setText(string);
            } catch (Exception e) {
            }
            break;
        }
    }

    public void onClick(View arg0) {
    }

    protected void initViews() {
        scrollView = (ScrollView) findViewById(R.id.scrollView1);
        textView = (TextView) findViewById(R.id.textView1);
        imageView = (ImageView) findViewById(R.id.testpic);
        mWebView = (WebView) findViewById(R.id.webView1);
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 将图片调整到适合webview的大小
        mWebView.getSettings().setUseWideViewPort(false);
        // 支持缩放
        mWebView.getSettings().setSupportZoom(true);
        // 设置支持各种不同的设备
        mWebView.getSettings()
                .setUserAgentString(
                        "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X;en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334bSafari/531.21.10");

    }

    protected void initEvents() {
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }

        });
        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mWebView.loadUrl(url);
                return true;
            }

            public void onReceivedSslError(WebView view,
                    SslErrorHandler handler, android.net.http.SslError error) {
                handler.proceed();
            }

        });
    }

    protected void onDestroy() {
        super.onDestroy();
        FileUtil.deleteDir(android.os.Environment.getExternalStorageDirectory()
                + "/tempDoc");
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        } else {
            FileUtil.deleteDir(android.os.Environment
                    .getExternalStorageDirectory() + "/tempDoc");
            finish();
            overridePendingTransition(R.anim.activity_in_from_left,
                    R.anim.activity_out_to_right);

        }

        return true;
    }

    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//

    public void makeFile() {
        String sdStateString = android.os.Environment.getExternalStorageState();
        if (sdStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
            try {
                File sdFile = android.os.Environment
                        .getExternalStorageDirectory();
                String path = sdFile.getAbsolutePath() + File.separator
                        + "tempDoc";
                File dirFile = new File(path);
                if (!dirFile.exists()) {
                    dirFile.mkdir();
                }
                File myFile = new File(path + File.separator + "my.html");
                if (!myFile.exists()) {
                    myFile.createNewFile();
                }
                htmlPath = myFile.getAbsolutePath();
            } catch (Exception e) {

            }
        }
    }

    public void makePictureFile() {
        String sdString = android.os.Environment.getExternalStorageState();
        if (sdString.equals(android.os.Environment.MEDIA_MOUNTED)) {
            try {
                File picFile = android.os.Environment
                        .getExternalStorageDirectory();
                String picPath = picFile.getAbsolutePath() + File.separator
                        + "tempDoc";
                File picDirFile = new File(picPath);
                if (!picDirFile.exists()) {
                    picDirFile.mkdir();
                }
                File pictureFile = new File(picPath + File.separator
                        + presentPicture + ".jpg");
                if (!pictureFile.exists()) {
                    pictureFile.createNewFile();
                }
                picturePath = pictureFile.getAbsolutePath();
            } catch (Exception e) {
                System.out.println("PictureFile Catch Exception");
            }
        }
    }

    // 读取word中的内容写到sdcard上的.html文件中
    public void readAndWrite() {
        try {
            myFile = new File(htmlPath);
            output = new FileOutputStream(myFile);
            String head = "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <body>";
            String tagBegin = "<p>";
            String tagEnd = "</p>";
            output.write(head.getBytes());
            int numParagraphs = range.numParagraphs();
            for (int i = 0; i < numParagraphs; i++) {
                Paragraph p = range.getParagraph(i);
                if (p.isInTable()) {
                    int temp = i;
                    if (tableIterator.hasNext()) {
                        String tableBegin = "<table style=\"border-collapse:collapse\" border=1 bordercolor=\"black\">";
                        String tableEnd = "</table>";
                        String rowBegin = "<tr>";
                        String rowEnd = "</tr>";
                        String colBegin = "<td>";
                        String colEnd = "</td>";
                        Table table = tableIterator.next();
                        output.write(tableBegin.getBytes());
                        int rows = table.numRows();

                        for (int r = 0; r < rows; r++) {
                            output.write(rowBegin.getBytes());
                            TableRow row = table.getRow(r);
                            int cols = row.numCells();
                            int rowNumParagraphs = row.numParagraphs();
                            int colsNumParagraphs = 0;
                            for (int c = 0; c < cols; c++) {
                                output.write(colBegin.getBytes());
                                TableCell cell = row.getCell(c);
                                int max = temp + cell.numParagraphs();
                                colsNumParagraphs = colsNumParagraphs
                                        + cell.numParagraphs();
                                for (int cp = temp; cp < max; cp++) {
                                    Paragraph p1 = range.getParagraph(cp);
                                    output.write(tagBegin.getBytes());
                                    writeParagraphContent(p1);
                                    output.write(tagEnd.getBytes());
                                    temp++;
                                }
                                output.write(colEnd.getBytes());
                            }
                            int max1 = temp + rowNumParagraphs;
                            for (int m = temp + colsNumParagraphs; m < max1; m++) {
                                range.getParagraph(m);
                                temp++;
                            }
                            output.write(rowEnd.getBytes());
                        }
                        output.write(tableEnd.getBytes());
                    }
                    i = temp;
                } else {
                    output.write(tagBegin.getBytes());
                    writeParagraphContent(p);
                    output.write(tagEnd.getBytes());
                }
            }
            String end = "</body></html>";
            output.write(end.getBytes());
            output.close();
        } catch (Exception e) {
            System.out.println("readAndWrite Exception");
        }
    }

    // 以段落的形式来往html文件中写内容
    public void writeParagraphContent(Paragraph paragraph) {
        Paragraph p = paragraph;
        int pnumCharacterRuns = p.numCharacterRuns();
        for (int j = 0; j < pnumCharacterRuns; j++) {
            CharacterRun run = p.getCharacterRun(j);
            if (run.getPicOffset() == 0 || run.getPicOffset() >= 1000) {
                if (presentPicture < pictures.size()) {
                    writePicture();
                }
            } else {
                try {
                    // StringBuffer text = new StringBuffer();
                    String text = run.text();
                    // boolean b = run.text().startsWith(" ");
                    // if (b) {
                    // text.append("    ");
                    // }
                    // text.append(run.text());
                    //
                    //
                    // output.write(text.toString().getBytes());
                    if (text.length() >= 2 && pnumCharacterRuns < 2) {
                        output.write(text.getBytes());
                    } else {
                        int size = run.getFontSize();
                        int color = run.getColor();
                        String fontSizeBegin = "<font size=\""
                                + decideSize(size) + "\">";
                        String fontColorBegin = "<font color=\""
                                + decideColor(color) + "\">";
                        String fontEnd = "</font>";
                        String boldBegin = "<b>";
                        String boldEnd = "</b>";
                        String islaBegin = "<i>";
                        String islaEnd = "</i>";
                        output.write(fontSizeBegin.getBytes());
                        output.write(fontColorBegin.getBytes());
                        if (run.isBold()) {
                            output.write(boldBegin.getBytes());
                        }
                        if (run.isItalic()) {
                            output.write(islaBegin.getBytes());
                        }
                        output.write(text.getBytes());

                        if (run.isBold()) {
                            output.write(boldEnd.getBytes());
                        }
                        if (run.isItalic()) {
                            output.write(islaEnd.getBytes());
                        }
                        output.write(fontEnd.getBytes());
                        output.write(fontEnd.getBytes());
                    }

                } catch (Exception e) {
                    System.out.println("Write File Exception");
                }
            }
        }
    }

    // 将word中的图片写入到.jpg文件中
    public void writePicture() {
        Picture picture = (Picture) pictures.get(presentPicture);
        byte[] pictureBytes = picture.getContent();
        Bitmap bitmap = BitmapFactory.decodeByteArray(pictureBytes, 0,
                pictureBytes.length);
        makePictureFile();
        presentPicture++;
        File myPicture = new File(picturePath);
        try {
            FileOutputStream outputPicture = new FileOutputStream(myPicture);
            outputPicture.write(pictureBytes);
            outputPicture.close();
        } catch (Exception e) {
            System.out.println("outputPicture Exception");
        }

        String imageString = "<img src=\"" + picturePath + "\"";
        if (bitmap.getWidth() > screenWidth) {
            imageString = imageString + " " + "width=\"" + screenWidth + "\"";
        }
        imageString = imageString + ">";
        try {
            output.write(imageString.getBytes());
        } catch (Exception e) {
            System.out.println("output Exception");
        }
    }

    // 处理word和html字体的转换
    public int decideSize(int size) {

        if (size >= 1 && size <= 8) {
            return 1;
        }
        if (size >= 9 && size <= 11) {
            return 2;
        }
        if (size >= 12 && size <= 14) {
            return 3;
        }
        if (size >= 15 && size <= 19) {
            return 4;
        }
        if (size >= 20 && size <= 29) {
            return 5;
        }
        if (size >= 30 && size <= 39) {
            return 6;
        }
        if (size >= 40) {
            return 7;
        }
        return 3;

    }

    // 处理word和html颜色的转换
    private String decideColor(int a) {
        int color = a;
        switch (color) {
        case 1:
            return "#000000";
        case 2:
            return "#0000FF";
        case 3:
        case 4:
            return "#00FF00";
        case 5:
        case 6:
            return "#FF0000";
        case 7:
            return "#FFFF00";
        case 8:
            return "#FFFFFF";
        case 9:
            return "#CCCCCC";
        case 10:
        case 11:
            return "#00FF00";
        case 12:
            return "#080808";
        case 13:
        case 14:
            return "#FFFF00";
        case 15:
            return "#CCCCCC";
        case 16:
            return "#080808";
        default:
            return "#000000";
        }
    }

    private void getRange(byte[] buffer2) throws Exception {
        // byte[] buffer = null;
        // try {
        // // String xml =
        // "file://"+android.os.Environment.getExternalStorageDirectory()
        // // + "/DCIM/yui.doc";
        // String xml= "file://"+android.os.Environment
        // .getExternalStorageDirectory().getAbsolutePath()+ File.separator
        // + "DCIM/yui.doc";
        // LogUtil.logMessage(xml);
        // File file2 = new File(xml);
        // FileInputStream fis = new FileInputStream(file2);
        // ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        // byte[] b = new byte[1000];
        // int n;
        // while ((n = fis.read(b)) != -1) {
        // bos.write(b, 0, n);
        // }
        // fis.close();
        // bos.close();
        // buffer = bos.toByteArray();
        // } catch (Exception e) {
        // }

        // 将byte数组转为inputstream对象,直接读入内存,不写入硬盘
        InputStream is = new ByteArrayInputStream(buffer2);
        POIFSFileSystem pfs = null;
        try {
            pfs = new POIFSFileSystem(is);
            hwpf = new HWPFDocument(pfs);
        } catch (Exception e) {

        }
        range = hwpf.getRange();
        pictures = hwpf.getPicturesTable().getAllPictures();
        tableIterator = new TableIterator(range);
    }

    private void test() throws Exception {
        byte[] buffer = null;
        try {
            String xml = android.os.Environment.getExternalStorageDirectory()
                    + "/DCIM/y.doc";
            LogUtil.log(xml);
            File file2 = new File(xml);
            FileInputStream fis = new FileInputStream(file2);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (Exception e) {
        }

        // 将byte数组转为inputstream对象,直接读入内存,不写入硬盘
        InputStream is = new ByteArrayInputStream(buffer);
        POIFSFileSystem pfs = null;
        try {
            pfs = new POIFSFileSystem(is);
            hwpf = new HWPFDocument(pfs);
        } catch (Exception e) {

        }
        range = hwpf.getRange();
        pictures = hwpf.getPicturesTable().getAllPictures();
        tableIterator = new TableIterator(range);
    }
}

使用POI读写word doc文件

(注:本文是基于poi3.9所写)Java好像
Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的。在hwpf里面我们使用HWPFDocument来表示一个word doc文档。在HWPFDocument里面有这么几个概念:
Range:它表示一个范围,这个范围可以是整个文档,也可以是里面的某一小节(Section),也可以是某一个段落(Paragraph),还可以是拥有共同属性的一段文本(CharacterRun)。
Section: word文档的一个小节,一个word文档可以由多个小节构成。
Paragraph: word文档的一个段落,一个小节可以由多个段落构成。
l CharacterRun: 具有相同属性的一段文本,一个段落可以由多个CharacterRun组成。
l Table:一个表格。
l TableRow:表格对应的行。
l TableCell:表格对应的单元格。
Section、Paragraph、CharacterRun和Table都继承自Range。
1 读word doc文件

在日常应用中,我们从word文件里面读取信息的情况非常少见,更多的还是把内容写入到word文件中。使用POI从word doc文件读取数据时主要有两种方式:通过WordExtractor读和通过HWPFDocument读。在WordExtractor内部进行信息读取时还是通过HWPFDocument来获取的。

1.1 通过WordExtractor读文件
在使用WordExtractor读文件时我们只能读到文件的文本内容和基于文档的一些属性,至于文档内容的属性等是无法读到的。如果要读到文档内容的属性则需要使用HWPFDocument来读取了。下面是使用WordExtractor读取文件的一个示例:

public class HwpfTest {  

   @SuppressWarnings("deprecation")  
   @Test  
   public void testReadByExtractor() throws Exception {  
      InputStream is = new FileInputStream("D:\\test.doc");  
      WordExtractor extractor = new WordExtractor(is);  
      //输出word文档所有的文本  
      System.out.println(extractor.getText());  
      System.out.println(extractor.getTextFromPieces());  
      //输出页眉的内容  
      System.out.println("页眉:" + extractor.getHeaderText());  
      //输出页脚的内容  
      System.out.println("页脚:" + extractor.getFooterText());  
      //输出当前word文档的元数据信息,包括作者、文档的修改时间等。  
      System.out.println(extractor.getMetadataTextExtractor().getText());  
      //获取各个段落的文本  
      String paraTexts[] = extractor.getParagraphText();  
      for (int i=0; i<paraTexts.length; i++) {  
         System.out.println("Paragraph " + (i+1) + " : " + paraTexts[i]);  
      }  
      //输出当前word的一些信息  
      printInfo(extractor.getSummaryInformation());  
      //输出当前word的一些信息  
      this.printInfo(extractor.getDocSummaryInformation());  
      this.closeStream(is);  
   }  

   /** 
    * 输出SummaryInfomation 
    * @param info 
    */  
   private void printInfo(SummaryInformation info) {  
      //作者  
      System.out.println(info.getAuthor());  
      //字符统计  
      System.out.println(info.getCharCount());  
      //页数  
      System.out.println(info.getPageCount());  
      //标题  
      System.out.println(info.getTitle());  
      //主题  
      System.out.println(info.getSubject());  
   }  

   /** 
    * 输出DocumentSummaryInfomation 
    * @param info 
    */  
   private void printInfo(DocumentSummaryInformation info) {  
      //分类  
      System.out.println(info.getCategory());  
      //公司  
      System.out.println(info.getCompany());  
   }  

   /** 
    * 关闭输入流 
    * @param is 
    */  
   private void closeStream(InputStream is) {  
      if (is != null) {  
         try {  
            is.close();  
         } catch (IOException e) {  
            e.printStackTrace();  
         }  
      }  
   }  

}  

1.2 通过HWPFDocument读文件
HWPFDocument是当前Word文档的代表,它的功能比WordExtractor要强。通过它我们可以读取文档中的表格、列表等,还可以对文档的内容进行新增、修改和删除操作。只是在进行完这些新增、修改和删除后相关信息是保存在HWPFDocument中的,也就是说我们改变的是HWPFDocument,而不是磁盘上的文件。如果要使这些修改生效的话,我们可以调用HWPFDocument的write方法把修改后的HWPFDocument输出到指定的输出流中。这可以是原文件的输出流,也可以是新文件的输出流(相当于另存为)或其它输出流。下面是一个通过HWPFDocument读文件的示例:

public class HwpfTest {  

   @Test  
   public void testReadByDoc() throws Exception {  
      InputStream is = new FileInputStream("D:\\test.doc");  
      HWPFDocument doc = new HWPFDocument(is);  
      //输出书签信息  
      this.printInfo(doc.getBookmarks());  
      //输出文本  
      System.out.println(doc.getDocumentText());  
      Range range = doc.getRange();  
//    this.insertInfo(range);  
      this.printInfo(range);  
      //读表格  
      this.readTable(range);  
      //读列表  
      this.readList(range);  
      //删除range  
      Range r = new Range(2, 5, doc);  
      r.delete();//在内存中进行删除,如果需要保存到文件中需要再把它写回文件  
      //把当前HWPFDocument写到输出流中  
      doc.write(new FileOutputStream("D:\\test.doc"));  
      this.closeStream(is);  
   }  

   /** 
    * 关闭输入流 
    * @param is 
    */  
   private void closeStream(InputStream is) {  
      if (is != null) {  
         try {  
            is.close();  
         } catch (IOException e) {  
            e.printStackTrace();  
         }  
      }  
   }  

   /** 
    * 输出书签信息 
    * @param bookmarks 
    */  
   private void printInfo(Bookmarks bookmarks) {  
      int count = bookmarks.getBookmarksCount();  
      System.out.println("书签数量:" + count);  
      Bookmark bookmark;  
      for (int i=0; i<count; i++) {  
         bookmark = bookmarks.getBookmark(i);  
         System.out.println("书签" + (i+1) + "的名称是:" + bookmark.getName());  
         System.out.println("开始位置:" + bookmark.getStart());  
         System.out.println("结束位置:" + bookmark.getEnd());  
      }  
   }  

   /** 
    * 读表格 
    * 每一个回车符代表一个段落,所以对于表格而言,每一个单元格至少包含一个段落,每行结束都是一个段落。 
    * @param range 
    */  
   private void readTable(Range range) {  
      //遍历range范围内的table。  
      TableIterator tableIter = new TableIterator(range);  
      Table table;  
      TableRow row;  
      TableCell cell;  
      while (tableIter.hasNext()) {  
         table = tableIter.next();  
         int rowNum = table.numRows();  
         for (int j=0; j<rowNum; j++) {  
            row = table.getRow(j);  
            int cellNum = row.numCells();  
            for (int k=0; k<cellNum; k++) {  
                cell = row.getCell(k);  
                //输出单元格的文本  
                System.out.println(cell.text().trim());  
            }  
         }  
      }  
   }  

   /** 
    * 读列表 
    * @param range 
    */  
   private void readList(Range range) {  
      int num = range.numParagraphs();  
      Paragraph para;  
      for (int i=0; i<num; i++) {  
         para = range.getParagraph(i);  
         if (para.isInList()) {  
            System.out.println("list: " + para.text());  
         }  
      }  
   }  

   /** 
    * 输出Range 
    * @param range 
    */  
   private void printInfo(Range range) {  
      //获取段落数  
      int paraNum = range.numParagraphs();  
      System.out.println(paraNum);  
      for (int i=0; i<paraNum; i++) {  
//       this.insertInfo(range.getParagraph(i));  
         System.out.println("段落" + (i+1) + ":" + range.getParagraph(i).text());  
         if (i == (paraNum-1)) {  
            this.insertInfo(range.getParagraph(i));  
         }  
      }  
      int secNum = range.numSections();  
      System.out.println(secNum);  
      Section section;  
      for (int i=0; i<secNum; i++) {  
         section = range.getSection(i);  
         System.out.println(section.getMarginLeft());  
         System.out.println(section.getMarginRight());  
         System.out.println(section.getMarginTop());  
         System.out.println(section.getMarginBottom());  
         System.out.println(section.getPageHeight());  
         System.out.println(section.text());  
      }  
   }  

   /** 
    * 插入内容到Range,这里只会写到内存中 
    * @param range 
    */  
   private void insertInfo(Range range) {  
      range.insertAfter("Hello");  
   }  

}  

2 写word doc文件

   在使用POI写word doc文件的时候我们必须要先有一个doc文件才行,因为我们在写doc文件的时候是通过HWPFDocument来写的,而HWPFDocument是要依附于一个doc文件的。所以通常的做法是我们先在硬盘上准备好一个内容空白的doc文件,然后建立一个基于该空白文件的HWPFDocument。之后我们就可以往HWPFDocument里面新增内容了,然后再把它写入到另外一个doc文件中,这样就相当于我们使用POI生成了word doc文件。
   在实际应用中,我们在生成word文件的时候都是生成某一类文件,该类文件的格式是固定的,只是某些字段不一样罢了。所以在实际应用中,我们大可不必将整个word文件的内容都通过HWPFDocument生成。而是先在磁盘上新建一个word文档,其内容就是我们需要生成的word文件的内容,然后把里面一些属于变量的内容使用类似于“${paramName}”这样的方式代替。这样我们在基于某些信息生成word文件的时候,只需要获取基于该word文件的HWPFDocument,然后调用Range的replaceText()方法把对应的变量替换为对应的值即可,之后再把当前的HWPFDocument写入到新的输出流中。这种方式在实际应用中用的比较多,因为它不但可以减少我们的工作量,还可以让文本的格式更加的清晰。下面我们就来基于这种方式做一个示例。
   假设我们现在拥有一些变动的信息,然后需要通过这些信息生成如下格式的word doc文件:

这里写图片描述
那么根据上面的描述,首先第一步,我们建立一个对应格式的doc文件作为模板,其内容是这样的
这里写图片描述
有了这样一个模板之后,我们就可以建立对应的HWPFDocument,然后替换对应的变量为相应的值,再把HWPFDocument输出到对应的输出流即可。下面是对应的代码。

public class HwpfTest {  

   @Test  
   public void testWrite() throws Exception {  
      String templatePath = "D:\\word\\template.doc";  
      InputStream is = new FileInputStream(templatePath);  
      HWPFDocument doc = new HWPFDocument(is);  
      Range range = doc.getRange();  
      //把range范围内的${reportDate}替换为当前的日期  
      range.replaceText("${reportDate}", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));  
      range.replaceText("${appleAmt}", "100.00");  
      range.replaceText("${bananaAmt}", "200.00");  
      range.replaceText("${totalAmt}", "300.00");  
      OutputStream os = new FileOutputStream("D:\\word\\write.doc");  
      //把doc输出到输出流中  
      doc.write(os);  
      this.closeStream(os);  
      this.closeStream(is);  
   }  

   /** 
    * 关闭输入流 
    * @param is 
    */  
   private void closeStream(InputStream is) {  
      if (is != null) {  
         try {  
            is.close();  
         } catch (IOException e) {  
            e.printStackTrace();  
         }  
      }  
   }  

   /** 
    * 关闭输出流 
    * @param os 
    */  
   private void closeStream(OutputStream os) {  
      if (os != null) {  
         try {  
            os.close();  
         } catch (IOException e) {  
            e.printStackTrace();  
         }  
      }  
   }  


}  

POI 读取word (word 2003 和 word 2007)

最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97、2003、2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 97 基本上已经退出市场,几乎没有人用这个版本了, 所以在我们的系统中只考虑 2003 版本和 2007 版本的,因为我们只要求能够读取 word 中的文字内容即可,其中的文字样式、图片等信息可以忽略,也不用直接操作 word 文件, 所以我们选择 用 apache 的 POI 进行读取。

读取 2003 版本(.doc)的word文件相对来说比较简单,只需要 poi-3.5-beta6-20090622.jar 和 poi-scratchpad-3.5-beta6-20090622.jar 两个 jar 包即可, 而 2007 版本(.docx)就麻烦多,我说的这个麻烦不是我们写代码的时候麻烦,是要导入的 jar 包比较的多,有如下 7 个之多:

1. openxml4j-bin-beta.jar
2. poi-3.5-beta6-20090622.jar
3. poi-ooxml-3.5-beta6-20090622.jar
4 .dom4j-1.6.1.jar
5. geronimo-stax-api_1.0_spec-1.0.jar
6. ooxml-schemas-1.0.jar
7. xmlbeans-2.3.0.jar
其中 4-7 是 poi-ooxml-3.5-beta6-20090622.jar 所依赖的 jar 包(在 poi-bin-3.5-beta6-20090622.tar.gz 中的 ooxml-lib 目录下可以找到)。

编写代码之前我们得先下载所需要的 jar 包, 我们只需下载 poi-bin-3.5-beta6-20090622.tar.gz 和 openxml4j-bin-beta.jar 即可,因为所需要的其他 jar 包都能在 poi-bin-3.5-beta6-20090622.tar.gz 中找到, 下面是下载地址:

poi-bin-3.5-beta6-20090622.tar.gz:http://apache.etoak.com/poi/dev/bin/poi-bin-3.5-beta6-20090622.tar.gz
openxml4j-bin-beta.jar:http://mirror.optus.net/sourceforge/o/op/openxml4j/openxml4j-bin-beta.jar

下方是读取 word 文件的 Java 代码,值得注意的是: POI 在读取 word 文件的时候不会读取 word 文件中的图片信息, 还有就是对于 2007 版的 word(.docx), 如果 word 文件中有表格,所有表格中的数据都会在读取出来的字符串的最后。
import java.io.File;  
import java.io.FileInputStream;  
import java.io.InputStream;  

import org.apache.poi.POIXMLDocument;  
import org.apache.poi.POIXMLTextExtractor;  
import org.apache.poi.hwpf.extractor.WordExtractor;  
import org.apache.poi.openxml4j.opc.OPCPackage;  
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;  

/** 
 * POI 读取 word 2003 和 word 2007 中文字内容的测试类<br /> 
 * @createDate 2009-07-25 
 * @author Carl He 
 */  
public class Test {  
    public static void main(String[] args) {  
        try {  
            //word 2003: 图片不会被读取  
              InputStream is = new FileInputStream(new File("c://files//2003.doc"));  
            WordExtractor ex = new WordExtractor(is);  
            String text2003 = ex.getText();  
            System.out.println(text2003);  

            //word 2007 图片不会被读取, 表格中的数据会被放在字符串的最后  
            OPCPackage opcPackage = POIXMLDocument.openPackage("c://files//2007.docx");  
            POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);  
            String text2007 = extractor.getText();  
            System.out.println(text2007);  

        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

如果想下载完整的示例代码,可以到这里下载,这个 rar 包中有 POI 读取word 2003 和 word 2007 所需要的全部 jar 包 和 word 2003、word 2007 示例文件。

全面了解POI操作Microsoft Office(Word、Excel、PowerPoint)

  1. POI 简介
    POI 是 Apache 下的 Jakata 项目的一个子项目,主要用于提供 java 操作 Microsoft

Office 办公套件如 Excel,Word,Powerpoint 等文件的 API.

 微软的Office 办公软件在企业的日常办公中占据着重要的地位,人们已经非常熟悉

Office 的使用。在我们开发的应用系统中,常常需要将数据导出到 Excel 文件中,或者

Word 文件中进行打印。比如移动的话费查询系统中就提供了将话费清单导入到 excel 表

格中的功能。这样在web 应用中,我们在浏览器中看到的数据可以被导出到 Excel 中了。

Excel 文件: xls 格式文件对应 POI API 为 HSSF 。 xlsx 格式为 office 2007 的文件格式,POI 中对应的API 为XSSF

Word 文件:doc 格式文件对应的 POI API 为 HWPF。 docx 格式为 XWPF

powerPoint 文件:ppt 格式对应的 POI API 为 HSLF。 pptx 格式为 XSLF

outlook :对应的 API 为 HSMF

Visio: 对应的 API 为 HDGF

Publisher : 对应的 API 为 HPBF

下面主要介绍如何操作Excel。
…………………..
……………………………

POI处理Word、Excel、PowerPoint 简单例子

第一:下载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;

publicclass Word {

publicstaticvoid main(String[] args)throws Exception {

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

}



publicstatic String getContent(String s)throws Exception {

   returngetContent(new java.io.FileInputStream(s));

}



publicstatic String getContent(File f)throws Exception {

   returngetContent(new java.io.FileInputStream(f));

}



publicstatic 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;

publicclassExcel {

publicstaticvoid main(String[] args)throws Exception {

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

}



publicstatic String getContent(String s)throws Exception {

   returngetContent(new java.io.FileInputStream(s));

}



publicstatic String getContent(File f)throws Exception {

   returngetContent(new java.io.FileInputStream(f));

}



publicstatic 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());

              } elseif (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;



publicclassPowerPoint {

    publicstaticvoid main(String[] args)throws Exception {

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

    }



    publicstatic String getContent(String s)throws Exception {

       returngetContent(new java.io.FileInputStream(s));

    }



    publicstatic String getContent(File f)throws Exception {

       returngetContent(new java.io.FileInputStream(f));

    }



    publicstatic 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();

    }

}

使用POI读写word docx文件

  • 1
    点赞
  • 0
    评论
  • 6
    收藏
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值