views:

44

answers:

1

I have a couple of columns of data in an excel sheet which I have to import to my application.

I do this using -

string strConn;
OleDbDataAdapter oledaExcelInfo;

strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Server.MapPath(strSavePath + strRepFileName) + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
oledaExcelInfo = new OleDbDataAdapter("SELECT * FROM [Book1]", strConn);

dsetExcelInfo = new DataSet();            
oledaExcelInfo.Fill(dsetExcelInfo, "CCInfo");

While this imports data successfully, sometimes the data is imported incorrectly i.e. sometimes a number 1234567 could be imported as 1.23E+06

This can be solved if the data in the excel file is '1234567 instead of 1234567 (append the single quotation mark)

I am now trying to manipulate data that I fetch from the excel so that before I import the data I can programmatically append a ' to all the values to prevent the incorrect import.

I even tried using OleDbDataAdapter.Update but I guess this will not help as this happens after the data is imported. Is that correct?

Can I manipulate data so that I import the correct data? How can I do it?

A: 

I used the open source NPOI library to import data from excel and preserve format of the data as it was in the spread sheet i.e. 1234567 would be imported as 1234567 instead of importing data in a different format like 1.23E+06

Instead of importing data as shown in the question above, I import data using NPOI library -

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

HSSFWorkbook hssfworkbook;
using (FileStream file = new FileStream(Server.MapPath(strSavePath + strRepFileName), FileMode.Open, FileAccess.Read))
{
     hssfworkbook = new HSSFWorkbook(file);
}
Sheet sheet = hssfworkbook.GetSheet("Book1");

DataTable dt = new DataTable();    //Create datatable 
dt.Columns.Add();             //Add data columns

for (int count = 0; count <= sheet.LastRowNum; count++)
{
    DataRow dr = dt.NewRow();         //Create new data row

    //Based on the type of data being imported - get cell value accordingly    
    dr[0] = sheet.GetRow(count).GetCell(0).StringCellValue;   
    //dr[0] = sheet.GetRow(count).GetCell(0).NumericCellValue;
    //dr[0] = sheet.GetRow(count).GetCell(0).RichStringCellValue;
    //dr[0] = sheet.GetRow(count).GetCell(0).DateCellValue;
    //dr[0] = sheet.GetRow(count).GetCell(0).BooleanCellValue;

    dt.Rows.Add(dr);  //Add row to datatable
}
Pavanred