c# - Take contents of an excel file (.xls or .xlsx) in to Dataset -
i have excel file named test.xls , want contents in excel sheet dataset.is possible tried code throws exception,here code
string filepath = server.mappath("portals\\_default") + "\\" + upprice.filename; upprice.postedfile.saveas(filepath); filestream stream = file.open(filepath, filemode.open, fileaccess.read); if (upprice.filename.contains(".xlsx")) { iexceldatareader excelreader = excelreaderfactory.createbinaryreader(stream); dataset result = excelreader.asdataset(); }
i'm going assume you're using http://exceldatareader.codeplex.com/
from code:
if (upprice.filename.contains(".xlsx")) { iexceldatareader excelreader = excelreaderfactory.createbinaryreader(stream); dataset result = excelreader.asdataset(); } else if (upprice.filename.contains(".xls")) { iexceldatareader excelreader = excelreaderfactory.createopenxmlreader(stream); dataset result = excelreader.asdataset(); }
these tests backwards. ".xlsx" files zipped xml documents. "xls" older binary files. consider using system.io.path.getextension() file extension since you'll notice contains(".xls") true both file types.
Comments
Post a Comment