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的阅读器也可以打开的