I wrote a program than can extract usernames and add them to an excel sheet through xml; however, every time there's more than 21 users, the excel becomes corrupted. If I edit the .xls file through notepad, I can fix it or corrupt it by simply removing or adding more than 22 users.
The Excel files that always get corrupted if I insert more than 21 users through xml.
This is the code that adds users, but it doesn't matter since even doing it manually can replicate the corruption issue.
public static void XlFormat(string[] nameSplit, DataTable dt, string[] lines, String path)
{
lines = new string[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++) //When I change the condition to i < 22 or lower, it will create a perfect file. More than 22 and it gets corrupted
{
lines[i] = "<Row><Cell ss:StyleID=\"s59\"><Data ss:Type=\"String\">" + dt.Rows[i][1] + "</Data></Cell>" +
"<Cell ss:StyleID=\"s59\"><Data ss:Type=\"String\">" + dt.Rows[i][2] + "</Data></Cell>" +
"<Cell ss:StyleID=\"s59\"><Data ss:Type=\"String\">" + dt.Rows[i][3] + "</Data></Cell>" +
"<Cell ss:StyleID=\"s59\"><Data ss:Type=\"String\">" + dt.Rows[i][4] + "</Data></Cell>" +
"<Cell ss:StyleID=\"s64\"/><Cell ss:StyleID=\"s65\"/></Row>";
}
/* Insert the data into designated points in excel_format.txt using regular */
/* expressions, including the cells for each user. It should be all in one */
/* final string. */
string linesCombined = "";
for (int i = 0; i < lines.Count(); i++)
{
linesCombined += lines[i];
}
string xmlLines = Regex.Replace(excelFormat, "--- INSERT USERS HERE ---", linesCombined);
xmlLines = Regex.Replace(xmlLines, "--- INSERT LEADER HERE ---", nameSplit[2]);
xmlLines = Regex.Replace(xmlLines, "--- INSERT DATE HERE ---", DateTime.Now.Date.ToString());
xmlLines = Regex.Replace(xmlLines, "--- INSERT EMAIL HERE ---", nameSplit[1]);
xmlLines = Regex.Replace(xmlLines, "--- INSERT GROUP HERE ---", nameSplit[0]);
/* Write the final string to an XLS file. This file type will open in Excel as a */
/* spreadsheet even though it was written as an XML file. */
System.IO.File.Delete(@"" + path + nameSplit[0] + ".xls");
System.IO.File.WriteAllText(@"" + path + nameSplit[0] + ".xls", xmlLines);
}
When did I ever limit my XLS to 22 users?!