I'm using ExcelDataReader v.2.1. library to read both xls and xlsx files in my C# project. This way:
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader;
string extension = Path.GetExtension(filePath);
if (extension == ".xls")
{
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (extension == ".xlsx")
{
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
else
{
throw new NotSupportedException("Wrong file extension");
}
return excelReader;
Then, I use "AsDataSet" method to get a filled DataSet:
_dataSet = GetDataReader(_options.Filepath).AsDataSet();
It works fine most of the times, but, with some XLSX files, it only reads the first column.
I've been looking at excelReader instance with a Watch and I saw that it actually gets all the values of the Excel, but then, when using the AsDataSet method, it only reads the first column.
Do you know what can be going wrong here? Do you think it could be a "AsDataSet" method bug?
install ExcelDataReader.DataSet via nugget
Install-Package ExcelDataReader.DataSet -Version 3.6.0
Install DataSet to work with AsDataSet
Related
ExcelDataReader documentation says that it can open .xlsb files.
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
// Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
// - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
// Choose one of either 1 or 2:
// 1. Use the reader methods
do
{
while (reader.Read())
{
// reader.GetDouble(0);
}
} while (reader.NextResult());
// 2. Use the AsDataSet extension method
var result = reader.AsDataSet();
// The result of each spreadsheet is in result.Tables
}
}
But when I try to open a .xlsb stream, it gives an error saying that it can't open a zip file.
I have tried to see if any free third party lib that can convert xlsb to XLS, without any luck.
I am using the ExcelDataReader.Dataset package to read in .xlsx files and store specific sheets as a DataTable, like so:
public void SelectWorkbookClick(string s)
{
string fileName = string.Format("{0}\\{1}", WorkbookFolder, Workbook);
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
FrontSheet = reader.AsDataSet().Tables[FrontSheetName];
RearSheet = reader.AsDataSet().Tables[RearSheetName];
}
}
}
This works perfectly for reading in sheets, however my .xlsx file has named ranges in which I need to access.
I have had a look around and cannot not find any support for this, does anyone know of anyways I could go around this?
This thread seems not helping me.
What I want to do is to read a excel .xlsx file contents to replace values of some cells and return the new file contents to the client. But the original file should remain as is. I don't want to save the new file to the system - it's not a solution.
Here is the code:
string excelFilePath = this.Server.MapPath("~/App_Data/Test.xlsx");
var fileStream = System.IO.File.Open(excelFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
Excel.IExcelDataReader exReder = Excel.ExcelReaderFactory.CreateOpenXmlReader(fileStream);
System.Data.DataSet dataSet = null;
using (exReder)
{
dataSet = exReder.AsDataSet();
}
if (dataSet == null)
{
throw new ArgumentNullException("Cannot Make Data Set");
}
dataSet.Tables[0].Rows[0].ItemArray = new[] { "Microsoft", "Test", "ASP.NET" };
bool hasChanges = dataSet.HasChanges(); // true
dataSet.AcceptChanges();
bool hasChanges2 = dataSet.HasChanges(); // false
var dataReader = dataSet.CreateDataReader(dataSet.Tables[0]);
TextReader textReader = dataReader.GetTextReader(1); // 1 is ordinal no matter what I pass it throws an exception
byte[] results = System.Text.Encoding.UTF8.GetBytes(textReader.ReadToEnd());
return this.File(results, System.Net.Mime.MediaTypeNames.Application.Octet);
I am using http://exceldatareader.codeplex.com/ package
dataReader.GetTextReader(1); always throws an exception. How to make this text reader? Or just get the bytes after the change?
Consider using the Microsoft provided Excel.Interop .net libraries. They are natively designed to perform these functions. Here's a dotnetperls blog on this:
Currently I found a workaround with http://epplus.codeplex.com/ .
I'm trying to insert multiple records with excel file to database. Scenerio is, the user selects the excel file from his hard drive then the code reads first row and shows it as the title columns. These columns are not ordered. So user matches right columns with drop down.
Everything is okay until now. But after matching I have to read the file again from another action. But i am not saving file to server. Is there any way to read or hold the file in variable or session?
My code similiar like this
public JsonResult ExcelUpload(FormCollection formCollection)
{
if (Request != null)
{
HttpPostedFileBase file = Request.Files["ExcelFile"];
}
}
If you mean by "another action", you mean a different HTTP request, then you can't do that without it persisting somewhere.
Better read file in data table by using library given here
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
and store DataSet into session for further processing.
I do not understand all the mechanics surrounding Streams and even less around the System.IO.Package class.
I have a .docx document as a binary in a DataBase and I wan't to fetch it, modify somewhat and then save it.
I currently have the method that modifies the document in a seperate library because it will be used in many places.
This is how I tried to do this:
byte[] doc = getDocFromDB();
using (MemoryStream mem = new MemoryStream())
{
mem.Write(doc, 0, doc.Length);
Package pack = Package.Open(mem, FileMode.Open, FileAccess.ReadWrite);
filler.FillTemplate(ref pack, someIrreleventData);
string filePath = Path.GetTempPath() + "docname.docx";
using (FileStream file = new FileStream(filePath, FileMode.Create))
{
mem.WriteTo(file);
file.Flush();
file.Close();
}
Process.Start(filePath);
}
With the library code going something like this:
public void FillTemplate(ref Package package, XElement data)
{
WordprocessingDocument document = WordprocessingDocument.Open(package);
//add the data to the document
//should I do document.close() or document.dispose() here?
}
The document just comes out just as it was saved into the DB without all the extra data added in.
I assumed as I opened the Package with a memory stream all the changes to the package would be saved into the stream as well.
What am I doing wrong and how can I do it better.
EDIT
I was wrong, there isn't anything broken with my code. Problem was that someIrreleventData part was null and both the fetcher there and the code inside FillTemplate method didn't handle the exception correctly.
I don't see any place where you call Flush() and/or Close() on the Package before trying to save it to file...
try changing
mem.Write(doc, 0, doc.Length);
mem.Position = 0; // new, perhaps this is relevant for Package.Open ?
Package pack = Package.Open(mem, FileMode.Open, FileAccess.ReadWrite);
filler.FillTemplate(ref pack, someIrreleventData);
pack.Flush(); pack.Close(); // new
mem.Position = 0; // new
AND: yes, you should call document.Close() .
Calling .Dispose() is a good idea though it would even be better if you used as using block which takes care of that and several other things...