Very old post... but same problem in 2026 ! The "x:" namespace is still added and can cause problem on some other software that import XLSX.
Here's our hack :
When creating the speadsheet, just before disposing, we call 2 customs functions :
```
SpreadsheetDocument spreadsheetDoc = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(this._spreadsheetDocumentStream, true)
// your code that generate the XLSX
// Fixing problem :
FixAllWorksheetDimensions(spreadsheetDoc); //only needed if software report wrong number of lines
RemovePrefixesEverywhere(spreadsheetDoc); //remove bad namespace
spreadsheetDoc.Dispose();
```
And here's the customs functions :
```
private void RemovePrefixesEverywhere(SpreadsheetDocument doc)
{
foreach (var part in doc.GetAllParts())
{
try
{
string xml;
using (var reader = new StreamReader(part.GetStream(FileMode.Open, FileAccess.Read)))
{
xml = reader.ReadToEnd();
}
if (xml.StartsWith("<?xml"))
{
// remove all "x:" prefixes and xmlns:x declaration
xml = xml.Replace("<x:", "<").Replace("</x:", "</").Replace("xmlns:x=", "xmlns=");
using (var writer = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)))
{
writer.Write(xml);
}
}
} catch { /* ignore parts that are not XML (e.g. images) */ }
}
}
private void FixAllWorksheetDimensions(SpreadsheetDocument doc)
{
foreach (var wsPart in doc.WorkbookPart.WorksheetParts)
{
var sheetData = wsPart.Worksheet.GetFirstChild<SheetData>();
if (sheetData == null || !sheetData.Elements<Row>().Any())
continue;
uint minRow = sheetData.Elements<Row>().Min(r => r.RowIndex.Value);
uint maxRow = sheetData.Elements<Row>().Max(r => r.RowIndex.Value);
// max length = number of columns of the longest row
int maxColIndex = sheetData.Elements<Row>()
.Max(r => r.Elements<Cell>().Count());
string maxCol = GetExcelColumnReference(maxColIndex);
string dimensionRef = $"A{minRow}:{maxCol}{maxRow}";
var dim = wsPart.Worksheet.GetFirstChild<SheetDimension>();
if (dim == null)
{
dim = new SheetDimension();
wsPart.Worksheet.InsertAt(dim, 0);
}
dim.Reference = new DocumentFormat.OpenXml.StringValue(dimensionRef);
wsPart.Worksheet.Save();
}
}
```
Enjoy it :-)
Very old post... but same problem in 2026 ! The "x:" namespace is still added and can cause problem on some other software that import XLSX.
Here's our hack :
When creating the speadsheet, just before disposing, we call 2 customs functions :
```
SpreadsheetDocument spreadsheetDoc = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(this._spreadsheetDocumentStream, true)
// your code that generate the XLSX
// Fixing problem :
FixAllWorksheetDimensions(spreadsheetDoc); //only needed if software report wrong number of lines
RemovePrefixesEverywhere(spreadsheetDoc); //remove bad namespace
spreadsheetDoc.Dispose();
```
And here's the customs functions :
```
private void RemovePrefixesEverywhere(SpreadsheetDocument doc)
{
foreach (var part in doc.GetAllParts())
{
try
{
string xml;
using (var reader = new StreamReader(part.GetStream(FileMode.Open, FileAccess.Read)))
{
xml = reader.ReadToEnd();
}
if (xml.StartsWith("<?xml"))
{
// remove all "x:" prefixes and xmlns:x declaration
xml = xml.Replace("<x:", "<").Replace("</x:", "</").Replace("xmlns:x=", "xmlns=");
using (var writer = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)))
{
writer.Write(xml);
}
}
} catch { /* ignore parts that are not XML (e.g. images) */ }
}
}
private void FixAllWorksheetDimensions(SpreadsheetDocument doc)
{
foreach (var wsPart in doc.WorkbookPart.WorksheetParts)
{
var sheetData = wsPart.Worksheet.GetFirstChild<SheetData>();
if (sheetData == null || !sheetData.Elements<Row>().Any())
continue;
uint minRow = sheetData.Elements<Row>().Min(r => r.RowIndex.Value);
uint maxRow = sheetData.Elements<Row>().Max(r => r.RowIndex.Value);
// max length = number of columns of the longest row
int maxColIndex = sheetData.Elements<Row>()
.Max(r => r.Elements<Cell>().Count());
string maxCol = GetExcelColumnReference(maxColIndex);
string dimensionRef = $"A{minRow}:{maxCol}{maxRow}";
var dim = wsPart.Worksheet.GetFirstChild<SheetDimension>();
if (dim == null)
{
dim = new SheetDimension();
wsPart.Worksheet.InsertAt(dim, 0);
}
dim.Reference = new DocumentFormat.OpenXml.StringValue(dimensionRef);
wsPart.Worksheet.Save();
}
}
```
Enjoy it :-)