Reputation: 33
I wanna get the name and count of the columns of the user's excel files, but i can't send the whole the file to the server, but just some of its first bytes (enough to read the first rows ). how can i do that in C#? is there any way not to send whole the excel file, and reading first rows? as i know, we need the file path to read an excel file, but i wanna just use a small part of file like a stream : in code below i use "ExcelDataReader" to read the files:
public List<string> Getheader()
{
List<string> headers = new List<string>();
byte[] hdrToRead = System.Text.Encoding.UTF8.GetBytes(remainedXlsStr);
MemoryStream stream = new MemoryStream(hdrToRead);
IExcelDataReader excelReader = null;
switch (currentFileType)
{
case (XlsType.xlsx):
{
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
break;
}
case (XlsType.xls):
{
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
break;
}
case (XlsType.notSupported):
{
break;
}
}
if (excelContent.HasHeader)
excelReader.IsFirstRowAsColumnNames = true;
if (excelReader.Read())
{ // first row
for (int i = 0; i < excelReader.FieldCount; i++)
{
// fieldType = excelReader.GetValue(i).GetType();
headers.Add(excelReader.GetValue(i).ToString());
}
}
return headers;
}
... thanks
Upvotes: 3
Views: 1136
Reputation: 12768
I don't know of any way to read a partial compressed file (xlsx), and both COM and ODBC as ways to access xls files require having the entire file. If you really want to give yourself ulcers, you could probably track down the xls file specification and figure out how to parse the initial bytes of the file, but what you are trying to do has a very limited use case so I doubt you'll find anything that'll do it for you or even give you much of a leg forward on the way...
Upvotes: 2