package com.jyf;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlString;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import javax.xml.namespace.QName;
import java.io.*;
import java.util.List;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws IOException {
changeWord("E:\\文档\\1.docx","E:\\文档\\2.docx",1);
}
//word 加水印 加解密
public static void changeWord(String inputSrc, String outSrc,int status) throws IOException {
InputStream in = new FileInputStream(new File(inputSrc));
XWPFDocument doc = new XWPFDocument(in);
addWaterMark(doc,"水印","#B0C4DE");
doc.write(new FileOutputStream(outSrc));
}
private static void addWaterMark(Object obj, String watermark, String color) {
if (obj instanceof XWPFDocument) {
XWPFDocument doc = (XWPFDocument) obj;
// create header-footer
XWPFHeaderFooterPolicy headerFooterPolicy =doc.createHeaderFooterPolicy();
// create default Watermark - fill color black and not rotated
headerFooterPolicy.createWatermark(watermark);
// get the default header
// Note: createWatermark also sets FIRST and EVEN headers
// but this code does not updating those other headers
XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.EVEN);
XWPFParagraph paragraph = header.getParagraphArray(0);
// get com.microsoft.schemas.vml.CTShape where fill color and rotation is set
paragraph.getCTP().newCursor();
XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(new QName("urn:schemas-microsoft-com:vml", "shape"));
if (xmlobjects.length > 0) {
com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape)xmlobjects[0];
// set fill color
ctshape.setFillcolor(color);
// set rotation
ctshape.setStyle(ctshape.getStyle() + ";rotation:60");
}
} else if (obj instanceof HWPFDocument) {
}
}
}
先粘贴代码:
//excel 加水印 加解密
public static void changeExcel(String path,String targetpath,int status) throws Exception {
//输入流
InputStream is = new FileInputStream(path);
Workbook wb = null;
//定义工作簿
if (path.toLowerCase().endsWith("xlsx")) {
wb = new XSSFWorkbook(POIXMLDocument.openPackage(path));
} else if (path.toLowerCase().endsWith("xls")) {
wb = new HSSFWorkbook(new POIFSFileSystem(is));
}
//获取第一个sheet页
Sheet hssfSheet = wb.getSheetAt(0);
//插入水印
// ExcelWaterRemarkUtils.putWaterRemarkToExcel(wb, hssfSheet, "E:\\1.png", 1, 1, 0, 0, 1, 1 , 30, 30);
if(hssfSheet == null){
return;
}
//遍历行row
for(int rowNum = 0; rowNum<=hssfSheet.getLastRowNum();rowNum++){
//获取每一行
Row row = hssfSheet.getRow(rowNum);
if(row == null){
continue;
}
//遍历列cell
for(int cellNum = 0; cellNum<=row.getLastCellNum();cellNum++){
//获取每一列
Cell cell = row.getCell(cellNum);
if(cell == null){
continue;
}
if (status==1)
cell.setCellValue(WordAPi.Encrypt(getValue(cell)));
else
cell.setCellValue(WordAPi.Decrypt(getValue(cell)));
}
}
FileOutputStream out =null;
out = new FileOutputStream(targetpath);
wb.write(out);
is.close();
out.close();
wb.close();
addWaterMark4Excel(targetpath);
hiddenSheet(targetpath,"Evaluation Warning");
}
/**
* 隐藏指定的Sheet
* @param targetFile 目标文件
* @param sheetName Sheet名称
*/
public static void hiddenSheet(String targetFile,String sheetName) {
try {
FileInputStream fis = new FileInputStream(targetFile);
XSSFWorkbook wb = new XSSFWorkbook(fis);
//隐藏Sheet
// wb.setSheetHidden(wb.getSheetIndex(sheetName), 1);
if (wb.getSheet(sheetName) == null) {
return;
}
wb.removeSheetAt(wb.getSheetIndex(sheetName));
FileOutputStream out1 = new FileOutputStream(targetFile);
wb.write(out1);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void removeSheetsByName(String sheetName, Workbook wb) {
if (wb.getSheet(sheetName) == null) {
return;
}
wb.removeSheetAt(wb.getSheetIndex(sheetName));
}
//word 加水印 加解密
public static void changeWord(String inputSrc, String outSrc,int status) throws IOException {
InputStream in = new FileInputStream(new File(inputSrc));
// XWPFDocument doc = new XWPFDocument(POIXMLDocument.openPackage(inputSrc));
XWPFDocument doc = new XWPFDocument(in);
XWPFDocument newdoc = new XWPFDocument();
/**
* 替换段落中指定的文本
*/
for(XWPFParagraph p : doc.getParagraphs()){
XWPFParagraph xwpfParagraph = newdoc.createParagraph();
List runs = p.getRuns();
CTPPr oldPPr = p.getCTP().getPPr();//获取段落对象的样式的ppr标签
if (oldPPr!=null) {
CTParaRPr oldPPrRpr = oldPPr.getRPr();//获取ppr标签里的rpr标签
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrChange oldPPrChange = oldPPr.getPPrChange();//获取pprchange标签
CTTrackChange rprIns = null;
CTParaRPrChange oldPraRprChange = null;
if (oldPPrRpr!=null) {
rprIns = oldPPrRpr.getIns();//获取ins标签
oldPraRprChange = oldPPrRpr.getRPrChange();//获取ppr标签中的rpr标签里的rprchange标签
}
if (oldPPrChange!=null) {//如果有change标签证明是修订内容,设置为取消修订
oldPPr.unsetPPrChange();
}
if (oldPraRprChange!=null) {
oldPPrRpr.unsetRPrChange();
}
if (rprIns!=null) {//ins标签代表着修订线,如果有修订线则取消修订线
oldPPrRpr.unsetIns();
}
}
xwpfParagraph.getCTP().setPPr(oldPPr);//把传过来的段落中的ppr标签设置到新的段落里
if(runs != null){
for(XWPFRun r : runs){
XWPFRun run =xwpfParagraph.createRun();
//需要替换的文本
String text = r.getText(0);
//替换指定的文本
if(text != null){
//替换的时候要注意,setText是有两个参数的
//第一个是替换的文本,第二个是从哪里开始替换
//0是替换全部,如果不设置那么默认就是从原文字
//结尾开始追加
try {
CTRPr oldRPr = r.getCTR().getRPr();
CTRPrChange oldRPrChange = oldRPr.getRPrChange();//run标签里也有rprchange标签也需要像上面一样设置
if (oldRPrChange!=null) {
oldRPr.unsetRPrChange();
}
run.getCTR().setRPr(oldRPr);
}catch (Exception e){
}
if (status ==1){
r.setText(WordAPi.Encrypt(text));
run.setText(WordAPi.Encrypt(text));
}
else{
r.setText(WordAPi.Decrypt(text));
run.setText(WordAPi.Decrypt(text));
}
// run.setCharacterSpacing(r.getCharacterSpacing());
run.setColor(r.getColor());
// run.setFontSize(r.getFontSize());
run.setSubscript(r.getSubscript());
run.setKerning(r.getKerning());
run.setTextPosition(r.getTextPosition());
run.setUnderline(r.getUnderline());
}else {
run.setText(r.getText(0));
}
}
}
}
/**
* 替换表格中指定的文字
*/
for(XWPFTable tab : doc.getTables()){
for(XWPFTableRow row : tab.getRows()){
for(XWPFTableCell cell : row.getTableCells()){
//注意,getParagraphs一定不能漏掉
//因为一个表格里面可能会有多个需要替换的文字
//如果没有这个步骤那么文字会替换不了
for(XWPFParagraph p : cell.getParagraphs()){
for(XWPFRun r : p.getRuns()){
String text = r.getText(0);
if (status ==1)
r.setText(WordAPi.Encrypt(text));
else
r.setText(WordAPi.Decrypt(text));
}
}
}
}
}
addWaterMark(newdoc,"智贝","#B0C4DE");
// doc.write(new FileOutputStream(outSrc));
newdoc.write(new FileOutputStream(outSrc));
}
//改变所有文本,不改变样式
public static void changePPT(String path,String targetpath,int status) throws IOException {
XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(path));
byte[] pictureData = IOUtils.toByteArray(new FileInputStream("/home/oaSoma/img/1.png"));
// byte[] pictureData = IOUtils.toByteArray(new FileInputStream("E:\\1.png"));
org.apache.poi.sl.usermodel.PictureData pictureData1 = slideShow.addPicture(pictureData, XSLFPictureData.PictureType.PNG);
for (XSLFSlide slide : slideShow.getSlides()) {
XSLFPictureShape pictureShape = slide.createPicture(pictureData1);
pictureShape.setAnchor(new java.awt.Rectangle(50, 300, 100, 100));
CTSlide ctSlide = slide.getXmlObject();
XmlObject[] allText = ctSlide.selectPath(
"declare namespace a='http://schemas.openxmlformats.org/drawingml/2006/main' " +
".//a:t"
);
for (int i = 0; i < allText.length; i++) {
if (allText[i] instanceof XmlString) {
XmlString xmlString = (XmlString)allText[i];
String text = xmlString.getStringValue();
if (text==null||text.equals("")) continue;
if (status==1) xmlString.setStringValue(WordAPi.Encrypt(text));
else xmlString.setStringValue(WordAPi.Decrypt(text));
}
}
}
FileOutputStream out = new FileOutputStream(targetpath);
slideShow.write(out);
slideShow.close();
out.close();
}
//Excel 加水印
public static void addWaterMark4Excel(String path) throws Exception {
FileInputStream is = new FileInputStream(path);
com.aspose.cells.Workbook workbook = new com.aspose.cells.Workbook(is);
//Get the first default sheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Add Watermark
Shape wordart = sheet.getShapes().addTextEffect(MsoPresetTextEffect.TEXT_EFFECT_1,
"智贝", "Arial Black", 50, false, true
, 18, 8, 1, 1, 130, 800);
//Get the fill format of the word art
MsoFillFormat wordArtFormat = wordart.getFillFormat();
//Set the color
wordArtFormat.setForeColor(com.aspose.cells.Color.getRed());
//Set the transparency
wordArtFormat.setTransparency(0.9);
//Make the line invisible
MsoLineFormat lineFormat = wordart.getLineFormat();
lineFormat.setVisible(false);
//Save the file
workbook.save(path);
}
private static void addWaterMark(Object obj, String watermark, String color) {
if (obj instanceof XWPFDocument) {
XWPFDocument doc = (XWPFDocument) obj;
// create header-footer
XWPFHeaderFooterPolicy headerFooterPolicy =doc.createHeaderFooterPolicy();
// create default Watermark - fill color black and not rotated
headerFooterPolicy.createWatermark(watermark);
// get the default header
// Note: createWatermark also sets FIRST and EVEN headers
// but this code does not updating those other headers
XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph paragraph = header.getParagraphArray(0);
// get com.microsoft.schemas.vml.CTShape where fill color and rotation is set
paragraph.getCTP().newCursor();
XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(new QName("urn:schemas-microsoft-com:vml", "shape"));
if (xmlobjects.length > 0) {
com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape)xmlobjects[0];
// set fill color
ctshape.setFillcolor(color);
// set rotation
ctshape.setStyle(ctshape.getStyle() + ";rotation:315");
}
} else if (obj instanceof HWPFDocument) {
}
}
POM文件
4.0.0
com.zhibei
Ioc_otl
1.0-SNAPSHOT
Ioc_otl
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
org.apache.poi
poi-ooxml
3.17
org.springframework
spring-core
4.3.6.RELEASE
com.chenlb.mmseg4j
mmseg4j-solr
2.4.0
org.apache.poi
poi-scratchpad
3.17
org.apache.poi
ooxml-schemas
1.1
org.apache.xmlbeans
xmlbeans
2.5.0
com.lowagie
itext
2.1.7
com.itextpdf
itextpdf
5.5.13
com.itextpdf
itext-asian
5.2.0
xerces
xercesImpl
2.11.0
redis.clients
jedis
3.1.0
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
maven-site-plugin
3.7.1
maven-project-info-reports-plugin
3.0.0