use Open XML SDK with files Open XML protected password for read
ASP.Net Open office XML Create dropdown list in excel without using Interop
I am using ASP.Net and Open office XML and I have been able to set the headers of the excel file.
However, I want to set the value to cells from say D2 to D1000 in a drop down fashion i.e. that the user can only select from
a predefined list of values as in a drop down list. How do I accomplish this using openoffice excel?
I have already done this using Office Interop.
The code for creating the excel is
List<ExcelExport> mpList =newList<ExcelExport>();DataTable dt =ListToDataTable(mpList);string attachment =string.Format("attachment;filename={0}-{1}.xlsx", ddlHealthFacility.SelectedItem.Text+" Excel export "," ");using(ExcelPackage pck =newExcelPackage()){//Create the worksheetExcelWorksheet ws = pck.Workbook.Worksheets.Add("Monthly Plan"); ws.Cells["A1"].LoadFromDataTable(dt,true);Byte[] fileBytes = pck.GetAsByteArray();Response.Clear();Response.Buffer=true;Response.AddHeader("content-disposition", attachment);Response.Charset="";Response.ContentType="application/vnd.ms-excel";StringWriter sw =newStringWriter();Response.BinaryWrite(fileBytes);Response.End();}
Delete legend entries from an Excel chart
Aiming to remove legend entries from an Excel chart from code I tried:
new LegendEntry( new Index() { Val = (UInt32Value)i }, new Delete { Val = true }));where
i
is the index of the entry.
What is strange is that if I open the Excel generated from code, and then manually remove an entry, I see the same code being generated:
C.LegendEntry legendEntry1 = new C.LegendEntry(); C.Index index4 = new C.Index(){ Val = (UInt32Value)1U }; C.Delete delete3 = new C.Delete(){ Val = true }; legendEntry1.Append(index4); legendEntry1.Append(delete3);
But when the Excel is generated, even though the code is there the legend entry is not deleted.
Is there a way to remove legend entries from an Excel chart from code?
Copy body of a word document into another document including contained pictures
Hello,
I'd like to copy (working with the WordprocessingDocument) the body of a word document into another document, including the contained pictures.
The copy process works fine, but the image in the new document could not be found.
Has anybody an idea how I can copy the whole body including the contained pictures?
Best regards
OpenXML - How to detect the active worksheets (current worksheets that user is editing) when read an xlsx file ?
I have a function for user to upload xlsx. Then, the program reads and converts excel data to a datatable.
If there are more than one worksheets in the file, I want the program reads the active one only.
So, how can I detect which one is active? Thanks.
Open XML Unordered Lists Bullet Alignment
Is there a way to control the left margin when an unordered list is converted? So basically what is happening the bullet is out of the margin. It should be moved over and line up with the header as it does if you see it in the browser as html. I tried setting the style margin, but that did not work. Has anyone run into this issue before? Thanks.
Insert a Picture into the Picture/Image Content Control using Open XML SDK 2.0
I have created a word template as follows
Here PizzaPicture is a Picture Content Control, PizzaName is a Plain Text Content Control and PizzaDescription is a Rich Text Content Control
I am able to assign text to PizzaName and PizzaDescription Easily but not able to even locate the PizzaPicture. Here is my code
using (WordprocessingDocument document = WordprocessingDocument.Open(@".\test.docx", true)) { MainDocumentPart mainPart = document.MainDocumentPart; DocumentFormat.OpenXml.Wordprocessing.Text text = null; SdtContentBlock pizzaNameBlock = null; SdtContentBlock pizzaDescriptionBlock = null;//SdtContentBlock pizzaPictureBlock = null; List<SdtBlock> sdtList = mainPart.Document.Descendants<SdtBlock>().ToList();foreach (SdtBlock sdt in sdtList) { Console.WriteLine(sdt.SdtProperties.GetFirstChild<Tag>().Val.Value); } SdtBlock s1 = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "PizzaName").Single(); SdtBlock s2 = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "PizzaDescription").Single(); SdtBlock s3 = mainPart.Document.Body.Descendants<SdtBlock>().FirstOrDefault(r => { SdtProperties p = r.Elements<SdtProperties>().FirstOrDefault();if (p != null) { Console.WriteLine("P is not null");// Is it a picture content control? SdtContentPicture pict = p.Elements<SdtContentPicture>().FirstOrDefault();if (pict != null) Console.WriteLine("Pict is not null");// Get the alias. SdtAlias a = p.Elements<SdtAlias>().FirstOrDefault();if (pict != null&& a.Val == "PizzaPicture")returntrue; }returnfalse; });if (s3 == null) { Console.Write(" s3 is Null"); } else { Console.WriteLine("s3 not null"); }if (s1 != null) { pizzaNameBlock = s1.Descendants<SdtContentBlock>().FirstOrDefault(); text = pizzaNameBlock.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault();// here you can set the current time text.Text = "Peperoni"; Console.WriteLine(text.Text); }string embed = null;if (s3 != null) { Drawing dr = s3.Descendants<Drawing>().FirstOrDefault();if (dr != null) { Blip blip = dr.Descendants<Blip>().FirstOrDefault();if (blip != null) embed = blip.Embed; } }if (embed != null) { IdPartPair idpp = document.MainDocumentPart.Parts .Where(pa => pa.RelationshipId == embed).FirstOrDefault();if (idpp != null) { ImagePart ip = (ImagePart)idpp.OpenXmlPart;using (FileStream fileStream = File.Open(@"c:\temp\pepperoni.jpg", FileMode.Open)) ip.FeedData(fileStream); } }if (s2 != null) { pizzaDescriptionBlock = s2.Descendants<SdtContentBlock>().FirstOrDefault();string altChunkId = "AltChunkId12345"; AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml, altChunkId); chunk.FeedData(File.Open(@".\html.txt", FileMode.Open)); AltChunk altChunk = new AltChunk(); altChunk.Id = altChunkId;////Replace content control with altChunk information OpenXmlElement parent = pizzaDescriptionBlock.Parent; parent.InsertAfter(altChunk, pizzaDescriptionBlock); pizzaDescriptionBlock.Remove(); } mainPart.Document.Save(); document.Close(); }
Here the call mainPart.Document.Descendants<SdtBlock>().ToList(); returns me only 2 items PizzaName, PizzaDescription butno PizzaPicture.
Secondly the line SdtContentPicture pict = p.Elements<SdtContentPicture>().FirstOrDefault();returns NULL. So it seems that there is no element of type SdtContentPicture.
What am I doing wrong? I am pretty sure that I have put PizzaPicture as a Picture content control.
My objective is to locate this control in the document and then insert an image into it programmatically.
MSDNStudent Knows not much!
creating answer file
I need to learn how to write answer file from scratch to install specific software such as word viewer .. any help ??
PowerShell - how sort XML file related with attributes
How to Get Multiple Tables per Worksheet
I would think this setup would work:
<Worksheet ss:Name="Sheet 1">
<Table x:FullColumns="1" x:FullRows="1">
....[[CONTENT Table 1]]
</Table>
<Table x:FullColumns="1" x:FullRows="1">
....[[CONTENT Table 2]]
</Table>
</Worksheet>
Each table has its own dataset, Is there some setting or attribute that I need to consider in order to get more than one table per sheet to appear?
Thanks for any help!
P.T.
Creating Excel spreadsheet using System.IO.Packaging
Hi, I am creating Excel spreadsheets dynamically on a Web server using System.IO.Packaging. It has been working great for some time. I have noticed that the Excel files were sometimes very large for the amount of content and I noticed that when I opened them up and saved them using Excel that the size was reduced dramatically. After looking at the individual files in the package I saw that the files in the ones created by my program had a compression ratio of "0 %."
How can I add the package parts compressed? I am using Visual Studio 2010, Framework 4.0.
Thanks,
Lloyd Carter
OpenXML - could not load type documentformat.openxml.spreadsheet.sheet
Hello!
I have this error: could not load type documentformat.openxml.spreadsheet.sheet (i'm using nu-get packet OpenXML)
On my local machine all works fine, then i posted my App to a server, and it worked there fine too. But now i have this error.
I didn't anything with my app, configs etc.
Please help me to find out this error. Thanx
Remove Special Characters
Hi,
I am having a word document and i need to search text and replace the text with some value, but the text is [Today], so how to replace special characters with empty string in open xml using C#.Net
Ravindranath
Remove spell check from template
Hello,
I am working on code that takes .pptx template and replace text/images inside it depending on keywords.
So I am using keyword like "%number%" and check it with code like:
static void ReplacePlaceholderText(PresentationDocument document, MyData mydata) { var elements = document.PresentationPart.Presentation.SlideIdList.ChildElements; var slideParts = elements.Select(element => (SlidePart)document.PresentationPart.GetPartById((element as SlideId).RelationshipId)); var texts = slideParts.SelectMany(slidePart => slidePart.Slide.Descendants<Drawing.Text>()); foreach (var text in texts.Where(txt => !string.IsNullOrEmpty(txt.Text))) text.Text = text.Text.Replace("%number%", mydata.Number ?? ""); }
Thing is %number% is split inside XML into "%" "number" and "%" with "number" attributed with "err = 1", e.g.:
<p:txBody><a:bodyPr wrap="square" rtlCol="0"><a:spAutoFit/></a:bodyPr><a:lstStyle/><a:p><a:r><a:rPr lang="en-US" dirty="0" smtClean="0"/><a:t>%</a:t></a:r><a:r><a:rPr lang="en-US" dirty="0" err="1" smtClean="0"/><a:t>number</a:t></a:r><a:r><a:rPr lang="en-US" dirty="0" smtClean="0"/><a:t>%</a:t></a:r><a:endParaRPr lang="en-US" dirty="0"/></a:p></p:txBody>
Is there a way to disable error checking and therefore splitting text into paragraphs based on this with "err=1" attributes?
Or am I using wrong approach overall and there is another way to perform such task?
Problem with mailmerge field in header/footer
Hi,
I have code in one of my projects (C#) that do an automated mailmerge via OpenXML on word.dotx. The datasource is in the database and the C# code does the magic of changing the text value of the MailMerge field.
Everything works nicely, even when i open the document in word. But if I do an printout of the stored document, the mailmerge fields in the header/footer section is reset to their original value.
I have tried to print with word.Application.printout and using StartInfo.Verb = "PrintTo", but with the same result. All mailmerge fields in the main document is fine, but header/footer is reset.
Can anyone help me out here?
/Svein Thomas
Can i use Open XML SDK for Saving Mail Attachments from Exchange Server?
Hi,
I want to save mail attachments from Exchange Server to my local drive. Can i use Open XML SDK for this?
Thanks,
Mohan.
Mohan Raju
How change row number position in the sheet.
Hi all!
I want change my excel file and remove from them all existing empty rows before my data.
Example: I have before
I want to have after:
I don't know how I can fast modify my files in Open XML 2.5.
But I found solution for Microsoft.Office.Interop.Excel:
//Create a new Workbook. //Open an existing excel file. Workbook wb = new Workbook(@"d:\test\book1.xls"); //Create a Worksheets object with reference to //the sheets of the Workbook. WorksheetCollection sheets = wb.Worksheets; //Get first Worksheet from WorksheetCollection Worksheet sheet = sheets[0]; //Delete the Blank Rows from the worksheet sheet.Cells.DeleteBlankRows();
But I must't use excel app on server side...
Help me please)
How to read Multilevel Numbering format in Word 2007 document?
Hi,
How to read Multilevel Numbering format using OpenXML SDK and also is there any relation between the parent and child sequence like 4-->4.1-->4.1.1 and 4.1.2
Selvam S
Open XML | How to get values from merged cells
Hi
I have spread sheet as below:
I need to get values from C3:D3, C3:D4...C6:D6
I wrote below code
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(fileName, false)) { WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart; IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>(); string relationshipId = sheets.FirstOrDefault(s => string.Compare(s.Name, "Names", true) == 0).Id; WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId); List<MergeCells> mergedCells; if (worksheetPart.Worksheet.Elements<MergeCells>().Count() > 0) { //How to access values from merged cells } }
Can anybody please help me in understanding to get values from merged cells?
Regards,
RK
Radhakrishna