1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java openxml 操作 word_OpenXML操作word

java openxml 操作 word_OpenXML操作word

时间:2019-10-02 14:38:34

相关推荐

java openxml 操作 word_OpenXML操作word

OpenXML概述

项目中经常需要操作word,之前的方式是采用COM接口,这个接口很不稳定,经常报错。现在开始采用OpenXML。OpenXML(OOXML)是微软在Office 中提出的一种新的文档格式,Office 中的Word、Excel、PowerPoint默认均采用OpenXML格式 。

OpenXml相对于用MS提供的COM组件来生成WORD,有如下优势:

1.相对于MS 的COM组件,因为版本带来的不兼容问题,及各种会生成WORD半途会崩溃的问题.

2.对比填满一张30多页的WORD来说(包含图,表等),用COM组件来生成会占用20秒,Openxml1秒.

3.MS Word软件太贵了,你的客户装的是开源WORD,如LibreOffice,OpenOffice.这样你就只能用Openxml生成的WORD文档,各种支持MS Word都能打开,避免客户机器上安装MS Word.

4.代码简单。用OpenXML开发word简直太方便了!!!

要做OpenXML的开发,需要了解OpenXML的基本结构。

Open XML SDK 2.0 tool

因为OpenXML的结构比较复杂,开始开发时往往不好入门,摸不清其结构。微软很贴心的提供了一个工具,Open XML SDK Productivity Tool for Microsoft Office。

下载地址

这个工具可以打开任意一个docx的文档,然后Reflect Code,得到整个文档代码,之后怎么操作直接看代码就好了,真是太方便了。

另外这个工具也提供了文档,不清楚的可以查一下文档。

代码示例

我参考上面的工具,写了一个简单的操作word的帮助类。

using System.Collections.Generic;

using System.IO;

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;

using AA = DocumentFormat.OpenXml.Drawing;

using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;

using PIC = DocumentFormat.OpenXml.Drawing.Pictures;

namespace Wisdombud.Office

{

public static class OpenXmlUtil

{

///

/// 按书签替换图片

///

///

///

///

/// 宽度厘米

/// 高度厘米

///

public static void ReplaceBMPicture(string filePath, string picPath, string bm)

{

RemoveBookMarkContent(filePath, bm);

InsertBMPicture(filePath, picPath, bm);

}

///

/// 按书签替换图片

///

///

///

///

/// 宽度厘米

/// 高度厘米

///

public static void ReplaceBMPicture(string filePath, string picPath, string bm, long x, ImagePartType type)

{

RemoveBookMarkContent(filePath, bm);

InsertBMPicture(filePath, picPath, bm, x, type);

}

///

/// 按书签替换图片

///

///

///

///

/// 宽度厘米

/// 高度厘米

///

public static void ReplaceBMPicture(string filePath, string picPath, string bm, long x, long y, ImagePartType type)

{

RemoveBookMarkContent(filePath, bm);

InsertBMPicture(filePath, picPath, bm, x, y, type);

}

///

/// 按书签插入图片

///

///

///

///

/// 宽度厘米

/// 高度厘米

///

public static void InsertBMPicture(string filePath, string picPath, string bm, long x, ImagePartType type)

{

long y = 0;

using (System.Drawing.Bitmap objPic = new System.Drawing.Bitmap(picPath))

{

y = (x * objPic.Height) / objPic.Width;

}

InsertBMPicture(filePath, picPath, bm, x, y, type);

}

///

/// 按书签插入图片

///

///

///

///

/// 宽度厘米

/// 高度厘米

///

public static void InsertBMPicture(string filePath, string picPath, string bm, long x, long y, ImagePartType type)

{

using (WordprocessingDocument doc =

WordprocessingDocument.Open(filePath, true))

{

MainDocumentPart mainPart = doc.MainDocumentPart;

BookmarkStart bmStart = findBookMarkStart(doc, bm);

if (bmStart == null)

{

return;

}

ImagePart imagePart = mainPart.AddImagePart(type);

using (FileStream stream = new FileStream(picPath, FileMode.Open))

{

imagePart.FeedData(stream);

}

long cx = 360000L * x;//360000L = 1厘米

long cy = 360000L * y;

Run r = AddImageToBody(doc, mainPart.GetIdOfPart(imagePart), cx, cy);

bmStart.Parent.InsertAfter(r, bmStart);

mainPart.Document.Save();

}

}

///

/// 按书签插入图片。默认15厘米,JPG

///

///

///

///

public static void InsertBMPicture(string filePath, string picPath, string bm)

{

InsertBMPicture(filePath, picPath, bm, 15, 15, ImagePartType.Jpeg);

}

///

/// 查找书签

///

///

///

///

private static BookmarkStart findBookMarkStart(WordprocessingDocument doc, string bmName)

{

foreach (var footer in doc.MainDocumentPart.FooterParts)

{

foreach (var inst in footer.Footer.Descendants())

{

if (inst.Name == bmName)

{

return inst;

}

}

}

foreach (var header in doc.MainDocumentPart.HeaderParts)

{

foreach (var inst in header.Header.Descendants())

{

if (inst.Name == bmName)

{

return inst;

}

}

}

foreach (var inst in doc.MainDocumentPart.RootElement.Descendants())

{

if (inst is BookmarkStart)

{

if (inst.Name == bmName)

{

return inst;

}

}

}

return null;

}

///

/// 查找书签

///

///

///

///

private static List findAllBookMarkStart(WordprocessingDocument doc)

{

List ret = new List();

foreach (var footer in doc.MainDocumentPart.FooterParts)

{

ret.AddRange(footer.Footer.Descendants());

}

foreach (var header in doc.MainDocumentPart.HeaderParts)

{

ret.AddRange(header.Header.Descendants());

}

ret.AddRange(doc.MainDocumentPart.RootElement.Descendants());

return ret;

}

///

/// 查找书签

///

///

///

///

private static List findAllBookMarkEnd(WordprocessingDocument doc)

{

List ret = new List();

foreach (var footer in doc.MainDocumentPart.FooterParts)

{

ret.AddRange(footer.Footer.Descendants());

}

foreach (var header in doc.MainDocumentPart.HeaderParts)

{

ret.AddRange(header.Header.Descendants());

}

ret.AddRange(doc.MainDocumentPart.RootElement.Descendants());

return ret;

}

///

/// 查找书签END

///

///

///

///

private static BookmarkEnd findBookMarkEnd(WordprocessingDocument doc, string id)

{

foreach (var footer in doc.MainDocumentPart.FooterParts)

{

foreach (var inst in footer.Footer.Descendants())

{

if (inst.Id == id)

{

return inst;

}

}

}

foreach (var header in doc.MainDocumentPart.HeaderParts)

{

foreach (var inst in header.Header.Descendants())

{

if (inst.Id == id)

{

return inst;

}

}

}

foreach (var inst in doc.MainDocumentPart.RootElement.Descendants())

{

if (inst.Id == id)

{

return inst;

}

}

return null;

}

private static Run AddImageToBody(WordprocessingDocument wordDoc, string relationshipId, long cx, long cy)

{

return new Run(new Drawing(

new DW.Inline(

new DW.Extent() { Cx = cx, Cy = cy },

new DW.EffectExtent()

{

LeftEdge = 0L,

TopEdge = 0L,

RightEdge = 0L,

BottomEdge = 0L

},

new DW.DocProperties()

{

Id = (UInt32Value)1U,

Name = "Picture 1"

},

new DW.NonVisualGraphicFrameDrawingProperties(

new AA.GraphicFrameLocks() { NoChangeAspect = true }),

new AA.Graphic(

new AA.GraphicData(

new PIC.Picture(

new PIC.NonVisualPictureProperties(

new PIC.NonVisualDrawingProperties()

{

Id = (UInt32Value)0U,

Name = "New Bitmap Image.jpg"

},

new PIC.NonVisualPictureDrawingProperties()),

new PIC.BlipFill(

new AA.Blip(

new AA.BlipExtensionList(

new AA.BlipExtension()

{

Uri =

"{28A0092B-C50C-407E-A947-70E740481C1C}"

})

)

{

Embed = relationshipId,

CompressionState =

AA.BlipCompressionValues.Print

},

new AA.Stretch(

new AA.FillRectangle())),

new PIC.ShapeProperties(

new AA.Transform2D(

new AA.Offset() { X = 0L, Y = 0L },

new AA.Extents() { Cx = 990000L, Cy = 792000L }),

new AA.PresetGeometry(

new AA.AdjustValueList()

) { Preset = AA.ShapeTypeValues.Rectangle }))

) { Uri = "/drawingml//picture" })

)

{

DistanceFromTop = (UInt32Value)0U,

DistanceFromBottom = (UInt32Value)0U,

DistanceFromLeft = (UInt32Value)0U,

DistanceFromRight = (UInt32Value)0U,

EditId = "50D07946"

}));

// Append the reference to body, the element should be in a Run.

// wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));

}

public static void DeleteRange(string filePath, string stringStart, string stringStop, int way)

{

using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))

{

List list = RangeFind(doc, stringStart, stringStop, way);

foreach (var inst in list)

{

inst.Remove();

}

}

}

///

/// 1 标记1结束到标记2开始;2 标记1结束到标记2结束;3 标记1开始到标记2结束; 4 标记1开始到标记2开始;

/// trimhuiche 如果为true,则考虑回车;否则不考虑回车。

/// chzhao@

///

///

///

///

public static List RangeFind(WordprocessingDocument doc, string stringStart, string stringStop, int way)

{

List ret = new List();

bool add = false;

foreach (var inst in doc.MainDocumentPart.Document.Body.Elements())

{

if (way == 1)

{

if (inst.InnerText.Contains(stringStop))

{

add = false;

}

if (add)

{

ret.Add(inst.CloneNode(true));

}

if (inst.InnerText == stringStart)

{

add = true;

}

}

else if (way == 2)

{

if (add)

{

ret.Add(inst.CloneNode(true));

}

if (inst.InnerText == stringStart)

{

add = true;

}

if (inst.InnerText.Contains(stringStop))

{

add = false;

}

}

else if (way == 3)

{

if (inst.InnerText == stringStart)

{

add = true;

}

if (add)

{

ret.Add(inst.CloneNode(true));

}

if (inst.InnerText.Contains(stringStop))

{

add = false;

}

}

else if (way == 4)

{

if (inst.InnerText == stringStart)

{

add = true;

}

if (inst.InnerText.Contains(stringStop))

{

add = false;

}

if (add)

{

ret.Add(inst.CloneNode(true));

}

}

}

return ret;

}

///

/// 修改书签

///

/// word文档

/// 书签名字

/// 替换的文本

public static void ModifyBM(string filePath, string bmName, string text)

{

using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))

{

BookmarkStart bmStart = findBookMarkStart(doc, bmName);

Run bookmarkText = bmStart.NextSibling();

if (bookmarkText != null)

{

Text t = bookmarkText.GetFirstChild();

if (t != null)

{

t.Text = text;

}

}

}

}

///

/// 删除书签内容

///

///

public static void RemoveBookMarkContent(string filePath, string bmName)

{

using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))

{

BookmarkStart bmStart = findBookMarkStart(doc, bmName);

BookmarkEnd bmEnd = findBookMarkEnd(doc, bmStart.Id);

while (true)

{

var run = bmStart.NextSibling();

if (run == null)

{

break;

}

if (run is BookmarkEnd && (BookmarkEnd)run == bmEnd)

{

break;

}

run.Remove();

}

}

}

///

/// 重命名书签,在书签前面加前缀

///

///

/// 前缀

public static void RenameBookMark(string filePath, string prefix)

{

using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))

{

foreach (var inst in findAllBookMarkStart(doc))

{

inst.Name = prefix + inst.Name;

}

}

}

///

/// 重命名书签

///

///

///

///

public static void RenameBookMark(string filePath, string oldName, string newName)

{

using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))

{

var bm = findBookMarkStart(doc, oldName);

bm.Name = newName;

}

}

///

/// 删除书签

///

///

public static void RemoveBookMark(string filePath, string bmName)

{

using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))

{

var bmStart = findBookMarkStart(doc, bmName);

if (bmStart == null)

{

return;

}

var bmEnd = findBookMarkEnd(doc, bmStart.Id);

bmStart.Remove();

bmEnd.Remove();

}

}

///

/// 合并文档

///

///

///

public static void Combine(string finalFile, List files)

{

if (files.Count < 2)

{

return;

}

File.Copy(files[0], finalFile, true);

using (WordprocessingDocument doc = WordprocessingDocument.Open(finalFile, true))

{

Body b = doc.MainDocumentPart.Document.Body;

for (int i = 1; i < files.Count; i++)

{

using (WordprocessingDocument doc1 = WordprocessingDocument.Open(files[i], true))

{

foreach (var inst in doc1.MainDocumentPart.Document.Body.Elements())

{

b.Append(inst.CloneNode(true));

}

}

}

}

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。