有没有办法强制 Microsoft.Jet.OLEDB 从 Excel 获取 MM/DD/YYYY 格式的日期列?

发布于 2024-09-15 03:05:55 字数 1239 浏览 5 评论 0原文

我在从 Excel 工作表读取 DateColumns 时遇到问题。

有时人们使用不同的日期格式,这会带来问题。假设当我期望从 Excel 列中得到 07/26/2010 时,我得到 26-Jul-2010 因为用户更改了其日期格式。

我使用 Microsoft.Jet.OLEDB 将 xls 表读取到 DataTable 中。

我能否以某种方式强制 OleDb 阅读器(无论 XLS 上设置的 DateFormat 如何)将所有日期转换为 MM/DD/YYYY 格式?

我使用这段代码来读取 Excel 文件:

string strConn;
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
          "Data Source=" + uploadedFileInfo.FullName + ";" +
          @"Extended Properties=""Excel 8.0;HDR=NO;""";


using (OleDbConnection connToExcel = new OleDbConnection(strConn))
{
    //You must use the $ after the object you reference in the spreadsheet
    connToExcel.Open();

    string firstSheetName = ExcelUploadedFileReaderBuilder
                            .GetFirstExcelSheetName(connToExcel);

    OleDbDataAdapter myCommand 
    = new OleDbDataAdapter(String.Format("SELECT * FROM [{0}]", firstSheetName), connToExcel);

    DataSet myDataSet = new DataSet();

    myCommand.Fill(myDataSet, "uploadedExcelTable");

    DataTable dtUploadedExcel = myDataSet.Tables["uploadedExcelTable"];

    lineCount = GetLineNumberWhereNULLRowOccured(dtUploadedExcel) + 1;

    connToExcel.Close();
}

I am having a problem with reading DateColumns from an excel sheet.

Sometimes people use different date Formats and this brings a problem. Let's say when I expect 07/26/2010 from an Excel column I get 26-Jul-2010 because the user has changed its date format.

I use Microsoft.Jet.OLEDB for reading the xls sheet into a DataTable.

Can I somehow force OleDb reader whatever the DateFormat is set on XLS, to convert all the dates into MM/DD/YYYY format?

I use this piece of code to read the Excel file:

string strConn;
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
          "Data Source=" + uploadedFileInfo.FullName + ";" +
          @"Extended Properties=""Excel 8.0;HDR=NO;""";


using (OleDbConnection connToExcel = new OleDbConnection(strConn))
{
    //You must use the $ after the object you reference in the spreadsheet
    connToExcel.Open();

    string firstSheetName = ExcelUploadedFileReaderBuilder
                            .GetFirstExcelSheetName(connToExcel);

    OleDbDataAdapter myCommand 
    = new OleDbDataAdapter(String.Format("SELECT * FROM [{0}]", firstSheetName), connToExcel);

    DataSet myDataSet = new DataSet();

    myCommand.Fill(myDataSet, "uploadedExcelTable");

    DataTable dtUploadedExcel = myDataSet.Tables["uploadedExcelTable"];

    lineCount = GetLineNumberWhereNULLRowOccured(dtUploadedExcel) + 1;

    connToExcel.Close();
}

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

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

发布评论

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

评论(2

死开点丶别碍眼 2024-09-22 03:05:55

您不必按照建议循环访问数据集。您可以通过在查询中指定所需的格式,让查询为您完成所有格式设置。
示例如下:

OleDbDataAdapter myCommand = 
  new OleDbDataAdapter("SELECT FORMAT([DateCol], 'MM/dd/yyyy') as [DateCol] FROM [SheetName]", connToExcel);

只要您使用 JET OLEDB 或 Ace OLEDB 数据提供程序,就保证可以正常工作。我不能保证它可以与其他数据提供商一起使用,但您可以随时检查。

You don't have to loop through the dataset as suggested. You can have your query do all the formatting for you by specifying in your query the format you want.
An example is as follows:

OleDbDataAdapter myCommand = 
  new OleDbDataAdapter("SELECT FORMAT([DateCol], 'MM/dd/yyyy') as [DateCol] FROM [SheetName]", connToExcel);

This is guaranteed to work as long as you're using JET OLEDB or the Ace OLEDB data provider. I cannot guarantee it'll work with other data providers but you can always check.

别再吹冷风 2024-09-22 03:05:55

循环遍历数据集。对于每个日期,将其转换为日期(如果单元格已修改为文本),然后将日期格式设置为“MM/DD/YYYY”形式的字符串。

以下代码是帮助您入门的示例:

string date1 = "07/26/2010";
string date2 = "26-Jul-2010";

DateTime dt1 = Convert.ToDateTime(date1);
DateTime dt2 = Convert.ToDateTime(date2);

string date1B = dt1.ToString("MM/DD/YYYY");
string date2B = dt2.ToString("MM/DD/YYYY");

date1B 和 date2B 将相等并且采用您想要的格式。

存在一些来回转换,这是因为您需要验证用户输入,因为它来自 Excel,并且用户很容易输入不符合您的数据库模型的数据。

最好将所有内容都放在 Try-Catch 块中,以防某些输入的日期本身不是有效日期。

Loop through the dataset. For each date, covert it to a date (in case the cell has been modified to be text), and then format the date as a string of form "MM/DD/YYYY".

The following code is an example to get you started:

string date1 = "07/26/2010";
string date2 = "26-Jul-2010";

DateTime dt1 = Convert.ToDateTime(date1);
DateTime dt2 = Convert.ToDateTime(date2);

string date1B = dt1.ToString("MM/DD/YYYY");
string date2B = dt2.ToString("MM/DD/YYYY");

date1B and date2B will be equal and in the format that you desire.

There is a bit of back and forth conversion going on and that is because you'll need to validate user input since it is coming from Excel and it is very easy for a user to enter data that won't conform with you database model.

It will be a good idea to put everything in a Try-Catch block in case some of the entered dates are not valid dates themselves.

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