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

Create an Excel Spreadsheet with an image on the First Header with Open Xml

$
0
0

Hello,

I have been trying to make a supposedly simple task of adding an image to the left of the First Header in an Excel Document, but i can't seem to accomplish it.

I have seen a few examples of inserting images but all of them are to insert an image to cells, i also tried to use Code Reflector from the OpenXML SDK Tools, but no clue in making it work. I have been able to insert some text on the header but i have no clue on how to do the same with an image. What i'm looking to acomplish is to have an image on the left side of the First Header and a Title on the right side of the header.

Can someone please shed some light on this with an actual working example?

This is the code i have working to create the excel Document with data from a DataTable

private MemoryStream CreateSummaryExcelDoc(DataTable dataTable)
        {
            MemoryStream s = new MemoryStream();

            //Create excel document
            using (SpreadsheetDocument excelDocument =
                SpreadsheetDocument.Create(s, SpreadsheetDocumentType.Workbook, true))
            {

                var workbookPart = excelDocument.AddWorkbookPart();
                excelDocument.WorkbookPart.Workbook = new Workbook();
                excelDocument.WorkbookPart.Workbook.Sheets = new Sheets();

                var sheetPart = excelDocument.WorkbookPart.AddNewPart<WorksheetPart>();

                var sheetData = new SheetData();
                sheetPart.Worksheet = new Worksheet(sheetData);
                Worksheet ws = sheetPart.Worksheet;

                // Header Text to Insert
                var textToInsert = "&R&B&18My Header Text"; // &R-Right Section f the Header &B- Bold &18-Font Size
                // Insert text to FirstHeader
                InsertHeaderFooter(ws, HeaderType.FirstHeader, textToInsert);

                Sheets sheets = excelDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>();

                string relationshipId = excelDocument.WorkbookPart.GetIdOfPart(sheetPart);
                // Create My Sheet
                Sheet sheet = new Sheet() { Id = relationshipId, SheetId = 1, Name = "My Sheet" };
                sheets.Append(sheet);

                // Table Header Row
                Row headerRow = new Row();
                // Insert the columns 
                List<String> columns = new List<string>();
                foreach (DataColumn column in dataTable.Columns)
                {
                    columns.Add(column.ColumnName);

                    Cell cell = new Cell();
                    cell.DataType = CellValues.String; 
                    cell.CellValue = new CellValue(column.ColumnName);
                    headerRow.AppendChild(cell);
                }

                sheetData.AppendChild(headerRow);

                // Insert data
                foreach (DataRow dsrow in dataTable.Rows)
                {
                    Row newRow = new Row();
                    foreach (String col in columns)
                    {
                        Cell cell = new Cell();
                        cell.DataType = CellValues.String; 
                        cell.CellValue = new CellValue(dsrow[col].ToString()); 
                        newRow.AppendChild(cell);
                    }

                    sheetData.AppendChild(newRow);
                }
            }

            return s;
        }

And this is function to add text to an Header.

private static void InsertHeaderFooter(Worksheet ws, HeaderType type, String textToInsert)
        {           
            HeaderFooter hf = ws.Descendants<HeaderFooter>().FirstOrDefault();
            if (hf == null)
            {
                hf = new HeaderFooter();
                ws.AppendChild<HeaderFooter>(hf);
            }

            // The HeaderFooter node should be there, at this point!
            if (hf != null)
            {
                // You've found the node. Now add the header or footer.
                // Deal with the attributes first:
                switch (type)
                {
                    case HeaderType.EvenHeader:
                    case HeaderType.EvenFooter:
                    case HeaderType.OddHeader:
                    case HeaderType.OddFooter:
                        // Even or odd only? Add a differentOddEven attribute and set 
                        // it to "1".
                        hf.DifferentOddEven = true;
                        break;

                    case HeaderType.FirstFooter:
                    case HeaderType.FirstHeader:
                        hf.DifferentFirst = true;
                        break;
                }

                switch (type)
                {
                    // This code creates new header elements, even if they
                    // already exist. Either way, you end up with a 
                    // "fresh" element.
                    case HeaderType.AllHeader:
                        hf.EvenHeader = new EvenHeader();
                        hf.EvenHeader.Text = textToInsert;

                        hf.OddHeader = new OddHeader();
                        hf.OddHeader.Text = textToInsert;
                        break;

                    case HeaderType.AllFooter:
                        hf.EvenFooter = new EvenFooter();
                        hf.EvenFooter.Text = textToInsert;

                        hf.OddFooter = new OddFooter();
                        hf.OddFooter.Text = textToInsert;
                        break;

                    case HeaderType.EvenFooter:
                        hf.EvenFooter = new EvenFooter();
                        hf.EvenFooter.Text = textToInsert;
                        break;

                    case HeaderType.EvenHeader:
                        hf.EvenHeader = new EvenHeader();
                        hf.EvenHeader.Text = textToInsert;
                        break;

                    case HeaderType.OddFooter:
                        hf.OddFooter = new OddFooter();
                        hf.OddFooter.Text = textToInsert;
                        break;

                    case HeaderType.OddHeader:
                        hf.OddHeader = new OddHeader();
                        hf.OddHeader.Text = textToInsert;
                        break;

                    case HeaderType.FirstHeader:
                        hf.FirstHeader = new FirstHeader();
                        hf.FirstHeader.Text = textToInsert;
                        break;

                    case HeaderType.FirstFooter:
                        hf.FirstFooter = new FirstFooter();
                        hf.FirstFooter.Text = textToInsert;
                        break;
                }
            }
            ws.Save();
        }

I appreciate any kind of help in this matter. Thanks!


Viewing all articles
Browse latest Browse all 1288

Trending Articles



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