I am receiving an uploaded Excel file on my server and I want to parse the
file. The data with in the table is processed in the correct order, but for some strange reason my header row gets alphabetized. If I have the first row's columns as such First Col
, A Second Col
, Third Col
, and A Fourth Col
, the code returns an array of [A Fourth Col
, A Second Col
, First Col
, Third Col
], i.e. alphabetized with strings starting with 'A's first, then 'F', then 'S', etc.
My C# code goes like this for reading the header row:
private JsonResult ProcessExcelFile(String filename, String connectionString)
{
DataTable ExcelFile = null;
List<Dictionary<string,dynamic>> dataCells = new List<Dictionary<string,dynamic>>();
var dataCount = 0;
dataCells.Add(new Dictionary<string, dynamic>());
dataCells.Add("fileName", "Excel," + fileName);
using (var conn = new OleDbConnection(connectionString))
{
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
DataTable tableSheets = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
List<string> sheetList = new List<string>();
foreach (DataRow tableSheet in tableSheets.Rows)
{
string currentSheetName = tableSheet[2].ToString().Replace("'", "");
sheetList.Add(currentSheetName);
}
DataSet data = new DataSet();
foreach(var sheetName in sheetList)
{
var dataTable = new DataTable();
string query = string.Format("SELECT * FROM [{0}]", sheetName);
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
adapter.Fill(dataTable);
data.Tables.Add(dataTable);
}
ExcelFile = new DataTable("ExcelFile");
List<string> columnNames = new List<string>();
DataTable dataColumns = conn.GetSchema("Columns");
foreach (DataRow dataColumn in dataColumns.Rows)
{
string currentSheet = dataColumn.ItemArray[2].ToString();
if(currentSheet == "Sheet1!")
{
columnsNames.Add(dataColumn["COLUMN_NAME"].ToString());
DataColumn tableHeader = new DataColumn(dataColumn["COLUMN_NAME"].ToString(), Type.GetType("System.String"));
ExcelFile.Columns.Add(tableHeader);
}
}
var cmd = conn.CreateCommand();
cmd.CommandText = String.Format(
@"SELECT * FROM [{0}$]",
"Sheet1"
);
using (var rdr = cmd.ExecuteReader())
{
DataRow NewRow;
if (rdr.HasRows)
{
while (rdr.Read())
{
dataCount++;
NewRow = ExcelFile.NewRow();
dataCells.Add(new Dictionary<string, dynamic>());
for (int index = 0; index < rdr.FieldCount; index++)
{
string fieldName = columnsList[index];
var fieldType = rdr.GetFieldType(index).FullName;
switch(fieldType)
{
case ("System.Byte"):
byte byteFieldValue = rdr.GetByte(index);
dataCells[dataCount].Add(fieldname, byteFieldValue);
NewRow[fieldname] = byteFieldValue;
break;
case ("System.Int16"):
Int16 int16FieldValue = rdr.GetInt16(index);
dataCells[dataCount].Add(fieldname, int16FieldValue);
NewRow[fieldname] = int16FieldValue;
break;
case ("System.Int32"):
Int32 int32FieldValue = rdr.GetInt32(index);
dataCells[dataCount].Add(fieldname, int32FieldValue);
NewRow[fieldname] = int32FieldValue;
break;
case ("System.Int64"):
Int64 int64FieldValue = rdr.GetInt64(index);
dataCells[dataCount].Add(fieldname, int64FieldValue);
NewRow[fieldname] = int64FieldValue;
break;
case ("System.Double"):
double doubleFieldValue = rdr.GetDouble(index);
dataCells[dataCount].Add(fieldname, doubleFieldValue);
NewRow[fieldname] = doubleFieldValue;
break;
case ("System.Boolean"):
bool boolFieldValue = rdr.GetBoolean(index);
dataCells[dataCount].Add(fieldname, boolFieldValue);
NewRow[fieldname] = boolFieldValue;
break;
case ("System.Char"):
char charFieldValue = rdr.GetChar(index);
dataCells[dataCount].Add(fieldname, charFieldValue);
NewRow[fieldname] = charFieldValue;
break;
case ("System.Decimal"):
decimal decimalFieldValue = rdr.GetDecimal(index);
dataCells[dataCount].Add(fieldname, decimalFieldValue);
NewRow[fieldname] = decimalFieldValue;
break;
case ("System.DateTime"):
DateTime datetimeFieldValue = rdr.GetDateTime(index);
dataCells[dataCount].Add(fieldname, datetimeFieldValue);
NewRow[fieldname] = datetimeFieldValue;
break;
case ("System.String"):
string stringFieldValue = "";
stringFieldValue = rdr.GetDouble(index);
dataCells[dataCount].Add(fieldname, stringFieldValue);
NewRow[fieldname] = stringFieldValue;
break;
default:
string defaultFieldValue = "type not found";
dataCells[dataCount].Add(fieldname, defaultFieldValue);
NewRow[fieldname] = defaultFieldValue;
break;
}
}
}
}
}
rdr.Close();
}
conn.Close();
}
JsonResult jsonObject = Json(dataCells);
return jsonObject
}
Anyone know why this is doing this?