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的閱讀器也可以打開的