我正在编写java代码来实现以下功能。
1.读取给定的微软办公文档(.doc)文件。
2.在文件中搜索给定的字符串。
3.删除位于任意位置的给定字符串。
4.在指定位置插入或替换任何给定的字符串。
5.将更新的文件内容写入并保存到新的.doc文件中。
我已经写了一个代码来读取,搜索,插入或替换,删除和保存文件,它运行良好,但我不能保留输入文件中应用的文本格式(如字体颜色,字体大小,对齐方式,左右缩进,样式等)。
请任何人帮助我解决这个问题。
谢谢
发布于 2013-06-05 16:51:04
我将为Ms-Word文档添加新的样式解决方案..
public class CreateDocumentFromScratch {
public static void main(String[] args) {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraphOne = document.createParagraph();
paragraphOne.setAlignment(ParagraphAlignment.CENTER);
paragraphOne.setBorderBottom(Borders.SINGLE);
paragraphOne.setBorderTop(Borders.SINGLE);
paragraphOne.setBorderRight(Borders.SINGLE);
paragraphOne.setBorderLeft(Borders.SINGLE);
paragraphOne.setBorderBetween(Borders.SINGLE);
XWPFRun paragraphOneRunOne = paragraphOne.createRun();
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setItalic(true);
paragraphOneRunOne.setText("Hello world! This is paragraph one!");
paragraphOneRunOne.addBreak();
XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
paragraphOneRunTwo.setText("Run two!");
paragraphOneRunTwo.setTextPosition(100);
XWPFRun paragraphOneRunThree = paragraphOne.createRun();
paragraphOneRunThree.setStrike(true);
paragraphOneRunThree.setFontSize(20);
paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
paragraphOneRunThree.setText(" More text in paragraph one...");
XWPFParagraph paragraphTwo = document.createParagraph();
paragraphTwo.setAlignment(ParagraphAlignment.DISTRIBUTE);
paragraphTwo.setIndentationRight(200);
XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
paragraphTwoRunOne.setText("And this is paragraph two.");
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(args[0]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
document.write(outStream);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}发布于 2017-12-06 19:21:42
您可以使用以下代码:
public class EditingWord{
public static void main(String[] args) {
// TODO Auto-generated method stub
String filename = "path to input file/file_input_name";
List<String> paraList = new ArrayList<String>();
try {
XWPFDocument doc = new XWPFDocument(OPCPackage.open(new FileInputStream(filename)));
List<XWPFParagraph> paragraphList = doc.getParagraphs();
for (XWPFParagraph para : paragraphList) {
if ((para.getStyle() != null) && (para.getNumFmt() != null)) {
for (XWPFRun run : para.getRuns()) {
String text = run.text();
text = text.replaceAll(text, "replacement" + text);
run.setText(text, 0);
}
}
}
doc.write(new FileOutputStream("path to your file/output_File_name"));
} catch (Exception e) {
e.printStackTrace();
}
}
}如果要将内容保存到同一文件,可以更改doc.write(new FileOutputStream("path to your inpufile/input_File_name"));
发布于 2013-05-30 18:56:24
我建议您使用Apache POI文档。我将使用文档进行一些实验,并轻松地获取Ms-Excel Sheet的文本格式。
http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html
在浏览应用程序接口时,我看到了DataFormat类及其层次结构、BuiltinFormats类和CellStyle类的setDataFormat方法。所以做了一些实验,下面的代码似乎起作用了!
XSSFCellStyle textFormatStyle = book.createCellStyle();
textFormatStyle.setDataFormat((short)BuiltinFormats.getBuiltinFormat("text"));
XSSFCell cell = row.createCell(columnIndex++);
cell.setCellStyle(textFormatStyle); 现在,一旦创建了电子表格,您就可以编辑单元格,当您按tab键离开时,格式仍然是“文本”。
我用完整的例子向你展示了另一种方式..其中我将进一步展示一个效果,您可以根据需要添加...
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Style example");
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("This is bold");
cell.setCellStyle(style);
font = workbook.createFont();
font.setItalic(true);
style = workbook.createCellStyle();
style.setFont(font);
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("This is italic");
cell.setCellStyle(style);
try {
FileOutputStream out = new FileOutputStream(new File("C:\\style.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}此代码将生成以下输出:

https://stackoverflow.com/questions/16833767
复制相似问题