Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

sharedStrings.xml error when original file contain escape character #491

Open
mcdull741 opened this issue Jun 26, 2019 · 1 comment
Open

Comments

@mcdull741
Copy link

Hi JanKallMan,

I am trying to read and update an xlsx file which contain escape character "&". But sharedStrings.xml is corrupted after saving(as) the file. I also tried to write the data into a new file and it worked.
Error.XLSX
Pass.XLSX
Test.xlsx
Here is the code:

public void TestEscapgeCharater()
{
	string srcPath = "Test.XLSX";
	string errPath = "Error.XLSX";
	string passPath = "Pass.XLSX";
	using (ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(srcPath)))
	{
		string addr = package.Workbook.Worksheets[1].Dimension.Address;
		object[,] objArr = (object[,])package.Workbook.Worksheets[1].Cells[addr].Value;

                //update some cells here

		//Error 
		package.Workbook.Worksheets[1].Cells[addr].Value = objArr;
		package.SaveAs(new System.IO.FileInfo(errPath));

		//Pass
		using (ExcelPackage new_package = new ExcelPackage(new System.IO.FileInfo(passPath)))
		{
			new_package.Workbook.Worksheets.Add(package.Workbook.Worksheets[1].Name);
			new_package.Workbook.Worksheets[1].Cells[addr].Value = objArr;
			new_package.Save();
		}
	}
}
@mcdull741
Copy link
Author

I found the issue. When the sharedString is richtext, this lib does not change inner text to xml format.
Here is the source code from SaveSharedStringHandler in ExcelWorkbook.cs

if (ssi.isRichText)
{
	cache.Append("<si>");
	ConvertUtil.ExcelEncodeString(cache, t);//not change the string to xml string
	cache.Append("</si>");
}
else
{
	if (t.Length > 0 && (t[0] == ' ' || t[t.Length - 1] == ' ' || t.Contains("  ") || t.Contains("\t") || t.Contains("\n") || t.Contains("\n")))   //Fixes issue 14849
	{
		cache.Append("<si><t xml:space=\"preserve\">");
	}
	else
	{
		cache.Append("<si><t>");
	}
	ConvertUtil.ExcelEncodeString(cache, ConvertUtil.ExcelEscapeString(t));
	cache.Append("</t></si>");
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant