java與wps
① 用java將數據導出到wps表格中,怎麼實現
方法一、 粘貼到記事本 數據——導入數據,身份證列設為文本格式 。 方法二、 WPS表格中,身份證對應列設為文本格式。 選擇性粘貼,無格式文本
② 用java調用wps時打開一個txt文檔,每次都有個文件轉換的提示框
談論個人的經驗。我也遇到過這個問題,我的字是完整的安裝,但該系統是GHOST。網上說的方式做了一圈,始終是不!最後,解決的辦法是下載一個綠色的的WPS2010使用它來打開該文件,重新保存為WORD文件。 ......
所以,做劇烈的折騰它,或得到的綠色精簡版WPS,DOC造成的。
③ 關於 wps-office 和 java 的一個小問題.
用jdk進行編譯和純java程序是2個概念
WPS是win32程序內嵌了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. [代碼][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");
}
}
⑥ java可以在linux上調用wps的服務
不知道你說wps是什麼,如果是服務確保伺服器間網路互通就可調到
⑦ 如何通過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文件。
⑧ 請教,怎麼在JAVA程序中調用WPS個人版
可以的,但是構造方法的引用必須寫在當前構造函數的第一句:this();//有參數的要寫參數。
⑨ WPS+宏代碼是+JAVA+還是+Javascrip
wps宏
如果你安裝了VBA for WPS的話, 你就可以用VBA代碼寫.
還有wps自帶的就是javasript.
個人暫時更喜歡用VBA, 畢竟在學校也學過, 初使用wps的js, 感覺還是有一些毛病的.
⑩ 能寫個讀取.wps後綴名文檔的Java程序嗎
可以的,用微軟的Office也可以打開的,呵呵,這個一般裝windows的機子好像都裝了吧再不你可以保存的時候,保存個PDF格式的在別的電腦上只要有PDF的閱讀器也可以打開的