资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

wps格式化JAVA代码 wps格式化文本

能写个读取.wps后缀名文档的Java程序吗?

如下方法,代码思路:加载一个.wps格式的文档,获取文档中的文本或图片,将获取到的内容保存到本地路径。下面以分别读取文本和图片为例:

创新互联一直通过网站建设和网站营销帮助企业获得更多客户资源。 以"深度挖掘,量身打造,注重实效"的一站式服务,以成都做网站、网站建设、移动互联产品、全网营销推广服务为核心业务。十余年网站制作的经验,使用新网站建设技术,全新开发出的标准网站,不但价格便宜而且实用、灵活,特别适合中小公司网站制作。网站管理系统简单易用,维护方便,您可以完全操作网站资料,是中小公司快速网站建设的选择。

1.读取WPS中的文本

import com.spire.doc.*;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

public class ReadTextFromWPS {

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

//通过流加载WPS文字文档

FileInputStream inputStream = new FileInputStream(new File("test.wps"));

Document doc = new Document();

doc.loadFromStream(inputStream, FileFormat.Doc);

//获取文本保存为String

String text = doc.getText();

//将String写入Txt

writeStringToTxt(text,"读取WPS文本.txt");

}

public static void writeStringToTxt(String content, String txtFileName) throws IOException {

FileWriter fWriter= new FileWriter(txtFileName,true);

try {

fWriter.write(content);

}catch(IOException ex){

ex.printStackTrace();

}finally{

try{

fWriter.flush();

fWriter.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}

2.读取WPS中的图片

import com.spire.doc.*;

import com.spire.doc.documents.DocumentObjectType;

import com.spire.doc.fields.DocPicture;

import com.spire.doc.interfaces.ICompositeObject;

import com.spire.doc.interfaces.IDocumentObject;

import javax.imageio.ImageIO;

import java.awt.image.RenderedImage;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

import java.util.Queue;

public class ExtractImgFromWPS {

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

//通过流加载WPS文字文档

FileInputStream inputStream = new FileInputStream(new File("test.wps"));

Document doc = new Document();

doc.loadFromStream(inputStream, FileFormat.Doc);

//创建Queue对象

Queue nodes = new LinkedList();

nodes.add(doc);

//创建List对象

List images = new ArrayList();

//遍历文档中的子对象

while (nodes.size() 0)

{

ICompositeObject node = (ICompositeObject) nodes.poll();

for (int i = 0; i node.getChildObjects().getCount(); i++)

{

IDocumentObject child = node.getChildObjects().get(i);

if (child instanceof ICompositeObject)

{

nodes.add(child);

//获取图片并添加到List

if (child.getDocumentObjectType() == DocumentObjectType.Picture)

{

DocPicture picture = (DocPicture) child;

images.add(picture.getImage());

}

}

}

}

//将图片保存为PNG格式文件

for (int i = 0; i images.size(); i++)

{

File file = new File(String.format("WPSImg-%d.png", i));

ImageIO.write((RenderedImage) images.get(i), "PNG", file);

}

}

}

读取时,注意在Java程序中导入spire.doc.jar。

用java将数据导出到wps表格中,怎么实现

一:页面图片显示预览:

1)如下图:

2)点击导出按钮后预览:

3)最终生成的excel表格图片预览:

二:代码演示:

说明:执行操作时,请先引进导出excel表格的jar文件包。

找到导出按钮所执行的js方法,在java后天查看该方法的实现即可。

1)jsp代码:

[html] view plaincopyprint?

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%

%@taglib prefix="s" uri="/struts-tags" %

%

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%

html

head

base href="%=basePath%"

title驾校合格率排名/title

link href="jsp/commonstyle/css/tabStyle.css" rel="stylesheet" type="text/css"

link rel="STYLESHEET" type="text/css" href="%=basePath%jsp/hgltj/js/tablesort.css"

script type="text/javascript" src="%=basePath%jsp/system/common/js/publicColor.js"/script

script type="text/javascript" src="jsp/commonstyle/js/js/My97DatePicker/WdatePicker.js" defer="defer"/script

script type="text/javascript" src="%=basePath%jsp/hgltj/js/tablesort.js"/script

script language="JavaScript"

function load()

{

//根据分辨率设置表格大小

maxw=document.getElementById("maintb").offsetWidth;

if(maxw824){//1024分辨率未展开

mainbox.width="98%";

} else if(maxw1013){//1024分辨率展开

mainbox.width="98%";

} else  if(maxw1081){//1280分辨率未展开

mainbox.width="95%";

} else  if(maxw1270){//1280分辨未展开

mainbox.width="95%";

}else{//1280以上分辨展开

mainbox.width="98%";

}

}

/script

script

function overIt(){

var the_obj = event.srcElement;

if(the_obj.tagName.toLowerCase() == "td"){

the_obj=the_obj.parentElement;

the_obj.oBgc=the_obj.currentStyle.backgroundColor;

the_obj.oFc=the_obj.currentStyle.color;

the_obj.style.backgroundColor='#4073C4';

the_obj.style.color='#ffffff';

the_obj.style.textDecoration='underline';

}

}

function outIt(){

var the_obj = event.srcElement;

if(the_obj.tagName.toLowerCase() == "td"){

the_obj=the_obj.parentElement;

the_obj.style.backgroundColor=the_obj.oBgc;

the_obj.style.color=the_obj.oFc;

the_obj.style.textDecoration='';

}

}

function serch(){

document.getElementById("formName").action="%=basePath %hgltj.action?method=getHglpm";

document.getElementById("formName").submit();

}

function tbbt(){

var jzrq=document.getElementById("jzrqId").value;

//var jxmc=document.getElementById("jxmcId").value;

window.open('%=basePath %hgltj.action?method=getHglpmTbtjjxkshgl.jzrq='+jzrq+'tjjxkshgl.zt='+1,'','height=650,width=1250,top=150,left=200,toobar=no,menubar=no,scrollbars=yes,resizable=no,location=no,');

}

function openwd(){

document.getElementById("formName").action="%=basePath %hgltj.action?method=downJxhglPm";

document.getElementById("formName").submit();

}

/script

/head

BODY onLoad="load()" style="background: url(images/cont_bg.gif); background-repeat: repeat-y"

input type="hidden" name="method" value="getDriverInfoList"/

table border="0" width="100%" cellspacing="0" cellpadding="0" id="maintb"

tr

td align="center"

table width="90%"  border="0"  cellspacing="0" cellpadding="0" id="mainbox" style="background:url(jsp/commonstyle/images/usermessage_02.gif); background-repeat:repeat-x;" 

!--宽度可变内容框--

tr 

td width="33" height="27" style=" background:url(jsp/commonstyle/images/usermessage_01.gif); background-position:left; background-repeat:no-repeat;" /td

td width="965" class="style1"font color="black"驾校合格率排名/font/td

td width="14" height="27" style=" background:url(jsp/commonstyle/images/usermessage_03.gif); background-position:right; background-repeat:no-repeat;" /td

/tr

tr

td colspan="3"

!--页面主体内容开始--

!--查询条件--

form action="" name="formName" method="post" style="margin:0px" id="formName" theme="simple"

table width="100%" border="0" cellpadding="0" cellspacing="0"   style="border-collapse:collapse;" id="tj" align="center"

tr align="left"

td class="tjbg1" style="text-align: left"

!-- input type="hidden" id="method"  name="method" value="getHglpm"/ --

统计日期:

input type="text" name="tjjxkshgl.jzrq" id="jzrqId" value="s:property value="tjjxkshgl.jzrq"/" onclick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM'})"/

!-- 驾校名称:

s:select id="jxmcId"  name="tjjxkshgl.jxxh"  value="tjjxkshgl.jxxh" list="schoolList" listKey="jxxh" listValue="jxmc" headerKey="" headerValue="--请选择--" theme="simple"/s:select        --

input name="input" value=" 统 计 " type="button" class="normalbtn" onClick="serch()" style="margin-bottom: 5px" /

input name="input" type="button" value=" 合格率图表 " onclick="tbbt()" class="normalbtn" style="margin-bottom: 5px" /

input id="Button1" type="button" value=" 导 出 " onclick="openwd();" class="normalbtn" style="margin-bottom: 5px" /

/td

/tr

/table!--查询结果--

/form

!--查询结果--

table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#abcfff" id="cxjg" align="center"

thead

tr class="tbtitle"

td width="4%" align="center" class="t1"名次/td

td width="8%" align="center" class="t1"名称/td

td class="t1" width="5%" align="center"科目一/td

td class="t1" width="5%" align="center"科目二/td

td class="t1" width="5%" align="center"科目三/td

td class="t1" width="5%" align="center"平均合格率/td

td class="t1" width="5%" align="center"操作/td

/tr

/thead

s:iterator id="jxhgl" value="jxhelpmList" status="st"

tr class="changeColor" onMouseOver="overIt()" onMouseOut="outIt()" style="cursor: hand" align="center"

tds:property value="#st.index+1"//td

tds:property value="#jxhgl.jxmc"//td

tds:property value="#jxhgl.km1hgl"//td

tds:property value="#jxhgl.km2hgl"//td

tds:property value="#jxhgl.km3hgl"//td

tds:property value="#jxhgl.avghgl"/%/td

td

a href="javascript:" onclick="openWin('%=basePath %hgltj.action?method=getTbForJxxhtjjxkshgl.jxxh=s:property value="#jxhgl.jxxh"/tjjxkshgl.zt=1','',1250,750);"图表/a

/td

/tr

/s:iterator

/table

/table

/td

/tr

/table

/body

/html

2)java代码演示:

[java] view plaincopyprint?

/**

* 驾校合格率导出excel图表

*/

//response.getOutputStream();// 取得输出流

response.reset();// 清空输出流

String tmptitle = "驾校合格率排名"; // 标题

response.setHeader("Content-disposition", "attachment; filename="+new String(tmptitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头

response.setContentType("application/vnd.ms-excel");// 定义输出类型

wbook = Workbook.createWorkbook(os); // 建立excel文件

WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称

// 设置excel标题

//cellFormat.setBackground(Colour.AQUA);

cellFormat.setFont(wfont);

label.setCellFormat(cellFormat);

wsheet.addCell(label);

//wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));

wsheet.setRowView(0,500); //第一行高度

wsheet.mergeCells(0, 0, 6, 1);  //合并单元格(第一列的第一行和第七列的第二行合并)

//wsheet.mergeCells(0, 1, 9, 1);

//      wsheet.mergeCells(0, 2, 0, 4);

//      wsheet.mergeCells(1, 2, 3, 2);

//      wsheet.mergeCells(4, 2, 6, 2);

//      wsheet.mergeCells(7, 2, 9, 2);

wsheet.setColumnView(0,10); //宽度

wsheet.setColumnView(1,25); //宽度

wsheet.setColumnView(2,10); //宽度

wsheet.setColumnView(3,10); //宽度

wsheet.setColumnView(4,10); //宽度

wsheet.setColumnView(5,10); //宽度

// 开始生成主体内容

wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);

wcfFC = new WritableCellFormat(wfont);

wsheet.addCell(new Label(0, 2, "名次",wcfFC));

wsheet.addCell(new Label(1, 2, "驾校名称",wcfFC));

wsheet.addCell(new Label(2, 2, "科目一",wcfFC));

wsheet.addCell(new Label(3, 2, "科目二",wcfFC));

wsheet.addCell(new Label(4, 2, "科目三",wcfFC));

wsheet.addCell(new Label(5, 2, "合格率",wcfFC));

int count=jxhelpmList.size();

if(count0){  ////判断集合是否不为0

TjJxkshgl tjhgl=null;

for(int i=0;ijxhelpmList.size();i++){

tjhgl=(TjJxkshgl)jxhelpmList.get(i);

wsheet.addCell(new Label(0, i+3, (i+1)+""));

wsheet.addCell(new Label(1, i+3, tjhgl.getJxmc()));

wsheet.addCell(new Label(2, i+3, tjhgl.getKm1hgl()));

wsheet.addCell(new Label(3, i+3, tjhgl.getKm2hgl()));

wsheet.addCell(new Label(4, i+3, tjhgl.getKm3hgl()));

wsheet.addCell(new Label(5, i+3, tjhgl.getAvghgl()));

}

您好,我想用java代码调用wps?

代码方法如下,从流加载wps文件,转为pdf格式

import com.spire.doc.*;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

public class WordToPDF {

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

FileInputStream inputStream = new FileInputStream(new File("test.wps"));

Document document = new Document();

document.loadFromStream(inputStream, FileFormat.Doc);

document.saveToFile("WPStoPDF.pdf",FileFormat.PDF);

}

}

在程序中需引入 spire.doc.jar。


文章名称:wps格式化JAVA代码 wps格式化文本
网站地址:http://cdkjz.cn/article/doohhsj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220