none
XML declaration is repeated at the document

    Question

  • Hi,
       I am using c# open XML to write to excel. However, in the document. The XML declaration is repeated. What is the cause of this error and how to fix it?

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <x:worksheet
    	xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    	xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" mc:Ignorable="x14ac"
    	xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    	<x:dimension ref="A1" />
    	<x:sheetViews>
    		<x:sheetView tabSelected="1" workbookViewId="0" /><?xml version="1.0" encoding="utf-8" standalone="yes"?><x:worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" mc:Ignorable="x14ac" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:dimension ref="A1" /><x:sheetViews><x:sheetView tabSelected="1" workbookViewId="0" />


    hon123456

    mardi 8 mai 2018 02:47

Toutes les réponses

  • Hi hon,

    I assume this issue is related with your code.

    Could you share us your code which is used to write to Excel file?

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    mercredi 9 mai 2018 06:20
    Modérateur
  • Thanks Tao. I think it will be related to the code. I will check it and prepare it to post the code.


    hon123456

    mercredi 9 mai 2018 09:20
  • If you have any update about your issue, please feel free to let us know.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    jeudi 10 mai 2018 06:23
    Modérateur
  • Hi,

        Here is the code used to create the document.

      
    using (SpreadsheetDocument document = SpreadsheetDocument.Open(excelFilePath, true))
                    {
                        WorkbookPart wbPart = document.WorkbookPart;
                        WorksheetPart wsPart = wbPart.WorksheetParts.First();
    
                        
                        Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
                            Where(s => s.Name == "Data").FirstOrDefault();
                        if (theSheet != null)
                        {
                            
                            WorkbookStylesPart stylesPart = document.WorkbookPart.WorkbookStylesPart;
                            
                            Font font1 = new Font(
                                    new Bold()
                                
                                    , new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }
                                
                                    );
                            
                            Font font2 = new Font(
                                    new Underline()
                                
                                    , new Color() { Rgb = new HexBinaryValue() { Value = "0000FF" } }
                                
                                    );
                            
                            Font font3 = new Font(
                                    new Color() { Rgb = new HexBinaryValue() { Value = "AAAAAA" } }
                                    );
                            stylesPart.Stylesheet.Fonts.Append(font1);
                            stylesPart.Stylesheet.Fonts.Append(font2);
                            stylesPart.Stylesheet.Fonts.Append(font3);
                            stylesPart.Stylesheet.Save();
                            
                            NumberingFormat decimalFormat = new NumberingFormat();
                            decimalFormat.NumberFormatId = UInt32Value.FromUInt32(3453);
                            decimalFormat.FormatCode = StringValue.FromString("#,##0.0000");
                            stylesPart.Stylesheet.NumberingFormats = new NumberingFormats();
                            stylesPart.Stylesheet.NumberingFormats.Append(decimalFormat);
                            
                            UInt32Value fontId1 = 1;
                            UInt32Value fontId2 = 2;
                            UInt32Value fontId3 = 3;
                            CellFormat cf1 = new CellFormat() { FontId = fontId1, FillId = 0, BorderId = 0, ApplyFont = true };
                            CellFormat cf2 = new CellFormat() { FontId = fontId2, FillId = 0, BorderId = 0, ApplyFont = true };
                            CellFormat cf3 = new CellFormat() { FontId = 0, FillId = 0, BorderId = 0, ApplyFont = true, NumberFormatId = decimalFormat.NumberFormatId, ApplyNumberFormat = BooleanValue.FromBoolean(true) };
                            CellFormat cf4 = new CellFormat() { FontId = fontId3, FillId = 0, BorderId = 0, ApplyFont = true };
                            stylesPart.Stylesheet.CellFormats.Append(cf1);
                            stylesPart.Stylesheet.CellFormats.Append(cf2);
                            stylesPart.Stylesheet.CellFormats.Append(cf3);
                            stylesPart.Stylesheet.CellFormats.Append(cf4);
                            uint styleDefault = 0;
                            uint styleHeader = 1;
                            uint styleHyperlink = 2;
                            uint styleDecimal = 3;
                            uint styleGray = 4;
    
                            Hyperlinks hyperlinks1 = new Hyperlinks();
                            
                            
                            SharedStringTablePart shareStringPart;
                            if (document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
                            {
                                shareStringPart = document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
                            }
                            else
                            {
                                shareStringPart = document.WorkbookPart.AddNewPart<SharedStringTablePart>();
                                shareStringPart.SharedStringTable = new SharedStringTable();
                            }
                            int linkIndex = 0;
    
                            
                            List<int> locationIDs = new List<int>(2048); // rows.Length);
                            List<int> indicatorIDs = new List<int>(2048); //rows.Length);
                            foreach (string locID in locIDsArr)
                            {
                                locationIDs.Add(Convert.ToInt32(locID));
                            }
                            foreach (string indID in indIDsArr)
                            {
                                indicatorIDs.Add(Convert.ToInt32(indID));
                            }
    
                            
                            Dictionary<int, Row> rowList = new Dictionary<int, Row>(10240);
                            Dictionary<string, Cell> cellList = new Dictionary<string, Cell>(10240);
    
                            
                            Row row1 = new Row();
                            Row row2 = new Row();
                            rowList.Add(1, row1);
                            rowList.Add(2, row2);
                            int columnIndex = 3; // 3 = C
                            int rowIndex = 1;
                            Worksheet ws = ((WorksheetPart)(wbPart.GetPartById(theSheet.Id))).Worksheet;
                            SheetData sheetData = ws.GetFirstChild<SheetData>();
                            
                            sheetData.Append(row1);
                            
                            sheetData.Append(row2);
                            AddBlankCell(wbPart, ws, theSheet, "A1", row1);
                            AddBlankCell(wbPart, ws, theSheet, "B1", row1);
                            AddBlankCell(wbPart, ws, theSheet, "A2", row2);
                            AddBlankCell(wbPart, ws, theSheet, "B2", row2);
                            AddBlankCell(wbPart, ws, theSheet, "C2", row2);
                            for (int i = 0; i < locationIDs.Count; i++)
                            {
                                int locationID = locationIDs[i];
                                string locationName = null;
                                {
                                    DataRow[] drs = locations.Tables[0].Select(string.Format("id = {0}", locationID));
                                    if (drs.Length > 0)
                                    {
                                        locationName = Convert.ToString(drs[0][Common.Systems.Sustainability.Constants.Location.LocationName]);
                                    }
                                }
    
                                if (locationName != null)
                                {
                                    // Add 'location' cell
                                    // create cell and write data
                                    string columnText = Common.ExcelUtil.GetColumnText(columnIndex);
                                    Cell cell = new Cell() { CellReference = string.Format("{0}{1}", columnText, rowIndex) };
                                    cell.DataType = new EnumValue<CellValues>(CellValues.String);
                                    cell.CellValue = new CellValue();
                                    cell.CellValue.Text = locationName;
    
                                    // add cell to row
                                    row1.Append(cell);
    
                                    // apply the header style
                                    cell.StyleIndex = styleHeader;
    
                                    // add cell to cell list
                                    cellList.Add(cell.CellReference, cell);
    
    
                                    // Add 'Attachment' cell
                                    // create cell and write data
                                    string columnText2 = Common.ExcelUtil.GetColumnText(columnIndex + 1);
                                    Cell cell2 = new Cell() { CellReference = string.Format("{0}{1}", columnText2, rowIndex + 1) };
                                    cell2.DataType = new EnumValue<CellValues>(CellValues.String);
                                    cell2.CellValue = new CellValue();
                                    cell2.CellValue.Text = ResourcesUtil.GetStringByName("Attachment", lang);
    
                                    // add cell to row
                                    row2.Append(cell2);
    
                                    // apply the header style
                                    cell2.StyleIndex = styleHeader;
    
                                    // add cell to cell list
                                    cellList.Add(cell2.CellReference, cell2);
    
    
                                    // Add 'Remarks' cell
                                    // create cell and write data
                                    string columnText3 = Common.ExcelUtil.GetColumnText(columnIndex + 2);
                                    Cell cell3 = new Cell() { CellReference = string.Format("{0}{1}", columnText3, rowIndex + 1) };
                                    cell3.DataType = new EnumValue<CellValues>(CellValues.String);
                                    cell3.CellValue = new CellValue();
                                    cell3.CellValue.Text = ResourcesUtil.GetStringByName("Remarks", lang);
    
                                    // add cell to row
                                    row2.Append(cell3);
    
                                    // apply the header style
                                    cell3.StyleIndex = styleHeader;
    
                                    // add cell to cell list
                                    cellList.Add(cell3.CellReference, cell3);
    
    
                                    columnIndex += 3;
                                }
                            }
    
    


    hon123456

    jeudi 10 mai 2018 09:36
  • Hi,

    There are multiple parts missing in the sharing code.

    Could you share us a simple project through OneDrive? Then we could try to reproduce your issue?

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    vendredi 11 mai 2018 06:38
    Modérateur
  • Hi Hon,

    Have you fixed this issue yet? If not, please feel free to let us know the detail step you blocked.

    Regards & Fei


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    jeudi 31 mai 2018 08:45
    Modérateur