Quantcast
Channel: Excel Data Reader - Read Excel files in .NET
Viewing all 448 articles
Browse latest View live

New Post: Microsoft Excel 97-2003 Worksheet (.xls) bad signature override?

$
0
0
First and mosr important:
This tool is superb. Most comprehansive and works perfect for me.

Still - I have a problem with my files:
I get from a third party Excel 97-2003 xls files with bad signature (or header). I checked the signature, and indeed, it is bad.
The starange thing is that MS Excel will open the files OK, while the ExcelBinaryReader will reject them as bad (Justifiedly).
I tried to find a way to override the signature checking, but it looks quite complex.
Can anyone help?

Here is the file:

https://drive.google.com/?usp=chrome_app#folders/0B-INd-LMMHaJMDNXSlV1Z2pFUzg
Thanks
Sam

New Post: Excel sheet to array

New Post: Excel sheet to array

$
0
0
Hi,
Me again.

So if I understood well the iDataReader feature I commited this piece of code (the DataSet remained for calculating number of rows and columns):
FileStream stream = File.Open(ExcelSourceFile, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = Path.GetExtension(ExcelSourceFile).ToLower() == ".xls" ? ExcelReaderFactory.CreateBinaryReader(stream) :ExcelReaderFactory.CreateOpenXmlReader(stream);
            
            DataSet ds = new DataSet();
            ds = excelReader.AsDataSet();
            rows = ds.Tables[0].Rows.Count;
            cols = ds.Tables[0].Columns.Count;

            int row=0;
            while (excelReader.Read())
            {
                for (int column = 0; column<=cols; column++)
                {
                    DataFromExcel[row, column] = excelReader[column].ToString();
                } // this is where code breaks with error: System.NullReferenceException
                row++;
            }
            // DataFromExcel[i, j] =
            excelReader.Close();
The error of this code says: System.NullReferenceException. Unfortunately I use VS Express so the debug tool is almost inexisting. At the top of the class there is a declaration:
object[,] DataFromExcel;
What I'm missing?

Best regards,
Mike.

New Post: Excel sheet to array

$
0
0
SHAME ON ME! I got the answer right in front of my eyes. I forgot to declare the size of the array.
            rows = ds.Tables[0].Rows.Count;
            cols = ds.Tables[0].Columns.Count;

            __DataFromExcel = new object[rows, cols];__
            int row=0;
            while (excelReader.Read())
Sorry for wasting your time.

Mike.

New Post: Import values from excel sheet [issue with dates and time]

$
0
0
Dear colleagues,
First I'd like to emphasize, that I read any possible topic related with dates and field formats.
Because I'm trying the possibilities of this library (but also I'm learning programming) I wanted to check whether the imported field from excel sheet is a date, datetime, time or any other stuff I prepared some excel with the dates and other type of fields.
Unfortunately I can't determine correctly what type of field is delivered in the excel sheet. To make my project faster and less time consuming I tried the method which I found on this forum. Instead of:
DataFromExcel[row, column] = excelReader[column].ToString();
I put:
DataFromExcel[row, column] = excelReader.GetValue(column).ToString();
"Value" should fix the problem with receiving field value with it's format without any changes. So for my test I did an excel sheet saved as XLS and XLSX. This is how I fill the array with data and then I display it with the messageBox:
DataFromExcel = new object[rows, cols+1];
            int row=0;
            while (excelReader.Read())
            {
                for (int column = 0; column<cols; column++)
                {
                    if (excelReader[column] != null)
                    {
                        DataFromExcel[row, column] = excelReader.GetValue(column).ToString(); // read the data from the excel sheet field
                    }
                    else
                    {
                        DataFromExcel[row, column] = "";
                    }

                    if (excelReader[2] != null) // check the type of the field (in my case it's DateTime )
                    {
                        DataFromExcel[row, 5] = excelReader.GetValue(2).GetType();
                    }
                    
                }
                row++;
            }
            excelReader.Close();


            for (int i = 1; i < rows; i++) // check what's imported.
            {
                MessageBox.Show("ROW " + i + ": " + DataFromExcel[i, 0] + " ^ " + DataFromExcel[i, 1] + " ^ " + DataFromExcel[i, 2] + " ^ " + DataFromExcel[i, 3] + " ^ " + DataFromExcel[i, 4] + " ^ " + DataFromExcel[i, 5]);
            }
And those are the results: For the XLSX file the datetime field is correctly imported (and shown in messageBox) and the provided type is System.DateTime. However for the XLS file instead of correct date I get the value e.g. '40613,3476851852' and the value type is shown as 'System.Double'.

My questions are:
  1. Is it possible to correctly check what is the type of the field in Excel sheet, so I could prepare some formatting for delivered data?
  2. Is it a bug with this dates format provided by the same method (GetValue) from the XLS file?
  3. Is there any other method to "download" data from Excel file with the values as they are written in the sheet (regardless to their type and format)?
I'd like to say, that I'd like to do something with the values delivered from the Excel sheet but I would like to assume, that the format of the excel sheet may be different every time I'd like to load it.

I'd appreciate for the support.

Best regards,
Michal

New Post: Reading merged cells

$
0
0
similar to [discussion:204248], I want to be able to read the same value for all merged cells , now If there is a merged cell, only the upper-left corner of the cell, has the value.
I really appreciate taking time to answer my question.

Created Unassigned: Unable to read full cell data [13071]

$
0
0
I am trying to read in a large amount of data using your wonderful tool, which is ideal.
The problem is that the file in question has very large data in a cell


Any help would be much appreciated

New Post: Get the available sheets present in Excel

$
0
0
I need to know the method to get the available sheets present in the excel file. Is there any predefined functions or Methods???

Please Help me...

New Post: Get the available sheets present in Excel

$
0
0
There is no predefined function. For our own application, I had to implement a new method, first in the interface:
        /// <summary>
        /// Retrieves the names of the worksheets
        /// </summary>
        /// <returns>A list containing the names of the worksheets</returns>
        List<string> GetWorksheetNames();
and then in ExcelBinaryReader.cs:
        public List<string> GetWorksheetNames()
        {
            List<string> namesList = new List<string>();
            foreach (XlsWorksheet worksheet in m_sheets)
            {
                namesList.Add(worksheet.Name);
            }

            return namesList;
        }
and ExcelOpenXmlReader.cs:
        public List<string> GetWorksheetNames()
        {
            List<string> namesList = new List<string>();
            for (int ind = 0; ind < _workbook.Sheets.Count; ind++)
            {
                namesList.Add(_workbook.Sheets[ind].Name);
            }

            return namesList;
        }
I submitted this and other changes allowing for a single worksheet to be retrieved into a DataTable (as opposed to the entire workbook into a DataSet) for consideration to be included in the next version of the Excel Data Reader.

To obtain the list of worksheets with the current Excel Data Reader code, you would have to call AsDataset() to load the entire workbook into a DataSet, and then iterate through the dataset's data tables to retrieve their names. By then, though, the content of the entire Excel file has been read.

New Post: Import values from excel sheet [issue with dates and time]

$
0
0
If there is a discrepancy in the date/time behaviour between XLS files and XLSW files, the first thing you should check is if you created the binary reader using one of the methods that has the convertOADate parameter, and set the value of the parameter to true, i.e.
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(filestream, true);

New Post: How to switch between sheets

$
0
0
I need to get information from sheet1 "your details" and then sheet2 "your sites"
foreach (DataTable dt in result.Tables)
            {
                if (dt.TableName.ToLower().Contains("your details"))
                {
                    excelReader.Read();
                    ... do stuff
                 }
                 else if(dt.TableName.ToLower().Contains("your sites"))
                {
                    excelReader.Read();
                    ... This is the problem,  I'm still reading from sheet "your details"
                }
But when the code finds the sheet "your sites" and I call [excelReader.Read();] it is still reading from the first sheet...
Can anyone help ?

Created Unassigned: XlsStream.ReadStream hangs on corrupted file [13090]

$
0
0
When I try to read a corrupted .xls file (with a part of file accidentally lost), my application hangs in an infinite loop, while I expect it to fail with some exception. As I use the library in a web application, such behavior can slow down the server greatly. It also creates a vulnerability to DDOS attacks. An example corrupted file is attached.

This issue can only be reproduced in "Any CPU" or "x64" build mode, as in "x86" OutOfMemory exception is thrown.

__Issue reason:__
I have discovered that the apllication loops in a "do while" cycle in XlsStream.ReadStream. The fat.GetNextSector never returns FATMARKERS.FAT_EndOfChain, so the loop never stops.

__Fix proposal:__
Infinite loop can easily be avoided by analyzing the return value of m_fileStream.Read call (the number of bytes read from file). When the file is read to end, m_fileStream.Read returns zero, and it means that we can exit the loop. So I added additional check to the "while" statement.
With such fix, an attempt to read a corrupted file results in an "ArgumentOutOfRange" exception thrown somewhere further in code. This is way better than a hang; however, the thrown exception is not informative. I guess we can throw an exception about file corruption explicitly when we get "zero" from m_fileStream.Read, but I'm not sure that in normal operation such situation can never happen (I just don't know the ExcelDataReader inner structure well enough). I have attached both variants of the fix.

Can the fix be added to the trunk version? Should I upload the patch in the "Source Code" section?

Thanks!

New Post: How to switch between sheets

$
0
0

It looks like you are using a mixture of dataset and idatareader. Just use one method.

New Post: How to switch between sheets

$
0
0
Thanks, I got it in the end... In casse anyone else lands here with the same problem - I found it easier to stick to datasets (easier to navigate & manipulate)
DataSet result = excelReader.AsDataSet();
            excelReader.IsFirstRowAsColumnNames = true;

            foreach (DataTable dt in result.Tables)
            {
                if(dt.TableName.ToLower().Contains("your sites"))
                {

                    DataSet ds = dt.DataSet;
                    DataTable Dt = ds.Tables["Your Sites"];

                    foreach (DataRow dr in Dt.Rows)
                    {
                        strSiteURL = (dr[1].ToString());
                        strCategory1 = (dr[2].ToString());
                        ....
                        ...
                    }
              }

New Post: How to read excel file if it is already open in Microsoft Excel?

$
0
0
Hello,
I am able to read all excel files with the code you provided but I cant read them if they are already open in Microsoft Excel.
Is it possible?
Thanks,

New Post: How to read excel file if it is already open in Microsoft Excel?

$
0
0
Yes, of course :)
Excel locks non-readonly files while modifying them.

Created Unassigned: ExcelCsvReader - CSV reader implementation [13097]

$
0
0
This is my contrib to the Library:

a CSV data reader implementation to extend ExcelReaderFactory.

New Post: csv

Commented Unassigned: ExcelCsvReader - CSV reader implementation [13097]

$
0
0
This is my contrib to the Library:

a CSV data reader implementation to extend ExcelReaderFactory.
Comments: ** Comment from web user: Ian1971 **

Thanks for this. I had thought about incorporating a CSV reader, but it wasn't high on my list because I had already integrated another Csv library. But it would be nice just to have one library.

I don't suppose you created unit tests for it did you?

I'll try and integrate this when I get some time (super busy all year so far).

Commented Unassigned: XlsStream.ReadStream hangs on corrupted file [13090]

$
0
0
When I try to read a corrupted .xls file (with a part of file accidentally lost), my application hangs in an infinite loop, while I expect it to fail with some exception. As I use the library in a web application, such behavior can slow down the server greatly. It also creates a vulnerability to DDOS attacks. An example corrupted file is attached.

This issue can only be reproduced in "Any CPU" or "x64" build mode, as in "x86" OutOfMemory exception is thrown.

__Issue reason:__
I have discovered that the apllication loops in a "do while" cycle in XlsStream.ReadStream. The fat.GetNextSector never returns FATMARKERS.FAT_EndOfChain, so the loop never stops.

__Fix proposal:__
Infinite loop can easily be avoided by analyzing the return value of m_fileStream.Read call (the number of bytes read from file). When the file is read to end, m_fileStream.Read returns zero, and it means that we can exit the loop. So I added additional check to the "while" statement.
With such fix, an attempt to read a corrupted file results in an "ArgumentOutOfRange" exception thrown somewhere further in code. This is way better than a hang; however, the thrown exception is not informative. I guess we can throw an exception about file corruption explicitly when we get "zero" from m_fileStream.Read, but I'm not sure that in normal operation such situation can never happen (I just don't know the ExcelDataReader inner structure well enough). I have attached both variants of the fix.

Can the fix be added to the trunk version? Should I upload the patch in the "Source Code" section?

Thanks!
Comments: ** Comment from web user: Ian1971 **

Thanks. I'll take a look at it as soon as I get some time.

Viewing all 448 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>