Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

import an xlsx(Excel2007) file and it throws following error.

$
0
0

 

Hi!

I am using

ICSharpCode.SharpZipLib.dll Assembly version 0.85.5.452

I m trying to import an xlsx(Excel2007) file and it throws following error.

"Arithmetic operation resulted in an overflow"

Exception is raised at 

       string[ val = new string[excelReader.FieldCount];

but before this line when following line is executed

excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

the excelReader properteis shows following error

RecordsAffected = '((Excel.ExcelOpenXmlReader)(excelReader)).RecordsAffected' threw an exception of type 'System.NotSupportedException'

 

 

however when i "select all" data of the same file and save it into new xlsx file. it is successfully imported.

The file i trying to import is send by our client... some of the files are working find but few are giving this problem

but following error exists in every excel wheather imported or not 

RecordsAffected = '((Excel.ExcelOpenXmlReader)(excelReader)).RecordsAffected' threw an exception of type 'System.NotSupportedException'

 

i want to know the reason..

please find the code below for consideration

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void CopyExcelToDatatable1(string strExcelFileName, SqlConnection objCon, SqlTransaction objTrans)

        {

            DataTable dt = new DataTable(Path.GetFileNameWithoutExtension(strExcelFileName));

            string strFileExt = Path.GetExtension(strExcelFileName);

            FileStream stream = File.Open(strExcelFileName, FileMode.Open, FileAccess.Read);

            IExcelDataReader excelReader = null;

 

            if (strFileExt.ToLower() == ".xlsx")

                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

            else

                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

 

            excelReader.Read();

 

            string[ val = new string[excelReader.FieldCount];

            for (int col = 0; col < excelReader.FieldCount; col++)

            {

                val[col] = Convert.ToString(excelReader.GetValue(col));

            }

 

            for (int colIndex = 0; colIndex < val.Length; colIndex++)

                dt.Columns.Add(val[colIndex].ToString());

 

            long counter = 0;

 

            try

            {

                while (excelReader.Read())

                {

                    counter++;

                    val = new string[excelReader.FieldCount];

                    for (int col = 0; col < excelReader.FieldCount; col++)

                    {

                        val[col] = Convert.ToString(excelReader.GetValue(col));

                    }

                    dt.Rows.Add(val);

                }

 

                excelReader.Close();

                excelReader.Dispose();

                dt.Dispose();

                GC.Collect();

            }

           catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

       }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 


Viewing all articles
Browse latest Browse all 1764

Trending Articles