从 Excel 导入数据集之前操作数据

发布于 2024-09-13 17:03:49 字数 819 浏览 16 评论 0原文

我的 Excel 工作表中有几列数据,我必须将其导入到我的应用程序中。

我这样做是使用 -

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");

虽然成功导入数据,但有时数据导入不正确,即有时可以将数字 1234567 导入为 1.23E+06

这可以解决,如果excel 文件中的数据是 '1234567 而不是 1234567 (附加单引号)

我现在尝试操作从 excel 获取的数据,以便在导入之前我可以通过编程方式将 ' 附加到所有值的数据,以防止错误导入。

我什至尝试使用 OleDbDataAdapter.Update 但我想这不会有帮助,因为这种情况发生在导入数据之后。这是正确的吗?

我可以操纵数据以便导入正确的数据吗?我该怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念﹏祤嫣 2024-09-20 17:03:49

我使用开源NPOI库从Excel导入数据并保留数据的格式,因为它是在电子表格即 1234567 将作为 1234567 导入,而不是以 1.23E+06 等不同格式导入数据,

而不是如图所示导入数据上面的问题,我使用 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 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
}
椵侞 2024-09-20 17:03:49

我刚刚遇到了类似的问题(在这里找到它:Quirky SELECT from通过 OleDbDataAdapter 方法 (C#) 的 Excel 文件。

如果您只想使用 .NET 框架而不使用外部库,请尝试使用以下连接字符串:

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

将引擎从 JET 切换到 ACE 对我来说是这样。

I just got a similar problem ( find it here: Quirky SELECT from Excel file via OleDbDataAdapter method (C#) ).

Try with the following connection string, if you want to use just .NET framework without external libraries:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文