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

Read All Tables from a Word File

$
0
0

I am trying to Read All Tables in a MS-Word Document. I am only getting the Tables in Page 1 only. Tables in other page are not getting in code.

 using (WordprocessingDocument package = WordprocessingDocument.Open(@"C:\B20150806_14h18m57.docx", false))
 {

 var tables = mainPart.Document.Descendants<Table>().ToList();
}

using Openxml


Thanks and Regards

Er.Pradipta Nayak
Visit my Blog
Xchanging


How to add TImeSheet Line Classification using Csom on Project Online

$
0
0

Hello all,

    How to add TImeSheet Line  Classification using Csom on Project Online

Reg: docPartCategory (Document Part Category Filter)

$
0
0

Hello,

Can you please help in understand  whats the purpose of docPartCategory (Document Part Category Filter) and docPartin the content control?

thanks

Vimal


regards Vimal

How to 'parse' raw HTML text to Word format using OpenXML?

$
0
0

Hello,

Fairly new to using OpenXML. I have a site which uses a rich-text editor (ckeditor IIRC), which outputs text into it as raw HTML data. Sample is below of a simple text with an inline-image:

<p>PHD Abstract</p><p><strong>Title</strong></p><ul><li>Item 1</li><li>Item 2</li></ul><p><img alt="" border="0" hspace="0" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxASEBAUDxQWDxQQFRAQFRAPEA8QFBAPFBQWFhQXFBQYHCggGBolHBQYITEhJSkrLi4uGB8zODMsNygtMCsBCgoKDg0OGhAQGCwcHB8sLCwsLCwsLCssLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCs3LCsrK//AABEIAK4BIgMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAAAQIDBAUGB//EADoQAAEDAwIDBgMHAwMFAAAAAAEAAhEDITEEEkFRYQUTInGBkQYyoRRCscHR4fBSYvEjcoIHFTOS0v/EABoBAQADAQEBAAAAAAAAAAAAAAABAgMEBgX/xAAiEQEBAAICAgMAAwEAAAAAAAAAAQIREiEDQQQxURMigXH/2gAMAwEAAhEDEQA/APdSnKgmulzpSiVGUSgnKJ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DYfKL2PpN7XHkmyvAaXiZAiDg85VrKLS2GgA2eXcSSceSXcW5biupVMi20QcbXtIjcT7D6Qojd8r7mC7adoAZEmeXD2Vwe4fKYm3qf2gSp0m73OmHQMuAnxSLfmpit7Z2AlhAAfPI7S0CIAnPESrNDQf4gIlplvisHDAA4ym522bAukNdwDg75fUEZUqeumQABHQCRm6mZ6q0sW6jYXbnmH23Q3wg/wBtrHqqqVHcJu1pue82kRaOXms51rw7nFyCZGPJZv8AuFTxBsAWF28vywou6rydGpTLvmbaQYDYw3AibXWd1MCQ1sSB4SSL3WLU6irudLsQDmJjkqaXaJ2h5EknZfgenunHJnc5tt1dBoIB8NgLZOZAOeKzmg4knhixaSB7qRq7oJnA4m3ks2tc5gaWHNnSXSZE8/RWx3elcjbonQPE33H6oVQc7kPc/ohW/sjp/9k=" style="border:0px solid black; height:174px; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; width:290px" vspace="0" /></p>


I store the data above in a DB for various purposes, mainly 1.) loading it back to display and 2.) generating some word documents. I currently have a library grabbed from the internet for HTML to OpenXML calledHTML2OpenXML.

The problem is, the current library I am using cannot parse the stored data completely (simplest example is the image is not displayed, as well as bulleted items).

Is there a recommended way of parsing HTML back to Open XML? ckeditor can already do the reverse (you copy-paste data from Word to the ckeditor text-area on the browser).

Thank you!





iterate openXML word c#

$
0
0

This is my first project with OpenXML so please pardon me if this is very basic question

Below is my word file and I want to read this file and write it into the database. So my question is how can I read this file and create data table which represent this data. I am using c# language. 

Deal Component

May ‘15

April ‘15

May ‘14

Cash Flow Senior Debt

(x EBITDA)

<$7.5MM EBITDA 1.50x-2.00x

>$10.0MM EBITDA 2.00x-3.50x

>$20.0MM EBITDA 3.00-4.25x

<$7.5MM EBITDA 1.50x-2.00x

>$10.0MM EBITDA 2.00x-3.50x

>$25.0MM EBITDA 3.00-4.25x

<$7.5MM EBITDA 1.50x-2.50x

>$10.0MM EBITDA 2.00x-3.50x

>$25.0MM EBITDA 3.00-4.00x

Thanks

OPEN XML SDK 2.0

$
0
0

I have file excel with 42 column and 1500 row. When i used OpenXML SDK 2.0  write  border for cell then it NO draw borther. But when i used file excel with 17 column and 1500 row then ok.

Hope everybody help. Thank.

How to Copy paragraph between two merge fields using openxml

$
0
0

Hi,

I need to copy the paragraph between two merge fields in word document and  paste that paragraph above that second merge fields.

For Example:

«Start»

The name is:«Name»

The Age is:«Age»

«End»

The result should be,

«Start»

The name is:«Name»

The Age is:«Age»

The name is:«Name»

The Age is:«Age»

«End»

How to implement above scenario using openxml.Please provide the solution.

Clone the paragraph and paste that paragraph with repeat in openxml

$
0
0

Hi

I have a template that has this line of code:

The name is:«Name»

The Age is:«Age»

so i need to clone this paragraph then foreach it on it and result:

The name is:«Name»

The Age is:«Age»

The name is:«Name»

The Age is:«Age»

The name is:«Name»

The Age is:«Age»

The name is:«Name»

The Age is:«Age»



Does an Android version of the Open XML SDK exist?

$
0
0

Does anyone know if Microsoft plans to release an Android version of the Open XML SDK as open source as it has for Windows? I found the github site with the Windows version, which is now up to 2.6. I also found what appears to be a Nuget version built using 2.5. Both of these would require WindowsBase.dll, which one would not have when building for Android. I did find an open source version of the SDK ported to Android put together by "wotisname" on github, but this is probably built on an older version of the open source and has problems (it's missing the spreadsheet support and has some bugs).

I'm looking for an officially supported copy published of the Open XML SDK for Android published by Microsoft that I can use with Xamarin Android.

I apologize for not including hyperlinks to my findings above, but I must be a nubie to this site and it wouldn't let me.

Thanks in advance.


Create Excel Pivot Table With External Datasource

$
0
0

Hi guys.

Was wondering if somebody here has some advice for me, I've been bumping my head against this for a week now. I would like to create an excel spreadsheet with a database connection and a pivot table from code behind, using either C# or VB.Net. Initially I managed to do this with Microsoft.Office.Excel.Interop but numerous websites have told me this isn't a good idea for deployed web applications so I went looking for other alternatives, in this case the Open XML SDK.

With the Open XML SDK I've managed to create an Excel Spreadsheet as well as a connection to the database within it. This works perfectly and I can manually create a PivotTable using the connection without any problem. But creating the Pivot Table programatically is proving to be a bit of a snag. I've manually created a test Excel document and reflected the code using the Open XML SDK 2.0 Productivity Tool, but it seems as if the code that the Productivity Tool generates adds the Pivot Table fields manually, something which I can't do as I don't know which table will be used during program execution. Same goes for the SharedStringTable. As far as I have it the fields should be automatically read from the datasource.

Does anybody have an example nearby that I can use to create the Pivot Table or perhaps have some code/advice that will help? Below is a dropbox link for my test application. I do think I'm either missing something simple or what I'm trying to do is simply impossible, but looking forward to hearing from the experts.

Test app link: https://www.dropbox.com/s/en9j1wyx3i5uoff/OOXML%203.zip?dl=0

Thanks!

Difficulties Updating Columns Collection In Worksheet Using SAX

$
0
0

Hey guys,

I am working on a project where I am using a DataReader to read large amounts of data (100,000+ rows, 20+ column) and am streaming those into a worksheet using the SAX OpenXmlWriter methods to avoid high memory usage.  This is working very fast and efficiently so far as I can tell.  However, as I am writing these values I am also storing the maximum length outputted in each column so I can then go on and update the Columns collection in the worksheet to set appropriate default widths when the spreadsheet is opened in Excel.  Very frustratingly the Columns collection is BEFORE the SheetData meaning that there seems to be no way to update it after writing all my data!  If I try to access the Columns collection via the DOM then it loads the whole spreadsheet into memory and causes my app to crash with an OutOfMemoryException.

Do you guys know of any clever way of updating the Columns collection once the SheetData has finished being outputted without forcing the whole document to be loaded into memory or having to go through the WHOLE spreadsheet with an OpenXmlReader and then output it all again to a new sheet with a separate OpenXmlWriter? Any help would be much appreciated.

Cheers,

James.

How to set a cell's formula

$
0
0
I just need to know how to set a cell's formula in excel. PLEASE

Inlining altChunks reliably

$
0
0

Hi all,

I have to cope with DOCX files generated by third-party software. There are several altChunks embedded in the document which mess up the styling of the master document pretty well.

I noticed that, once opened in Word, updated, and saved back to a new file, the altChunks seem to be inlined in the main document. Is there a library or algorithm that I can do this inlining WITHOUT using Word via interop etc.

Thanks!

Mirko

PowerPoint: How to get inset(padding) for text inside a shape.

$
0
0

Is there any method to get the real padding(inset) for text inside a shape with OpenXML SDK?

Text box attribute:

Vertical alignment: Middle

Text direction: Horizontal

Autofit: Do not Autofit

Internal margin: left 0.1cm, top 0cm, Right 0.2cm, bottom 0cm.

The text inside a shape, it's at the middle of the shape. so, the real top margin is not 0cm. How can I cat get the real margin form SDK.

Thank you.

Auto wrap text is not applied with the style

$
0
0

Hello experts,

So, i have a template that i edit with c# to insert some data from a database.

On a specific cell i'm apllying a style that comes with the template using it's styleIndex, and associating it to the cell.
The problem is all the specific style formats of the associated styleIndex are apllied (font color, font size, font, etc..) except for the wrap text.

Only if i click on the style, or hover over other style, on the opened excel sheet the wrap text is applied.

The code to update the cell is the following:

Cell cell = GetCell(worksheetPart.Worksheet,
                                         columnName, rowIndex);

cell.CellValue = new CellValue(text);
cell.DataType =
         new EnumValue<CellValues>(CellValues.String);

 if (styleIndex > 0)
       cell.StyleIndex = styleIndex.Value;

 // Save the worksheet.
 worksheetPart.Worksheet.Save();

I know that i'm applying the right styleIndex because i see it when i open the excel. The Wrap text for the cell is also enabled, but it's like the cell is not updated, i have to force it by clicking again on the style.

below is the code to create the excel from template

using (SpreadsheetDocument excelDocument =
                    SpreadsheetDocument.Open(s, true))
                {

                    SheetData sheetData = new SheetData();
                    Sheet sheet = excelDocument.WorkbookPart.Workbook.Descendants<Sheet>().Last();
                    var propRef = entityData["Reference"].ToString();
                    sheet.Name = propRef;

                    WorksheetPart worksheetPart = GetWorksheetPartByName(excelDocument, propRef);

// args[WorksheetPart, String, Row, Column, StyleIndex]
UpdateCell(worksheetPart, "Some text", 12, "B", 40);
excelDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.Save();
}

Best regards,
johny


C# Open XML SDK 2.5 - make Shape uneditable, unresizeable and immovable

$
0
0

I have a method AddShape() inside a C# script using the OpenXML SDK 2.5, which adds aShape to my Presentation. When I open the Presentation in Powerpoint I canmove, edit the text and resize the Shape. I want to "lock" the Shape, so that these actions (Moving, Editing the text and resizing) can not be done.

Are there any ways to achieve this with OpenXML SDK?

If so which properties do I need to add to the Shape and so forth?

C# OpenXmlReader get cell value from excel instead of formula

$
0
0

I wrote this code so can read a couple thousand cells in about 2 seconds instead of having to wait about 3 minutes with a DOM approach.

static void Main(string[] args)
    {
        Cell theCell;
        string value;
        //List<string> values = new List<string>();
        //List<string> address = new List<string>();
        using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open("readme.xlsx", false))
        {
            WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            Sheet theSheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Steam").FirstOrDefault();
            WorksheetPart worksheetPart = (WorksheetPart)(workbookPart.GetPartById(theSheet.Id));
            OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
            var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();


            while (reader.Read())
            {
                if (reader.ElementType == typeof(Cell))
                {
                    theCell = (Cell)reader.LoadCurrentElement();
                    value = theCell.InnerText; //THIS LINE NEED TO BE CHANGED

                    //theCell.Descendants<CellValue>().FirstOrDefault().Text;
                    //The above line was how formulas were resolved in the old method, but it now throws null point exceptions

                    if (theCell.DataType != null)
                    {
                        switch (theCell.DataType.Value)
                        {
                            case CellValues.SharedString:
                                if (stringTable != null)
                                {
                                    value = stringTable.SharedStringTable.
                                      ElementAt(int.Parse(value)).InnerText;
                                }
                                break;

                            case CellValues.Boolean:
                                switch (value)
                                {
                                    case "0":
                                        value = "FALSE";
                                        break;
                                    default:
                                        value = "TRUE";
                                        break;
                                }
                                break;
                        }
                    }
                    Debug.WriteLine(theCell.CellReference);
                    Debug.WriteLine(value);
                }

Is there a way to modify this code so it reads cell values instead of formula without changing
if (reader.ElementType == typeof(Cell))

to

if (reader.ElementType == typeof(CellValue))

By keeping the ElementType in Cell, I can capture the cell reference which I can use to index values later on.


How can I send a date from C# to Excel using Open XML - and make the cell containing the date in Excel be a date cell instead of a general cell?

$
0
0

Hi

I'm using c# and openxml to create an Excel workbook. This works fine, however my dates are just string representations of dates when I open the sheet. I find that the cell datatype is set to general, however I really need it to be date so that I can use the filtering functions in Excel properly. (I need to filter by January, not the string "01.01" or "02.01"). 

I found a forum reply from 2009 stating that cells can either be strings or numbers, one can only format the string to look like a date. (https://social.msdn.microsoft.com/Forums/en-US/3143212a-c798-4a93-ab2b-f08625c5cbe5/how-to-get-cell-datatype-in-spreadsheet?forum=oxmlsdk)

Is this still the case, or is there any way I can turn my cell into a cell with a datatype of Date from my c# code? I tried changing the template, but as soon as I filled the cells with data, they switched back to "general". The emtpy cells in that column were still date-cells. 

Thanks

Writing data in chunks to excel using OpenXml and appending to the existing rows

$
0
0

 public void ExportDataSet(DataSet ds, string destination)
        {

            using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(destination, true))
            {
                WorkbookPart workbookPart = spreadSheet.WorkbookPart;
                IEnumerable<Sheet> sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
                string relationshipId = sheets.FirstOrDefault(s => string.Compare(s.Name, "Raw data", true) == 0).Id;
                WorksheetPart worksheetPart = (WorksheetPart)spreadSheet.WorkbookPart.GetPartById(relationshipId);
                SheetData sheetData = new SheetData();

                DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                List<String> columns = new List<string>();
                System.Data.DataTable dt = ds.Tables[0];
                foreach (DataColumn column in dt.Columns)
                {
                    columns.Add(column.ColumnName);
                    DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                    cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                    cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
                    headerRow.AppendChild(cell);
                }
                sheetData.AppendChild(headerRow);                
                foreach (DataRow dsrow in dt.Rows)
                {                  

                    DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                    foreach (String col in columns)
                    {
                        DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                        cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                        cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString());
                        newRow.AppendChild(cell);

                    }  
                     sheetData.AppendChild(newRow);           


                }


                worksheetPart.Worksheet.ReplaceChild<SheetData>(sheetData, worksheetPart.Worksheet.Elements<SheetData>().FirstOrDefault());         


            }
        }

Using the above code i can only see the last set of data rows..Is it possible to append rows to the older set.

                                                                                                   

copy both paragraph and table and paste at particular position in OpenXML

$
0
0

Hi,

I have word document and i need to copy both paragraph and table and paste at particular position.Below is format of one of the page in my document.I need to copy from paragraph 2 to paragraph 3(paragraph 2 ,table 1 and paragraph 3) and paste after paragraph 4.How to do this scenario in OpenXML.Please provide the solution.

Viewing all 1288 articles
Browse latest View live


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