Quantcast
Channel: Open XML Format SDK forum
Viewing all 1288 articles
Browse latest View live

Rich Text Content controls and Custom XML makes the document size growing too fast

$
0
0

Hi, I have a problem with Word Rich COntent controls and CustoXML mappings.

I've tried the code, proposed in the MSDN article Content controls in Word

https://msdn.microsoft.com/en-us/library/jj889465.aspx#WordCC_WhatsNew

The code works fine, no problem. BUT, as soon as I put some styles in the rich text content, the XML mapping is replaced by the full (escaped) XML document representing the docx.

Somethig like : <x><?xml version="1.0" standalone="yes"?> <?mso-application progid="Word.Document"?> <pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"><pkg:part pkg:name="/_rels/.rel ... </x>

<x></x>

Is this a well known bug ? Is there a way to avoid this feature which makes the feature unusable as soon the document size is growing very fast ?

Compare two word documents c# and get differences

$
0
0

We are trying to build a console application in c#. The application needs to open a word document convert it to XML and then compare the XML against an another XML that is also generated from a word doc. The app is then supposed to generate the differences in the two documents. We are trying to achieve this using OpenXML api and some of the code samples provided there such as:-

using (WordprocessingDocument doc = WordprocessingDocument.Open("Test.docx", false))
{
    XElement root = doc.MainDocumentPart.GetXDocument().Root;
    XElement paragraph = root.Descendants(W.p).First();
    Console.WriteLine(paragraph.Value);
}

For implementing the above sample we have included the following references:-

using System.Xml.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;

But we are just not able to test the below mentioned code:-

    XElement root = doc.MainDocumentPart.GetXDocument().Root;

as it throws the following error:-

"'DocumentFormat.OpenXml.Packaging.MainDocumentPart' does not contain a definition for 'GetXDocument' and no extension method 'GetXDocument' accepting a first argument of type 'DocumentFormat.OpenXml.Packaging.MainDocumentPart' could be found (are you missing a using directive or an assembly reference?)"

Is there something that we are missing here? Or any other documentation or tutorial that shows the complete steps with the help of an example to show the use of WordprocessingDocument?

Thanks.

Excel Open XML RowBreaks and Footer Images - XML file positioning

$
0
0

Using VS2012, C#, OpenXML 2.5, I created a test utility that adds a page break (Row Break) to a worksheet using (pseudo):

objRB = New RowBreaks();
objRowBreaks.Append(objRB);
objRowBreaks.ManualBreakCount++;
objRowBreaks.Count++;

Works just fine. Then I added an image (manually) in my Excel page footer. Then I run the program that appends the Row Break again, and when I attempt to open the workbook afterwards, I get an error: "We found a problem with some content in 'YourSheet.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes."

But nothing is recoverable and I end up with an empty workbook.

After some analysis with the Open XML 2.5 Productivity Tool, I discovered that the problem is the positioning of the elements within the workbook's XML. When youmanually add a page break to the same workbook and save it, the worksheet elements are ordered as follows:


<rowBreaks count="1" manualBreakCount="1"><brk id="24" max="16383" man="1" /></rowBreaks><legacyDrawingHF r:id="rId2" />

just before the worksheet element close (</worksheet>) and all is well.

However, when I programmatically add the row/page break (as described above), the XML elements are output with the RowBreaks at the end:

<x:legacyDrawingHF r:id="rId2" /><x:rowBreaks count="1" manualBreakCount="1"><x:brk id="24" max="16383" man="1" /></x:rowBreaks>

This causes the corruption error described above.  If I manually edit the XML to reverse the position of the RowBreaks and the LegacyDrawingHF, the workbook opens just fine.

Finally, a question:  Does anyone know how to programmatically make sure that the RowBreaks element gets positioned before the LegacyDrawingHeaderFooter element?  Or know of any other way around this problem?

Thanks.

Programmatically create Excel XML(.xml) file into Excel spreadsheet file(.xls or .xlsx)

$
0
0

Hi

I have a requirement where incoming document I receive is Excel xml file. If I open in notepad, it shows XLS/XML well formed code but opens in excel file table(double click). Initially I thought I can read xml stream(ReadXML(stream))and write the data into excel file using Open XML approach. I was able to find out all data from the xml dataset.Tables[18].Rows.count however there is no way I can find out the header, values, and different worksheets etc. I came out with an array by hardcode number of columns(& custom logic). It worked out verywell for the provided sample excel files. and it would have worked out for any excel xml file as long as the total number of columns are same with any values for those columns.

Now the new excel file has more columns(structure of table changed, additional column) and I cant find out the total number of columns/header dynamically. Also, the data table array is also changed  not dataset.Table[18] anymore.

I thought of using xml serialization and deserialization. This also requires target types or list of columns. Ideally I want to create an excel file(.xls or .xlsx) irrespective of number of rows, number of columns, number of worksheets in the incoming excel xml file. The following are xml elements.

Worksheet
    Tables
      Row
        Cell
          Data

Will you plesae help me to create any sample code to assign xml stream header, stream values and table dynamically? 

Note that I cant use the Interop assembly to convert excel xml file to xlsx file.

Appreciate your assistance.

Thanks
Shri

How to read Excel VBA using openXML in C#

$
0
0

I need to read VBA code which is included in macro enabled excel sheet using OpenXml in c#. Tried to read using following code. But it returns some wired string. Could u please help me to do this.

using (var document = SpreadsheetDocument.Open(filePath, false))
{

VbaProjectPart vbaProjectPart = document.WorkbookPart.VbaProjectPart;

Stream stream = vbaProjectPart.GetStream();

using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
        {
                string vbaCode = reader.ReadToEnd();
        }

}

Thanks in advance!

-Desika-


Unable to read the percentage value from excel using open xml sdk

$
0
0

Hi, 

I am using Open xml SDK to read the data from excel and it is working fine for string data. but when we try to read the cell value containing percentage e.g. 20% it reads that value as 0.2 which is invalid, please let me know if there is any way to solve this issue.

Regards

Rahul Pundlik

Reading and writing in xslx file

$
0
0

Hi All,

I am trying to read and write into an excel file.  I have sample excel template which has header column in first row.  I want to read one of the header (find the column based on header) and write the data into corresponding column.  When ever I tried to read header data then I am getting an integer value.  I am not sure why I am getting that value even if I changed the cell format to string.

using (SpreadsheetDocument document =SpreadsheetDocument.Open(filepath, false))
            {
                WorkbookPart wbPart = document.WorkbookPart;
                Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().FirstOrDefault();

                //MessageBox.Show(worksheet.Name); //getting correct sheet name

                // Retrieve a reference to the worksheet part.
                WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

                // Use its Worksheet property to get a reference to the cell 
                // whose address matches the address you supplied.
                var theCell = wsPart.Worksheet.Descendants<Cell>().FirstOrDefault().CellReference; //getting as A1

                var test = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == theCell).FirstOrDefault().InnerXml;
//test getting as "53" in xml.
                MessageBox.Show(test);
            }

Please help me to fix this issue.

Thanks

Selvakumar R


Read Excel Charts using OpenXML

$
0
0

I need to get chart name and the chart type of two charts where is being in the same excel sheet. 

chart name could be taken from DrawingPart -> WorksheetDrawing -> twoCellAnchor -> GraphicFrame -> NoneVisualGraphicFrameProperties -> NoneVisualDrawingProperties -> Name

From where can I take chart type?

Is it 

DrawingPart -> ChartPart -> ChartSpace  -> Chart -> PlotArea ?

Then How can I map particular chart name to chart type ?

There are two drawingPart and ChartPart within a same WorkbookPart for two different charts. Is there any relational link between chart name and chart type?


Read Excel Charts using OpenXML

$
0
0

Hello Msdns 

I want know how to implement a copy all charts in Excel file and copy to Image DataType in C# 

Read excel file go through the excel workbook -> Worksheet-> chartpart-> Imageslist.add(image);

Please let me know how to do this ? 

Actually after preparing a list of images i am automating that into PPT file so i need to know to do this 

Thanks 

Ranjith 


Ranjith@Learner @Microsoft products

How to embed pdf file in PowerPoint slide via OpenXml SDK

$
0
0

Hi,

I use the following code to read pdf binary file to feed data into EmbeddedObjectPart. But PowerPoint doesn't recognize the embedded file as pdf when I click on the pdf icon in my slide.

OpenXmlPart newPart = null;
newPart = slidePart.AddEmbeddedObjectPart(“application/vnd.openxmlformats-officedocument.oleObject”);
byte[] byteArray = File.ReadAllBytes(pdfFile);
using (Stream newObject = newPart.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
    newObject.Write(byteArray, 0, byteArray.Length);
}
oid = slidePart.GetIdOfPart(newPart);

I used Open Xml SDK2.5 Productivity tool to load a manually created pptx file with embedded pdf file. I saw the pdf content was loaded in the string variable embeddedObjectPart1Data. If I use that data in my code to generate my pptx, the embedded pdf file can be opened when its icon is clicked.

I want to know how Productivity tool reads pdf to a string variable, or how to fix my loading code so that the created embedded pdf file can be opened by PowerPoint.

Thanks in advance!!!

 

Extract default font size of Title/SubTitle in Powerpoint document

$
0
0

Hi,

Short
------
How can I extract Font size of the Title/SubTitle text?

 

Details
--------
I need to extract font sizes of each text at PowerPoint document using OpenXml SDk. And I can extract font details using RunProperties for normal texts, But when if the text is a Title/SubTitle then the font size is not avaialble at RunProperties. So how can I extaract font size details for such texts (ie. Title/SubTitle).

Thanks in advance


- Vins

OpenXML SDK 2.5 Excel File Corruption

$
0
0

I've come across a problem in using the OpenXML SDK 2.5 where the spreadsheet cannot be opened without losing data when you fill cells past column z (all on the same row). The Issue is that in the xml for the sheet, column references AA, AB, AC, etc sort BEFORE column B in the cell list. The consequence of this is that the data for cells A, AA, AB, AC, etc is written, but the data for the cells B to Z is stripped out of the recovered spreadsheet.

Create a blank spreadsheet (.xlsx), and, using the SDK, put data into cells A1, B1, C1, AA1, AB1, AC1. Save the file.

Try to open the spreadsheet in excel and receive the message that the file is essentially corrupt. Recover the spreadsheet and see that there is data in cells A1, AA1, AB1 and AC1, but that in B1 and C1 is missing.

If you go into the xml internals of the spreadsheet and move the structures for cells AA and beyond to after that for column C (in this case) and then reopen the file, it's fine.

I am currently working around this by starting my spreadsheet in column AA.

Mike

The dependancy of DocumentFormat.OpenXml 2.5.0 does not support framework DNX core, version=5.0

$
0
0
Hi, I need to export some data in a mvc 6 rc1 project, but after I installed the DocumentFormat.OpenXml nuget package, I got error as " the dependancy of DocumentFormat.OpenXml 2.5.0 does not support framework DNX core, version=5.0" Does that mean I cannot use DocumentFormat.OpenXml SDK for my MVC 6 rc1 project, please advise.

Wrong values trying to read words count from a Microsoft Word document with OpenXML?

$
0
0

I have a word document and I want to get word count programmatically using OpenXML sdk,
I managed to get word count but openXML returns wrong values.
note that the test document is mixed languages (Arabic, English) Arabic is RTL language.

if you open the word document using Microsoft word in the UI it gives you the correct number of words

but if you go and get the value stored in the app.xml file for the same document you will get different value.

I tried the code in this link
msdn.microsoft.com /en-us/library/office/bb521237(v=office.14).aspx

// To retrieve the properties of a document part.
public static void GetPropertyFromDocument(string document)
{
    XmlDocument xmlProperties = new XmlDocument();

    using (WordprocessingDocument wordDoc =
        WordprocessingDocument.Open(document, false))
    {
        ExtendedFilePropertiesPart appPart = wordDoc.ExtendedFilePropertiesPart;

        xmlProperties.Load(appPart.GetStream());
    }
    XmlNodeList chars = xmlProperties.GetElementsByTagName("Characters");

    MessageBox.Show("Number of characters in the file = " +
        chars.Item(0).InnerText, "Character Count");
}


the file I tested contains

word count is 13 but using upper code it gives me 11!


Replace picture in Word header with open xml

$
0
0

I'm trying to use C# and Open XML to open a docx file and replace images in picture content controls. The picture content controls have tag ID's that match ID's in an xml data file that contains URI's to the replacement pictures. I have it working in the body, but the the header replacement has been unsuccessful.

I have searched many places, but have yet to come across any code that is reliable, consistent and complete (for what it claims to accomplish).  To build my code I've followed the examples from these primary links (as well as a number of others):

http://www.eelcomulder.nl/2012/07/06/open-xml-setting-multiple-picture-content-controls-by-tag-name-without-going-crazy/

and here:

Fill Picture content control in header of word doc using OpenXML

However, the first example does not address headers and did not work with my headers. The second link has a poster with the same issue and he says he got it working. However, I'm not sure how because it certainly does not work for me. He assigns an imageID that is never used. He also uses a function StringToStream which, I ASSUME, takes a filename string and returns a stream with that file in it. I did a little rewriting, bringing that function into a using statement for a new stream.

I admit that, being new to both C# and Open XML, I am confused by some of the operations, especially when working with streams. I've wittled down the function to it's essence so I don't post 150 lines:

//segment from Mainbyte[] byteArray =File.ReadAllBytes(myDoc.FullName);//read myDoc.docx file into bytearray//using (MemoryStream mem = new MemoryStream()) {   // memory stream or file stream?        
using (FileStream mem =newFileStream(assembledDoc.FullName,FileMode.Open)){
    mem.Write(byteArray,0,(int)byteArray.Length);// open mem stream and write file to itXDocument xdoc =newXDocument(data);// get data file that contains replacement info
    replaceHeaderPicture(mem,xdoc);// now try to replace header images}publicstaticvoid replaceHeaderPicture(Stream docStream,XDocument document){
    using (WordprocessingDocument wDoc =WordprocessingDocument.Open(docStream,true)){// ... find all SdtElement descendents that represent pictures and retrieve corresponding URI for replacement// ... descendants is a IEnumerable of SdtElementforeach(SdtElement item in descendants){
            DW.Blip blipElement = item.Descendants<DW.Blip>().FirstOrDefault();string imageId ="default value";

            using (MemoryStream stream =newMemoryStream()){// create bitmap from image file pointed to by imageContent file nameBitmap image =newBitmap(pictureFileName);
                imageId = blipElement.Embed.Value;ImagePartType imagePartType =ImagePartType.Png;//...//Add image and change embeded id.ImagePart imagePart =null;OpenXmlElement ancestor = item.Parent;Type p = ancestor.GetType();//...//for header elementsHeaderPart headerPart =((Header)(ancestor)).HeaderPart;// ancestor=item.Parent
                imagePart = headerPart.AddImagePart(imagePartType);
                image.Save(stream,ImageFormat.Jpeg);
                stream.Position=0;
                imagePart.FeedData(stream);
                blipElement.Embed= headerPart.GetIdOfPart(imagePart);}}}}

Why grab the imageID if it's not used? is blipElement automatically saved? Does it matter whether I use a Filestream or MemoryStream (I tried both)?

Most importantly, what is wrong with what am I doing here so that the image has not been replaced in the header?  


OpenXML Excel Date Formatting

$
0
0

 am learning to read the data from excel using OpenXml SDK and i do not understand the way the date works. I wrote a piece of code that reads the data and binds it to a grid and everything works but the date works if i don't choose the default format. The code works if i choose the following option (mm/dd/yyyy) in Excel

choose specific date formatAnd it does not work if i choose the default date format(*mm/dd/yyyy where * indicates regional setting) which is

Choose default date format

Even though the value of NumberFormaId is 14, the following line always returns null if i choose default date format

l_numbering_formats = l_workbookPart.WorkbookStylesPart.Stylesheet.NumberingFormats

I then used the OpenXml Productivity tool and found that if i choose the specific date format (not the default format), i see NumberingFormat node with the following code

<x:numFmts count="1" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:numFmt numFmtId="165" formatCode="m/d/yyyy;@" /></x:numFmts>
But i do not see the node if i choose the default date format. Could someone explain why? i tried to read the documentation and OpenXml tutorial but still don't find an answer.

The element has invalid child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:p'.

$
0
0

Hello, I am getting below error message when i apply transform to a XML file:

The element has invalid child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:p'. 
List of possible elements expected: <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlPr>.

Affected Node:

<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pPr><w:pStyle w:val="ListParagraph" /><w:numPr><w:ilvl w:val="1" /><w:numId w:val="8" /></w:numPr></w:pPr><w:r><w:rPr /><w:t>For more info on the documentation</w:t></w:r></w:p>

and complete WordProcessingML is as under:

<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:exslt="http://exslt.org/common"><w:body><w:customXml w:uri="DitaOXmlPub" w:element="topic"><w:customXmlPr><w:attr w:name="id" w:val="topic_wdw_4cn_kq" /><w:attr w:name="class" w:val="- topic/topic " /><w:attr w:name="TopicFullPath" w:val="https://.../Non_Working.xml" /></w:customXmlPr><w:customXml w:uri="DitaOXmlPub" w:element="title"><w:p><w:pPr><w:pStyle w:val="Heading8" /></w:pPr><w:r><w:rPr /><w:t>SYSTEM DATABASE</w:t></w:r></w:p></w:customXml><w:customXml w:uri="DitaOXmlPub" w:element="shortdesc"><w:p><w:pPr><w:pStyle w:val="shortdesc" /></w:pPr></w:p></w:customXml><w:customXml w:uri="DitaOXmlPub" w:element="body"><w:customXml w:uri="DitaOXmlPub" w:element="ul"><w:customXmlPr><w:attr w:name="id" w:val="ul_kmx_cty_vq" /><w:attr w:name="class" w:val="- topic/ul " /></w:customXmlPr><w:customXml w:uri="DitaOXmlPub" w:element="li"><w:p><w:pPr><w:pStyle w:val="ListParagraph" /><w:numPr><w:ilvl w:val="0" /><w:numId w:val="8" /></w:numPr></w:pPr><w:customXml w:uri="DitaOXmlPub" w:element="p"><w:customXml w:uri="DitaOXmlPub" w:element="b"><w:customXml w:uri="DitaOXmlPub" w:element="u"><w:r><w:rPr><w:b /><w:u w:val="single" /></w:rPr><w:t>More details</w:t></w:r></w:customXml></w:customXml></w:customXml><w:customXml w:uri="DitaOXmlPub" w:element="ul"><w:customXmlPr><w:attr w:name="id" w:val="ul_kgw_2ty_vq" /><w:attr w:name="class" w:val="- topic/ul " /></w:customXmlPr><w:customXml w:uri="DitaOXmlPub" w:element="li"><w:p><w:pPr><w:pStyle w:val="ListParagraph" /><w:numPr><w:ilvl w:val="1" /><w:numId w:val="8" /></w:numPr></w:pPr><w:r><w:rPr /><w:t>For more info on the documentation</w:t></w:r></w:p></w:customXml></w:customXml></w:p></w:customXml></w:customXml></w:customXml></w:customXml></w:body></w:document>

Any help on this is much appreciated.


Thanks, Nilesh

Is it advisable to try OpenXML for generation of documents?

$
0
0

I am trying to generate documents in open xml format (.docx or .dot). I googled a lot. All the content in the msdn site is archived and stated to be outdated. The openxmldeveloper.org site is not maintained. Brian joans and erich white are two authors writing blogs in this topic. They also seem to stop post new contents. I tried to use the openxml sdk 2.5. The support seems to be stopped. Any comments friends?

What are the alternative if I want to generate documents from database avoiding third party tools?

within tags

$
0
0

Hello,

Is it possible to have <Codeblock> style within <li> tags? This is what i am trying to achieve using code given:

<body class="- topic/body "><p class="- topic/p ">This is a test for <codeblock xml:space="preserve" class="+ topic/pre pr-d/codeblock ">codeblock</codeblock> inside a list item</p><ul id="01ec1811-639a-4389-ad1e-deb6d775325f" class="- topic/ul "><li class="- topic/li ">This is <b class="+ topic/ph hi-d/b ">Line</b> No. 1</li><li class="- topic/li ">This is Line 2<codeblock xml:space="preserve" class="+ topic/pre pr-d/codeblock ">First Codeblock</codeblock></li><li class="- topic/li "><p class="- topic/p ">Voici un autre codebloc dans une sous liste</p><ul id="e6b8268b-d4f1-4b1f-b5e8-c0744720cd49" class="- topic/ul "><li class="- topic/li ">This is line 3</li><li class="- topic/li ">This is line 4 <codeblock xml:space="preserve" class="+ topic/pre pr-d/codeblock ">Second Codeblock</codeblock></li></ul></li></ul></body>


Thanks, Nilesh

Issues creating OpenXML Excel files in 2010 with Array formulas

$
0
0

When I create a document using OpenXML, and use Subtotal, Aggregate or {} array formulas, they are not calculated when the user opens them. This is a WebAPI application, file is generated on server.

If the user selects each cell and presses F2 and then Enter, the formula is calculated. For the pure array formulas they have to go to the formula bar and press Ctl + Shift + Enter.

This is, of course, completely useless. The problem does not occur with Excel 2016 (I don't have a copy of 2013 to test).

Any thoughts on this?

Viewing all 1288 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>