使用Java读取Word文件的简单例子分享

网友投稿 369 2023-07-27


使用Java读取Word文件的简单例子分享

public String bin2hex(String bin) {

char[] digital = "0123456789ABCDEF".toCharArray();

StringBuffer sb = new StringBuffer("");

byte[] bs = bin.getBytes();

int bit;

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

bit = (bs[i] & 0x0f0) >> 4;

sb.append("\\'");

sb.append(digital[bit]);

bit = bs[i] & 0x0f;

sb.append(digital[bit]);

}

return sb.toString();

}

public String readByteRtf(InputStream ins, String path){

String sourcecontent = "";

try{

ins = new FileInputStream(path);

byte[] b = new byte[1024];

if (ins == null) {

System.out.println("源模板文件不存在");

}

int bytesRead = 0;

while (true) {

bytesRead = ins.read(b, 0, 1024); // return final read bytes counts

if(bytesRead == -1) {// end of InputStream

System.out.println("读取模板文件结束");

break;

}

sourcecontent += new String(b, 0, bytesRead); // convert to string using bytes

}

}catch(Exception e){

e.printStackTrace();

}

return sourcecontent ;

}

以上为核心代码,剩余部分就是替换,从新组装java中的String.replace(oldstr,newstr);方法可以实现,在这就不贴了。源代码部分详见附件。

运行源代码前提:

c盘创建YQ目录,将附件中"模板.rtf"复制到YQ目录之下,运行OpreatorRTF.java文件即可,就会在YQ目录下生成文件名如:21时15分19秒_cheney_记录.rtf 的文件。

pahttp://ckage com;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import java.util.Date;

public class OperatorRTF {

public String strToRtf(String content){

char[] digital = "0123456789ABCDEF".toCharArray();

StringBuffer sb = new StringBuffer("");

byte[] bs = content.getBytes();

int bit;

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

bit = (bs[i] & 0x0f0) >> 4;

sb.append("\\'");

sb.append(digital[bit]);

bit = bs[i] & 0x0f;

sb.append(digital[bit]);

}

return sb.toString();

}

public String replaceRTF(String content,String replacecontent,int flag){

String rc = strToRtf(replacecontent);

String target = "";

if(flag==0){

target = content.replace("$timetop$",rc);

}

if(flag==1){

target = content.replace("$info$",rc);

}

if(flag==2){

target = content.replace("$idea$",rc);

}

if(flag==3){

target = content.replace("$advice$",rc);

}

if(flag==4){

target = content.replace("$infosend$",rc);

}

return target;

}

public String getSavePath() {

String path = "C:\\YQ";

File fDirecotry = new File(path);

if (!fDirecotry.exists()) {

fDirecotry.mkdirs();

}

return path;

}

public String ToSBC(String input){

char[] c = input.toCharArray();

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

if (c[i] == 32){

c[i] = (char) 12288;

continue;

}

if (c[i] < 127){

c[i] = (char) (c[i] + 65248);

}

}

return new String(c);

}

public void rgModel(String username, String content) {

// TODO Auto-generated method stub

Date current=new Date();

SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String targetname = sdf.format(current).substring(11,13) + "时";

targetname += sdf.format(current).substring(14,16) + "分";

targetname += sdf.format(current).substring(17,19) + "秒";

targetname += "_" + username +"_记录.rtf";

String strpath = getSavePath();

String sourname = strpath+"\\"+"模板.rtf";

String sourcecontent = "";

InputStream ins = null;

try{

ins = new FileInputStream(sourname);

byte[] b = new byte[1024];

if (ins == null) {

System.out.println("源模板文件不存在");

}

int bytesRead = 0;

while (true) {

bytesRead = ins.read(b, 0, 1024); // return final read bytes counts

if(bytesRead == -1) {// end of InputStream

System.out.println("读取模板文件结束");

break;

}

sourcecontent += new String(b, 0, bytesRead); // convert to string using bytes

}

}catch(Exception e){

e.printStackTrace();

}

String targetcontent = "";

String array[] = content.split("~");

for(int i=0;i

if(i==0){

targetcontent = replaceRTF(sourcecontent, array[i], i);

}else{

targetcontent = replaceRTF(targetcontent, array[i], i);

}

}

try {

FileWriter fw = new FileWriter(getSavePath()+"\\" + targetname,true);

PrintWriter out = new PrintWriter(fw);

if(targetcontent.equals("")||targetcontent==""){

out.println(sourcecontent);

}else{

out.println(targetcontent);

}

out.close();

fw.close();

System.out.println(getSavePath()+" 该目录下生成文件" + targetname + " 成功");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

OperatorRTF oRTF = new OperatorRTF();

String content = "2008年10月12日9时-2008年10月12日6时~我们参照检验药品的方法~我们参照检验药品的方法~我们参照检验药品的方法~我们参照检验药品的方法";

oRTF.rgModel("cheney",content);

}

}

package com.poi.world;

import java.io.FileInputStream;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.usermodel.Paragraph;

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;

public class POI_Word{

public static void main(String[] args){

try {

String[] s=new String[20];

FileInputStream in=new FileInputStream("D:\\mayi.doc");

POIFSFileSystem pfs=new POIFSFileSystem(in);

HWPFDocument hwpf=new HWPFDocument(pfs);

Range range =hwpf.getRange();

TableIterator it=new TableIterator(range);

int index=0;

while(it.hasNext()){

Table tb=(Table)it.next();

for(int i=0;i

//System.out.println("Numrows :"+tb.numRows());

TableRow tr=tb.getRow(i);

for(int j=0;j

//System.out.println("numCells :"+tr.numCells());

// System.out.println("j :"+j);

TableCell td=tr.getCell(j);

for(int k=0;k

//System.out.println("numParagraphs :"+td.numParagraphs());

Paragraph para=td.getParagraph(k);

s[index]=para.text().trim();

index++;

}

}

}

}

// System.out.println(s.toString());

for(int i=0;i

System.out.println(s[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

if(i==0){

targetcontent = replaceRTF(sourcecontent, array[i], i);

}else{

targetcontent = replaceRTF(targetcontent, array[i], i);

}

}

try {

FileWriter fw = new FileWriter(getSavePath()+"\\" + targetname,true);

PrintWriter out = new PrintWriter(fw);

if(targetcontent.equals("")||targetcontent==""){

out.println(sourcecontent);

}else{

out.println(targetcontent);

}

out.close();

fw.close();

System.out.println(getSavePath()+" 该目录下生成文件" + targetname + " 成功");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

OperatorRTF oRTF = new OperatorRTF();

String content = "2008年10月12日9时-2008年10月12日6时~我们参照检验药品的方法~我们参照检验药品的方法~我们参照检验药品的方法~我们参照检验药品的方法";

oRTF.rgModel("cheney",content);

}

}

package com.poi.world;

import java.io.FileInputStream;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.usermodel.Paragraph;

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;

public class POI_Word{

public static void main(String[] args){

try {

String[] s=new String[20];

FileInputStream in=new FileInputStream("D:\\mayi.doc");

POIFSFileSystem pfs=new POIFSFileSystem(in);

HWPFDocument hwpf=new HWPFDocument(pfs);

Range range =hwpf.getRange();

TableIterator it=new TableIterator(range);

int index=0;

while(it.hasNext()){

Table tb=(Table)it.next();

for(int i=0;i

//System.out.println("Numrows :"+tb.numRows());

TableRow tr=tb.getRow(i);

for(int j=0;j

//System.out.println("numCells :"+tr.numCells());

// System.out.println("j :"+j);

TableCell td=tr.getCell(j);

for(int k=0;k

//System.out.println("numParagraphs :"+td.numParagraphs());

Paragraph para=td.getParagraph(k);

s[index]=para.text().trim();

index++;

}

}

}

}

// System.out.println(s.toString());

for(int i=0;i

System.out.println(s[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

//System.out.println("Numrows :"+tb.numRows());

TableRow tr=tb.getRow(i);

for(int j=0;j

//System.out.println("numCells :"+tr.numCells());

// System.out.println("j :"+j);

TableCell td=tr.getCell(j);

for(int k=0;k

//System.out.println("numParagraphs :"+td.numParagraphs());

Paragraph para=td.getParagraph(k);

s[index]=para.text().trim();

index++;

}

}

}

}

// System.out.println(s.toString());

for(int i=0;i

System.out.println(s[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

//System.out.println("numCells :"+tr.numCells());

// System.out.println("j :"+j);

TableCell td=tr.getCell(j);

for(int k=0;k

//System.out.println("numParagraphs :"+td.numParagraphs());

Paragraph para=td.getParagraph(k);

s[index]=para.text().trim();

index++;

}

}

}

}

// System.out.println(s.toString());

for(int i=0;i

System.out.println(s[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

//System.out.println("numParagraphs :"+td.numParagraphs());

Paragraph para=td.getParagraph(k);

s[index]=para.text().trim();

index++;

}

}

}

}

// System.out.println(s.toString());

for(int i=0;i

System.out.println(s[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

System.out.println(s[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:初步解析Java中AffineTransform类的使用
下一篇:如何实现移动端浏览器不显示 pc 端的广告
相关文章

 发表评论

暂时没有评论,来抢沙发吧~