wpsjava
‘壹’ 如何通过java 读取.wps et及 dps文件格式的内容
下面是三个java例子,关于读取wps/et/dps的方法
1.读取wps(读取文本): 通过流加载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. 读取et:直接加载et格式的表格文件,读取数据
import com.spire.xls.*;
public class ExcelToText {
public static void main(String[] args) {
//加载et格式的表格文件
Workbook workbook = new Workbook();
workbook.loadFromFile("test.et");
//获取工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//获取指定单元格中的文本数据
CellRange range = sheet.getCellRange("A1");
String text = range.getText().trim();
System.out.println(text);
}
}
3.读取dps:直接加载dps格式的幻灯片文档,读取文本
import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.ParagraphEx;
import com.spire.presentation.Presentation;
import java.io.FileWriter;
public class ExtractText {
public static void main(String[]args) throws Exception{
//加载测试文档
Presentation ppt = new Presentation();
//ppt.loadFromFile("test.pptx");
ppt.loadFromFile("test.dps");
StringBuilder buffer = new StringBuilder();
//遍历文档中的幻灯片,提取文本
for (Object slide : ppt.getSlides())
{
for (Object shape : ((ISlide) slide).getShapes())
{
if (shape instanceof IAutoShape)
{
for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs())
{
buffer.append(((ParagraphEx) tp).getText());
}
}
}
}
//保存到文本文件
FileWriter writer = new FileWriter("ExtractTextfromDPS.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
}
}
这里须在Java程序中导入spire.office.jar文件。
‘贰’ 关于 wps-office 和 java 的一个小问题.
用jdk进行编译和纯java程序是2个概念
WPS是win32程序内嵌了java代码,它是边执行边解释的,所以你感觉不到速度慢
而你自己编写的java程序执行前都要通过解释器来解释一遍,速度慢就很明显了
‘叁’ ipad中怎么用wps阅读java
在IPAD中安装的WPS,只能打开自己文档中的文件,或者是文档漫游的文档。
其他文档,需要通过电脑,保存到这两个地方后,才能编辑。
‘肆’ java wps将文档转成pdf
看看wps或其他软件用命令行转pdf,用java执行命令行就可以了
‘伍’ wps如何插入Java格式文本
虽然java格式,里面都是纯文本的,记事本打开直接复制就可以啦。
‘陆’ Java读取.wps后缀名文档的代码
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Test {
public static void createDtcxEXCEL(HttpServletResponse response,
java.util.List queryList, int flag, String[] ywName, String fileName)
throws IOException {
response.setContentType("application/vnd.ms-excel;charset=GBK");
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("GBK"), "ISO8859_1"));
OutputStream output = response.getOutputStream();
// 创建新Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell;
sheet = workbook.createSheet();
String strReportName = "查询结列表";
workbook.setSheetName(0, strReportName); // 新建名strReportName工作表
// 创建表
// 索引0位置创建行(顶端行)
row = sheet.createRow((short) 0);
for (int kk = 0; kk < ywName.length; kk++) {
// 索引0位置创建单元格(左端)
cell = row.createCell( kk);
// cell.setCellStyle(HSSFCellStyle.ALIGN_CENTER);
// 定义单元格字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 设置字符显示格式unicode格式显示
//cell.setEncoding(HSSFCell.ENCODING_UTF_16);
// 单元格输入些内容
cell.setCellValue(ywName[kk]);
}
int line = 1;
int cellWidth = ywName.length;
for (int i = 0; i < queryList.size(); i++) {
// HashMap personInfo = (HashMap) queryList.get(i);
Object[] personInfo = (Object[]) queryList.get(i);
row = sheet.createRow((short) line);
for (int j = 0; j < cellWidth; j++) {
cell = row.createCell(j);
//cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
if (personInfo[j + flag] instanceof Integer) {
if (personInfo[j + flag] != null)
cell.setCellValue((Integer) personInfo[j + flag]);
else
cell.setCellValue("");
} else if (personInfo[j + flag] instanceof BigDecimal) {
if (personInfo[j + flag] != null)
cell.setCellValue(new Double(personInfo[j + flag]
.toString()));
else
cell.setCellValue("");
} else if (personInfo[j + flag] instanceof Double) {
if (personInfo[j + flag] != null)
cell.setCellValue((Double) personInfo[j + flag]);
else
cell.setCellValue("");
} else {
if (personInfo[j + flag] != null)
cell.setCellValue(personInfo[j + flag].toString());
else
cell.setCellValue("");
}
}
line++;
}
workbook.write(output);
output.flush();
output.close();
}
}
,
‘柒’ 用java将数据导出到wps表格中,怎么实现
一:页面图片显示预览:
1)如下图:
二:代码演示:
说明:执行操作时,请先引进导出excel表格的jar文件包。
找到导出按钮所执行的js方法,在java后天查看该方法的实现即可。
1)jsp代码:
[html]view plainprint?
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"
+request.getServerName()+":"+request.getServerPort()
+path+"/";
%>
<html>
<head>
<basehref="<%=basePath%>">
<title>驾校合格率排名</title>
<linkhref="jsp/commonstyle/css/tabStyle.css"rel="stylesheet"type="text/css">
<linkrel="STYLESHEET"type="text/css"href="<%=basePath%>jsp/hgltj/js/tablesort.css">
<scripttype="text/javascript"src="<%=basePath%>jsp/system/common/js/publicColor.js"></script>
<scripttype="text/javascript"src="jsp/commonstyle/js/js/My97DatePicker/WdatePicker.js"defer="defer"></script>
<scripttype="text/javascript"src="<%=basePath%>jsp/hgltj/js/tablesort.js"></script>
<scriptlanguage="JavaScript">
functionload()
{
//根据分辨率设置表格大小
maxw=document.getElementById("maintb").offsetWidth;
if(maxw<824){//1024分辨率未展开
mainbox.width="98%";
}elseif(maxw<1013){//1024分辨率展开
mainbox.width="98%";
}elseif(maxw<1081){//1280分辨率未展开
mainbox.width="95%";
}elseif(maxw<1270){//1280分辨未展开
mainbox.width="95%";
}else{//1280以上分辨展开
mainbox.width="98%";
}
}
</script>
<script>
functionoverIt(){
varthe_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';
}
}
functionoutIt(){
varthe_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='';
}
}
functionserch(){
document.getElementById("formName").action="<%=basePath%>hgltj.action?method=getHglpm";
document.getElementById("formName").submit();
}
functiontbbt(){
varjzrq=document.getElementById("jzrqId").value;
//varjxmc=document.getElementById("jxmcId").value;
window.open('<%=basePath%>hgltj.action?method=getHglpmTb&tjjxkshgl.jzrq='+jzrq+'&tjjxkshgl.zt='+1,'','height=650,width=1250,top=150,left=200,toobar=no,menubar=no,scrollbars=yes,resizable=no,location=no,');
}
functionopenwd(){
document.getElementById("formName").action="<%=basePath%>hgltj.action?method=downJxhglPm";
document.getElementById("formName").submit();
}
</script>
</head>
<BODYonLoad="load()"style="background:url(images/cont_bg.gif);background-repeat:repeat-y">
<inputtype="hidden"name="method"value="getDriverInfoList"/>
<tableborder="0"width="100%"cellspacing="0"cellpadding="0"id="maintb">
<tr>
<tdalign="center">
<tablewidth="90%"border="0"cellspacing="0"cellpadding="0"id="mainbox"style="background:url(jsp/commonstyle/images/usermessage_02.gif);background-repeat:repeat-x;">
<!--宽度可变内容框-->
<tr>
<tdwidth="33"height="27"style="background:url(jsp/commonstyle/images/usermessage_01.gif);background-position:left;background-repeat:no-repeat;"></td>
<tdwidth="965"class="style1"><fontcolor="black">驾校合格率排名</font></td>
<tdwidth="14"height="27"style="background:url(jsp/commonstyle/images/usermessage_03.gif);background-position:right;background-repeat:no-repeat;"></td>
</tr>
<tr>
<tdcolspan="3">
<!--页面主体内容开始-->
<!--查询条件-->
<formaction=""name="formName"method="post"style="margin:0px"id="formName"theme="simple">
<tablewidth="100%"border="0"cellpadding="0"cellspacing="0"style="border-collapse:collapse;"id="tj"align="center">
<tralign="left">
<tdclass="tjbg1"style="text-align:left">
<!--<inputtype="hidden"id="method"name="method"value="getHglpm"/>-->
统计日期:
<inputtype="text"name="tjjxkshgl.jzrq"id="jzrqId"value="<s:propertyvalue="tjjxkshgl.jzrq"/>"onclick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM'})"/>
<!--驾校名称:
<s:selectid="jxmcId"name="tjjxkshgl.jxxh"value="tjjxkshgl.jxxh"list="schoolList"listKey="jxxh"listValue="jxmc"headerKey=""headerValue="--请选择--"theme="simple"></s:select>-->
<inputname="input"value="统计"type="button"class="normalbtn"onClick="serch()"style="margin-bottom:5px"/>
<inputname="input"type="button"value="合格率图表"onclick="tbbt()"class="normalbtn"style="margin-bottom:5px"/>
<inputid="Button1"type="button"value="导出"onclick="openwd();"class="normalbtn"style="margin-bottom:5px"/>
</td>
</tr>
</table><!--查询结果-->
</form>
<!--查询结果-->
<tablewidth="100%"border="0"cellpadding="4"cellspacing="1"bgcolor="#abcfff"id="cxjg"align="center">
<thead>
<trclass="tbtitle">
<tdwidth="4%"align="center"class="t1">名次</td>
<tdwidth="8%"align="center"class="t1">名称</td>
<tdclass="t1"width="5%"align="center">科目一</td>
<tdclass="t1"width="5%"align="center">科目二</td>
<tdclass="t1"width="5%"align="center">科目三</td>
<tdclass="t1"width="5%"align="center">平均合格率</td>
<tdclass="t1"width="5%"align="center">操作</td>
</tr>
</thead>
<s:iteratorid="jxhgl"value="jxhelpmList"status="st">
<trclass="changeColor"onMouseOver="overIt()"onMouseOut="outIt()"style="cursor:hand"align="center">
<td><s:propertyvalue="#st.index+1"/></td>
<td><s:propertyvalue="#jxhgl.jxmc"/></td>
<td><s:propertyvalue="#jxhgl.km1hgl"/></td>
<td><s:propertyvalue="#jxhgl.km2hgl"/></td>
<td><s:propertyvalue="#jxhgl.km3hgl"/></td>
<td><s:propertyvalue="#jxhgl.avghgl"/>%</td>
<td>
<ahref="javascript:"onclick="openWin('<%=basePath%>hgltj.action?method=getTbForJxxh&tjjxkshgl.jxxh=<s:propertyvalue="#jxhgl.jxxh"/>&tjjxkshgl.zt=1','',1250,750);">图表</a>
</td>
</tr>
</s:iterator>
</table>
</table>
</td>
</tr>
</table>
</body>
</html>
2)java代码演示:
[java]view plainprint?
/**
*驾校合格率导出excel图表
*/
//response.getOutputStream();//取得输出流
response.reset();//清空输出流
Stringtmptitle="驾校合格率排名";//标题
response.setHeader("Content-disposition","attachment;filename="+newString(tmptitle.getBytes(),"iso8859-1")+".xls");//设定输出文件头
response.setContentType("application/vnd.ms-excel");//定义输出类型
wbook=Workbook.createWorkbook(os);//建立excel文件
WritableSheetwsheet=wbook.createSheet(tmptitle,0);//sheet名称
//设置excel标题
//cellFormat.setBackground(Colour.AQUA);
cellFormat.setFont(wfont);
label.setCellFormat(cellFormat);
wsheet.addCell(label);
//wsheet.addCell(newLabel(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=newjxl.write.WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
wcfFC=newWritableCellFormat(wfont);
wsheet.addCell(newLabel(0,2,"名次",wcfFC));
wsheet.addCell(newLabel(1,2,"驾校名称",wcfFC));
wsheet.addCell(newLabel(2,2,"科目一",wcfFC));
wsheet.addCell(newLabel(3,2,"科目二",wcfFC));
wsheet.addCell(newLabel(4,2,"科目三",wcfFC));
wsheet.addCell(newLabel(5,2,"合格率",wcfFC));
intcount=jxhelpmList.size();
if(count>0){////判断集合是否不为0
TjJxkshgltjhgl=null;
for(inti=0;i<jxhelpmList.size();i++){
tjhgl=(TjJxkshgl)jxhelpmList.get(i);
wsheet.addCell(newLabel(0,i+3,(i+1)+""));
wsheet.addCell(newLabel(1,i+3,tjhgl.getJxmc()));
wsheet.addCell(newLabel(2,i+3,tjhgl.getKm1hgl()));
wsheet.addCell(newLabel(3,i+3,tjhgl.getKm2hgl()));
wsheet.addCell(newLabel(4,i+3,tjhgl.getKm3hgl()));
wsheet.addCell(newLabel(5,i+3,tjhgl.getAvghgl()));
}
‘捌’ java能否wps调用页码
1. [代码][Java]代码
package experiments;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Doc2Pdf {
public static Converter newConverter(String name) {
if (name.equals("wps")) {
return new Wps();
} else if (name.equals("pdfcreator")) {
return new PdfCreator();
}
return null;
}
public synchronized static boolean convert(String word, String pdf) {
return newConverter("pdfcreator").convert(word, pdf);
}
public static interface Converter {
public boolean convert(String word, String pdf);
}
public static class Wps implements Converter {
public synchronized boolean convert(String word, String pdf) {
File pdfFile = new File(pdf);
File wordFile = new File(word);
ActiveXComponent wps = null;
try {
wps = new ActiveXComponent("wps.application");
ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", new Variant(wordFile.getAbsolutePath()));
doc.invoke("ExportPdf", new Variant(pdfFile.getAbsolutePath()));
doc.invoke("Close");
doc.safeRelease();
} catch (Exception ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (Error ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} finally {
if (wps != null) {
wps.invoke("Terminate");
wps.safeRelease();
}
}
return true;
}
}
public static class PdfCreator implements Converter {
public static final int STATUS_IN_PROGRESS = 2;
public static final int STATUS_WITH_ERRORS = 1;
public static final int STATUS_READY = 0;
private ActiveXComponent pdfCreator;
private DispatchEvents dispatcher;
private volatile int status;
private Variant defaultPrinter;
private void init() {
pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator");
dispatcher = new DispatchEvents(pdfCreator, this);
pdfCreator.setProperty("cVisible", new Variant(false));
pdfCreator.invoke("cStart", new Variant[]{new Variant("/NoProcessingAtStartup"), new Variant(true)});
setCOption("UseAutosave", 1);
setCOption("UseAutosaveDirectory", 1);
setCOption("AutosaveFormat", 0); // 0 = PDF
defaultPrinter = pdfCreator.getProperty("cDefaultPrinter");
status = STATUS_IN_PROGRESS;
pdfCreator.setProperty("cDefaultprinter", "PDFCreator");
pdfCreator.invoke("cClearCache");
pdfCreator.setProperty("cPrinterStop", false);
}
private void setCOption(String property, Object value) {
Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]);
}
private void close() {
if (pdfCreator != null) {
pdfCreator.setProperty("cDefaultprinter", defaultPrinter);
pdfCreator.invoke("cClearCache");
pdfCreator.setProperty("cPrinterStop", true);
pdfCreator.invoke("cClose");
pdfCreator.safeRelease();
pdfCreator = null;
}
if (dispatcher != null) {
dispatcher.safeRelease();
dispatcher = null;
}
}
public synchronized boolean convert(String word, String pdf) {
File pdfFile = new File(pdf);
File wordFile = new File(word);
try {
init();
setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath());
if (pdfFile.exists()) {
pdfFile.delete();
}
pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath());
int seconds = 0;
while (isInProcess()) {
seconds++;
if (seconds > 30) { // timeout
throw new Exception("convertion timeout!");
}
Thread.sleep(1000);
}
if (isWithErrors()) return false;
// 由于转换前设置cOption的AutosaveFilename不能保证输出的文件名与设置的相同(pdfcreator会加入/修改后缀名)
// 所以这里让pdfcreator使用自动生成的文件名进行输出,然后在保存后将其重命名为目标文件名
File outputFile = new File(pdfCreator.getPropertyAsString("cOutputFilename"));
if (outputFile.exists()) {
outputFile.renameTo(pdfFile);
}
} catch (InterruptedException ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (Exception ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (Error ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} finally {
close();
}
return true;
}
private boolean isInProcess() {
return status == STATUS_IN_PROGRESS;
}
private boolean isWithErrors() {
return status == STATUS_WITH_ERRORS;
}
// eReady event
public void eReady(Variant[] args) {
status = STATUS_READY;
}
// eError event
public void eError(Variant[] args) {
status = STATUS_WITH_ERRORS;
}
}
public static void main(String[] args) {
convert("e:\\test.doc", "e:\\output.pdf");
}
}
‘玖’ WPS+宏代码是+JAVA+还是+Javascrip
wps宏
如果你安装了VBA for WPS的话, 你就可以用VBA代码写.
还有wps自带的就是javasript.
个人暂时更喜欢用VBA, 毕竟在学校也学过, 初使用wps的js, 感觉还是有一些毛病的.
‘拾’ 能写个读取.wps后缀名文档的Java程序吗
可以的,用微软的Office也可以打开的,呵呵,这个一般装windows的机子好像都装了吧再不你可以保存的时候,保存个PDF格式的在别的电脑上只要有PDF的阅读器也可以打开的