简体   繁体   English

从excel导入到数据集之前先处理数据

[英]manipulate data before importing from excel to dataset

I have a couple of columns of data in an excel sheet which I have to import to my application. 我必须将Excel表格中的几列数据导入到应用程序中。

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 ie sometimes a number 1234567 could be imported as 1.23E+06 尽管此操作成功导入了数据,但有时数据输入不正确,即有时可以将数字1234567导入为1.23E+06

This can be solved if the data in the excel file is '1234567 instead of 1234567 (append the single quotation mark) 如果excel文件中的数据是'1234567而不是1234567 (添加单引号),则可以解决此问题

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. 现在,我试图处理从excel中获取的数据,以便在导入数据之前,可以以编程方式在所有值后附加一个' ,以防止错误导入。

I even tried using OleDbDataAdapter.Update but I guess this will not help as this happens after the data is imported. 我什至尝试使用OleDbDataAdapter.Update,但我想这将无济于事,因为在导入数据后会发生这种情况。 Is that correct? 那是对的吗?

Can I manipulate data so that I import the correct data? 我可以操纵数据以便导入正确的数据吗? How can I do it? 我该怎么做?

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 ie 1234567 would be imported as 1234567 instead of importing data in a different format like 1.23E+06 我使用开放源代码的NPOI库从excel导入数据并保留数据的格式,就像在电子表格中一样,即1234567将作为1234567导入,而不是以1.23E+06类的其他格式导入数据

Instead of importing data as shown in the question above, I import data using NPOI library - 我不使用上面的问题来导入数据,而是使用NPOI库导入数据-

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
}

I just got a similar problem ( find it here: Quirky SELECT from Excel file via OleDbDataAdapter method (C#) ). 我只是遇到了类似的问题(在这里找到它: 通过OleDbDataAdapter方法(C#)从Excel文件进​​行古怪的SELECT )。

Try with the following connection string, if you want to use just .NET framework without external libraries: 如果只想使用.NET框架而不使用外部库,请尝试使用以下连接字符串:

string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";";  

Switching the engine from JET to ACE did it for me. 我将引擎从JET切换到ACE做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM