You could use Apache's POI.
It's a java library for reading and writing MS Office formats. Since it's java and you're using C# you'll need IKVM and the java classes from the POI Project.
However, the easiest way is to just download Jon Iles excelent MPXJ project and you've got it all. Just set a reference to IKVM.OpendJDK.ClassLibrary.dll
, IKVM.Runtime.dll
, poi-3.2-FINAL-20081019.dll
I've hacked together a quick console app to show you an simple way to read an Excel sheet. It only reads the first sheet and doesn't use the row or cell iterators, but it does the job well.
With a very small amount of effort i'm sure you could figure out how to use an input stream rather than a file.
//C# code for using the Apache POI libraries
using System;
using System.Collections.Generic;
using System.Text;
// poi for xls
using org.apache.poi;
using org.apache.poi.poifs;
using org.apache.poi.poifs.filesystem;
using org.apache.poi.hssf;
using org.apache.poi.hssf.usermodel;
using org.apache.poi.ss;
namespace ConsoleApplication1
{
class Test
{
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.Out.WriteLine("Usage: XLSReadTest <xls file>");
}
else
{
XLSRead x = new XLSRead();
x.Process(args[0]);
//x.Process("c:\\temp\\testfile.xls");
}
}
}
class XLSRead
{
public void Process(string inputFile)
{
int r = 0;
Console.Out.WriteLine("Reading input file started.");
DateTime start = DateTime.Now;
java.io.InputStream inputStream = new java.io.FileInputStream(inputFile);
POIFSFileSystem fs = new POIFSFileSystem(inputStream);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sh = wb.getSheetAt(0);
r = sh.getFirstRowNum();
while (r <= sh.getLastRowNum())
{
HSSFRow row = sh.getRow(r);
int c = row.getFirstCellNum();
string val = "";
while (c < row.getLastCellNum())
{
HSSFCell cell = row.getCell(c);
switch(cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
val = cell.getNumericCellValue().ToString();
break;
case HSSFCell.CELL_TYPE_STRING:
val = cell.getStringCellValue();
break;
}
Console.Out.WriteLine("Row: " + r + ", Cell: " + c + " = " + val);
c++;
}
r++;
}
long elapsed = DateTime.Now.Ticks - start.Ticks;
String seconds = String.Format("{0:n}", elapsed / 1000000);
Console.Out.WriteLine("\r\n\r\nReading input file completed in " + seconds + "s." + "\r\n");
}
}
}