0

I want to read all columns as text values only from an excel file in a web application irrespective of the type of the data(date, number etc).

Please see the connection string used. Only ACE driver is installed and we can't use any other drivers in production server.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;
                             Extended Properties="Excel 12.0;HDR=YES;IMEX=1";

Setting IMEX=1 doesn't work. It returns null for some values.

I could see a lot of articles but am couldn't see a right answer.

Any help would be appreciated.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
Biju Thomas
  • 1,079
  • 3
  • 14
  • 27
  • So your connection works? The issue you're having is reading the data? If so, can you post a little but of the code where you are extracting the data out of the excel file – astro boy Jun 26 '12 at 03:46
  • @astroboy: Yes connection string works and data is loaded into data table. But with null for some value. there is one column which has numeric values. if i put a text value in any of the cell in that column then it is treated as null. – Biju Thomas Jun 26 '12 at 03:56

1 Answers1

3

I think this is what you are after ACE work around

Unfortunately, you can't set ImportMixedTypes or TypeGuessRows from the connection string since those settings are defined in the registry. For the ACE OleDb driver, they're stored at

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Excel

in the registry. So, you can simplify your connection string to get rid of some of those extended properties.

Once you set TypeGuessRows to 0 and ImportMixedTypes to Text in the registry, you should get the behavior you are expecting.


Or you can use Microsoft.Office.Interop.Excel to read the file. Sample code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelTut
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/C.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                {
                    MessageBox.Show(xlRange.Cells[i, j].Value2.ToString());
                }
            }

        }

    }
}

code taken from read excel file via interop

Jaskier
  • 1,075
  • 1
  • 10
  • 33
astro boy
  • 1,410
  • 1
  • 11
  • 16
  • Is there any other workaround? We cant change the Registry values since there are multiple application hosted in the production server. – Biju Thomas Jun 26 '12 at 04:53