Java读取.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();
}
}
}
}

读取结果:

注意在程序中导入spire.doc.jar。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-09-27
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();
}
}
,本回答被网友采纳
相似回答