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

Copy Word Document content in a table to Excel with Paragraph formatting using OpenXML

$
0
0

Hi,

I have a word document(.docx) which contains a table. The user can enter the information in a paragraph style. In each cell they can add 1 or more paragraph information. I would like to read the contents in each cell and create a excel SpreadSheet. Each cell text content in excel needs to be copied to the new Excel file. 

I used OpenXML and I can read the contents using InnerText. But the Paragraph formatting(space between two paragraph) are missing. When I copied to the Excel cell, all the paragraph text are merged in to single. How to copy with the Paragraph style to the Excel cell.

Thanks in advance


R.Mani | http://rmanimaran.wordpress.com


Reply comment use Open XML SDK

$
0
0

Hello friends.

I want use C#, read file docx and add new comment, add reply comment use Open XML SDK.

I described as in the photo link:https://social.msdn.microsoft.com/Forums/getfile/1323765

please help me.

Thanks!




Bookmark whole text between HEADINGS

$
0
0

See below code, which will create bookmark where style is HEADING1. How can we modify this so that text between HEADING1 and next HEADING1 element gets selected under BOOKMARK1.

So Bookmark should include text starting from HEADING1... and should end before HEADING2 starts

  using (var doc = WordprocessingDocument.Open(@"D:\DocFiles\Scan.docx", true))
                {
                    paragraphs = doc.MainDocumentPart.Document.Body
                                    .OfType<Paragraph>().ToList();
                    int bookmarkId = 0;
                    // MyDocuments.Body is a WordProcessDocument.MainDocumentPart.Document.Body
                    foreach (Paragraph para in paragraphs.ToList())
                    {
                        // if the paragraph has no properties or has properties but no pStyle, it's not a "Heading1"
                        ParagraphProperties pPr = para.GetFirstChild<ParagraphProperties>();
                        if (pPr == null || pPr.GetFirstChild<ParagraphStyleId>() == null) continue;
                        // if the value of the pStyle is not Heading1 => skip the paragraph
                        if (pPr.GetFirstChild<ParagraphStyleId>().Val != "Heading1") continue;

                    // it's a paragraph with Heading1 style, insert the bookmark

                    // the bookmark must have a start and an end
                    // the bookmarkstart/end share the same id
                   
                    BookmarkStart bms = new BookmarkStart() { Name ="BookmarkbyLokesh" + bookmarkId, Id = bookmarkId.ToString() };
                    BookmarkEnd bme = new BookmarkEnd() { Id = bookmarkId.ToString() };++bookmarkId;

                    // Insertion of bookmarkstart after the paragraphProperties
                    pPr.InsertAfterSelf(bms);
                    bms.InsertAfterSelf(bme);

                    // The bookmarkend can be inserted after the bookmarkstart or after the object the bookmark must surrounding
                    // here we will insert it after bms. If you want to surround an object, find the object within the paragraph and insert it after

                }
                doc.Save();
                doc.Close();

Retain formatting for the paragraph

$
0
0

Below code works fine and print the paragraph... we are able to get style and para text

But when we display the paragraph, it looses formatting. How can we retain formatting in below mentioned code

Section where we r printing text

  foreach (var p in paraWithText)
            {
                    Response.Write(p.StyleName + " -" + p.Text);
             }

Output comes like this:

Heading2 -Financial definitions
BodyText -In this Agreement:
ListAlpha2 -moneys borrowed and debit balances at banks or other financial institutions;
ListAlpha2 -any acceptances under any acceptance credit or bill discount facility (or dematerialised equivalent);
ListAlpha2 -any note purchase facility or the issue of bonds
ListAlpha2 -any Finance Lease;

See full code below

     const string fileName = @"D:\DocFiles\Scan.docx";
            const string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
            const string stylesRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
            const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
            XNamespace w = wordmlNamespace;
            XDocument xDoc = null;
            XDocument styleDoc = null;

            using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.Read))
            {
                PackageRelationship docPackageRelationship =
                  wdPackage
                  .GetRelationshipsByType(documentRelationshipType)
                  .FirstOrDefault();
                if (docPackageRelationship != null)
                {
                    Uri documentUri =
                        PackUriHelper
                        .ResolvePartUri(
                           new Uri("/", UriKind.Relative),
                                 docPackageRelationship.TargetUri);
                    PackagePart documentPart =
                        wdPackage.GetPart(documentUri);

                    //  Load the document XML in the part into an XDocument instance.  
                    xDoc = XDocument.Load(XmlReader.Create(documentPart.GetStream()));

                    //  Find the styles part. There will only be one.  
                    PackageRelationship styleRelation =
                      documentPart.GetRelationshipsByType(stylesRelationshipType)
                      .FirstOrDefault();
                    if (styleRelation != null)
                    {
                        Uri styleUri = PackUriHelper.ResolvePartUri(documentUri, styleRelation.TargetUri);
                        PackagePart stylePart = wdPackage.GetPart(styleUri);

                        //  Load the style XML in the part into an XDocument instance.  
                        styleDoc = XDocument.Load(XmlReader.Create(stylePart.GetStream()));
                    }
                }
            }

            string defaultStyle =
                (string)(
                    from style in styleDoc.Root.Elements(w + "style")
                    where (string)style.Attribute(w + "type") == "paragraph" &&
                          (string)style.Attribute(w + "default") == "1"
                    select style
                ).First().Attribute(w + "styleId");

            // Find all paragraphs in the document.  
            var paragraphs =
                from para in xDoc
                             .Root
                             .Element(w + "body")
                             .Descendants(w + "p")
                let styleNode = para
                                .Elements(w + "pPr")
                                .Elements(w + "pStyle")
                                .FirstOrDefault()
                select new
                {
                    ParagraphNode = para,
                    StyleName = styleNode != null ?
                        (string)styleNode.Attribute(w + "val") :
                        defaultStyle
                };

            // Retrieve the text of each paragraph.  
            var paraWithText =
                from para in paragraphs
                select new
                {
                    ParagraphNode = para.ParagraphNode,
                    StyleName = para.StyleName,
                    Text = ParagraphText(para.ParagraphNode)
                };

            foreach (var p in paraWithText)
            {
                    Response.Write(p.StyleName + " -" + p.Text);
             }

Does Microsoft support to export embedded object from excel, such as pdf, jpg, etc?

$
0
0

Hi,

I am trying to find a way to read embedded object from excel, such as pdf, jpg. Does Microsoft support that?  

Thanks,

How to identify continuous section break in word

$
0
0

Hi,

Can any one suggest me how to identify continuous section break in word.

Thanks,

Gayatri

How to reset the auto numbers in each section of word document

$
0
0

Hi,

Is there any way by which I can reset auto numbers in each section of word document.

Thanks,

Gayatri

because we found a problem with its contents part:/word/document.xml, Line:0,Column:0

$
0
0

I inserted a QR picture into .docx file with OpenXml sdk 2.0. but sometimes, the created document cannot open, and prompt the message above.

I compared the document that work with document that don't work with openxml sdk productivity tool, only a few difference that look harmless (I am new to OpenXml).

Unlike other thread on the web, my error message says Line:0,Column:0 .

How can I know what's wrong?

Update: The Word client version is 2016. I had not tried 2013 yet.  I just download WPS 2019 for test and it can open the "corrupt" .docx file.

But I still don't know what makes ms word think the file is corrupt and how can it be created and saved with openxml.

By the way, wordprocessingdoc.MainDocumentPart.AddImagePart(ImagePartType.Png) does not work on my environment, so, I followed sugguest from https://stackoverflow.com/questions/18569113/open-xml-sdk-addimagepart-change-image-location-from-media-to-word-mediato add the image file into zip package and it works.

Update: I tried Word 2013 on a win 7 computer. It behave the same as Word 2016 on win 10 for this issue.

I posted in another forum:
https://social.msdn.microsoft.com/Forums/en-US/92a94048-3339-4943-90e0-03fe8d0632a9/cannot-open-docx-after-inserting-picture-with-openxml-problem-with-partworddocumentxml65292?forum=os_binaryfile#92a94048-3339-4943-90e0-03fe8d0632a9

How to get track changes which are not accepted or rejected in excel

$
0
0

I want to find out pending track revisions in excel

Currently I am getting all the track revisions  programmatically in an excel file using open xml and c#

as below

            List<string> excelrevisions = new List<string>();
            using (var document = SpreadsheetDocument.Open(fileName, false))
            {
                if (document.WorkbookPart.WorkbookRevisionHeaderPart != null
                    && document.WorkbookPart.WorkbookRevisionHeaderPart
                        .GetPartsCountOfType<WorkbookRevisionLogPart>() != 0)
                {

                    foreach (DocumentFormat.OpenXml.Packaging.WorkbookRevisionLogPart logpart in document.WorkbookPart.WorkbookRevisionHeaderPart.WorkbookRevisionLogParts)
                    {
                        excelrevisions.Add(logpart.Revisions.InnerText);

                    }
            }

But my requirement is to identify  if any  track changes are there in excel  which is not yet accepted or rejected

I am not able to identify whether all the changes are accepted or rejected .

                     

After save document, I can not open saved document OpenXML SDK 2.5

$
0
0

After I save document (object Document open myfile.docx and save it as myfileEdited) Document.SaveAs("myfileEdited.docx"); or I use Close(); when I call 

using (WordprocessingDocument Document2 = WordprocessingDocument.Open("myfileEdited.docx", true)) 
{
// ...
}

it crash here and I got message 

"Unhandled Exception: System.IO.IOException: The process cannot access the file 'path to file' because it is being used by another process."


Excel was able to open the file by repairing or removing the unreadable content

$
0
0

Hi there,

I just started using Open XML SDK and created a program to generate XLSX. It works well but the only problem is that i get a warning message while opening the xlsx and then displays the data correctly.

"Excel was able to open the file by repairing or removing the unreadable content."
"Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded."

/xl/worksheets/sheet1.xml

/xl/worksheets/sheet2.xml

I tried to windiff the xml generated xml after correction and before correction by office but in vain. I guess the problem is related to coding and could anyone please shed some light on this issue?

Thanks,

Holy

I post the code below and here is steps I do on the code.

1 .Open an XLSX file (from template with no data)

2. Set an value on a particular cell

3. Fill data on sheet1

4. Fill data on sheet2

        public Stream Create(Stream fromXlsxTemplate, IDataReader data)
        {
            if (fromXlsxTemplate == null)
            {
                throw new ArgumentNullException("fromXlsxTemplate");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            fromXlsxTemplate.Position = 0;

            // Open SpreadSheet from Resource Stream (Open XML)
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fromXlsxTemplate, true))
            {
                WorkbookPart wbPart = document.WorkbookPart;
                Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().ElementAtOrDefault(0);

                WorksheetPart wsPart =
                    (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

                while (data.Read())
                {
                    WriteHeader(data, wbPart, wsPart);
                }

                data.NextResult();

                //Write to Sheet 1
                WriteSheet1(data, wsPart);

                //Move to result for sheet2
                data.NextResult();

                Sheet sheetDettaglo = wbPart.Workbook.Descendants<Sheet>().ElementAtOrDefault(1);

                WorksheetPart wsPartDet =
                    (WorksheetPart)(wbPart.GetPartById(sheetDettaglo.Id));

                //Write to Sheet 2
                WriteSheet2(data, wsPartDet);


            }

            return fromXlsxTemplate;
        }

        private void WriteHeader(IDataReader reader, WorkbookPart wbPart, WorksheetPart wsPart)
        {
            // A2 Cell
            Cell a2Cell = wsPart.Worksheet.Descendants<Cell>().
              Where(c => c.CellReference == "A2").FirstOrDefault();

            if (a2Cell != null)
            {
                string a2CellValue = a2Cell.InnerText;
                var stringTable = wbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
                if (stringTable != null)
                {
                    a2CellValue = stringTable.SharedStringTable.ElementAt(int.Parse(a2CellValue)).InnerText;
                }
                a2Cell.DataType = CellValues.String;
                a2Cell.CellValue = new CellValue(string.Format(a2CellValue, DateTime.Parse(reader.GetValue(0).ToString()).ToString("dd/MM/yyyy")));

            }
        }

        private uint WriteSheet1(IDataReader reader, WorksheetPart wsPart)
        {
            SheetData sheetData = wsPart.Worksheet.GetFirstChild<SheetData>();
            uint rowIdx = 6; // Start from row 6
            while (reader.Read())
            {
                Row newRow = new Row() { RowIndex = new DocumentFormat.OpenXml.UInt32Value(rowIdx) };

                //Value fo Cell Emissioni
                Cell cell = new Cell() { StyleIndex = (UInt32Value)1U };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("Emissioni")).ToString()); //
                newRow.AppendChild(cell);

                //Value fo Cell Operazioni
                cell = new Cell() { StyleIndex = (UInt32Value)9U };
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("Operazioni")).ToString());
                newRow.AppendChild(cell);

                //Value fo Cell Totale
                cell = new Cell() { StyleIndex = (UInt32Value)9U };
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("Totale")).ToString());
                newRow.AppendChild(cell);

                sheetData.Append(newRow);
                rowIdx++;
            }
            return rowIdx;
        }

        private uint WriteSheet2(IDataReader reader, WorksheetPart wsPartDet)
        {
            uint rowIdx;
            SheetData sheetDet = wsPartDet.Worksheet.GetFirstChild<SheetData>();
            rowIdx = 2; // Start from row 2

            while (reader.Read())
            {
                Row newRow = new Row() { RowIndex = new DocumentFormat.OpenXml.UInt32Value(rowIdx) };

                //n_polizza
                Cell cell = new Cell() { };
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("n_polizza")).ToString());
                newRow.AppendChild(cell);

                //c_attivita
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_attivita")).ToString());
                newRow.AppendChild(cell);

                //d_effetto
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("d_effetto")).ToString());
                newRow.AppendChild(cell);

                //descrizione
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("descrizione")).ToString());
                newRow.AppendChild(cell);

                //tipo_liquidazione
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("tipo_liquidazione")).ToString());
                newRow.AppendChild(cell);

                //d_carico
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("d_carico")).ToString());
                newRow.AppendChild(cell);

                //timestamp
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("timestamp")).ToString());
                newRow.AppendChild(cell);

                //d_assunzione
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("d_assunzione")).ToString());
                newRow.AppendChild(cell);

                //c_operazione_online
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_operazione_online")).ToString());
                newRow.AppendChild(cell);

                //c_prodotto
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_prodotto")).ToString());
                newRow.AppendChild(cell);

                //c_fondo 					
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_fondo")).ToString());
                newRow.AppendChild(cell);

                //descrizione_fondo
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("descrizione_fondo")).ToString());
                newRow.AppendChild(cell);

                //descrizione_prodotto
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("descrizione_prodotto")).ToString());
                newRow.AppendChild(cell);

                //importo
                cell = new Cell() { };
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("importo")).ToString());
                newRow.AppendChild(cell);

                //c_abi
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_abi")).ToString());
                newRow.AppendChild(cell);

                //descrizione_lv
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("descrizione_lv")).ToString());
                newRow.AppendChild(cell);

                sheetDet.Append(newRow);
                rowIdx++;
            }
            return rowIdx;
        }

xlsx not opening in *some* mobile apps

$
0
0

Hello,  

I have an app that generates an xlsx download through the OpenXML 2.5 SDK.  It opens in Excel on the desktop without issue. Validation (both in code and by using the Open XML 2.5 Productivity Tool) reports no errors.

My users have reported that iPhone and iPad fail to open it using either Excel or Google Sheets.  My Android device opens it using Google Sheets or Excel without issue, but Polaris Office 6.5.2 shows only the last row of data.

If I open the file on the desktop in Excel and re-save it, my Android now can open it with Polaris Office as well.  iPad is opening it properly in whatever its default app is.

Comparing the original file with the re-saved one shows that they're vastly different (different namespaces are in use, the cell contents have all been moved to the shared strings table, etc.)

It would seem that *something* is wrong with the file that isn't detected with the SDK validation.  Does anyone know of a way to debug these issues?  A more rigorous validator?  Any insight would be most appreciated.

thanks

How to read file excel row and sheet specific using openXML ?

$
0
0

How to read excel row and sheet specific using open XML C# into sql server table  ? 

I have a excel file with no format I mean I can only read excel file in first sheet and first row, how i read second sheet and row A6? ( the names of a columns begin in A6  to D6)

I try with oledb and interoop and i read open xml is the choice for that

do you have examples , pls im newbie and late on the work 

Multiple Tables to Word

$
0
0

Hi,

I was wondering if someone may be able to advise me on this before i set off in the wrong direction?

I've had the OpenXML PowerTools working perfectly well producing a multi-lined single table of results in Word.  However, some bright spark has now decided that we now need to separate the results into subcategory's, with a title for each (in my mind this now means separate tables?)

The xml output files were previously straightforwards, but now I've spent a little bit of time breaking the output down into the required subcategory's its becoming more obvious that I'm not going to be able to just pass the xml and the document template files to the DocumentBuilder function!  I'm a bit puzzled as to the structure of the xml for one, and what (if possible) would work.

The fun part is that there isn't a set number of subcategory's per report, it varies each time, so sadly there's no way to pre-set tables in Word any more.

Will I have to go for a full on custom Docx creation process?  I can't see how this could be done with PowerTools.

Any advice would be greatly received!




Format one word document like another

$
0
0

Hello,

I have a requirement to format a word document (.docx) to make it look like another. I will have documents from 2 different sources. Format of the document I receive from source 1 is the standard. I will have to format the document I receive from source 2 so that it completely looks like the first document.. e.g.: the header style, table styles, paragraph fonts, page header/footer etc.

Is there an option via OpenXML sdk or any other component so that I can copy styles from one document and apply onto another?

Thanks in advance.



Open xml + hyperlink formula

$
0
0

Can we use hyperlink formula in open xml sdk to create a hyperlink cell in excel.

Any sample code.

Problem with opening an xlsx made with openXML

$
0
0

Hi everyone, 

I have a problem with a file genered with OpenXML SDK 2.8.1, when I open it, I get the error "Excel was able to open the file by repairing or removing the unreadable content" and below : "Removed Part: Print options."

Log file : 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error085080_01.xml</logFileName><summary>Errors were detected in file 'C:\Users\mij.ATTICPLUS\Documents\Attic\WinDesc-2016-A\AFF726\phase_001\WExport\Excel\nhfnhf - Lot N°06 METALLERIE.xlsx'</summary><removedParts summary="Following is a list of removed parts:"><removedPart>Removed Part: Print options.</removedPart></removedParts></recoveryLog>

I try to search the problem using the openXML SDK productivity tool but cannot find it.

Thank you by advance for your help

OpenAndRepair Parameter Office Introp Concern

$
0
0

Hi,

i am creating application in C# using office interop.

while opening file for Word,Excel,PowerPoint there is paramter called 'OpenAndRepair' , if i set this objOpenAndRepair = true, will it cause any effect in terms of file output ?

i mean , if i am trying to print and get text from doc or excel file, after apply repair does it effect any thing in output ?

assuming, repair will do some internal changes to repair data, so is that possibility that data is altered or changed by using this paramter ?

Thanks

powerpoint automation

$
0
0
Is there a way to create a powerpoint on the fly via a web app built in C#? Not only create the slide, but populate it with data as well from legends, colors, etc.? 

Document (.docx) creation using OpenXML not read certain CSS

$
0
0

hi, Im creating a docx document using OpenXML which convert HTML file to word. However certain css are not reflected to word document such as to style table with border

th {
        border: 0.5px solid black;
    }

What is actually happened, cause the file transform not follow as CSS written at HTML file? Can anybody assist me.

Viewing all 1288 articles
Browse latest View live


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