【问题标题】:Some Excel Files not moving from Shared Path to SQL Server某些 Excel 文件未从共享路径移动到 SQL Server
【发布时间】:2016-06-30 10:19:43
【问题描述】:

我们有一个应用程序,其中 Excel 文件中的数据(存在于共享路径中)移动到数据库。如果出现任何错误,文件会通过将错误写入日志文件来移动到错误文件夹。它使用 Windows 服务进行操作。

有时文件没有任何错误仍然通过写入日志External table is not in the expected format.移动到错误文件夹但是相同的文件再次上传一次或多次,它移动到数据库没有任何错误。

Windows 服务、数据库和共享路径存在于 XP Server 中。这些年来,应用程序运行良好。但是最近几天,几乎每个文件都出现上述问题。

我们也安装了Microsoft 2003、2007、2012办公组件和访问引擎。但问题仍然存在。

我在下面提到了 Windows 服务代码。请帮忙。提前致谢。

using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Common;

namespace Impexp_Service
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer T1 = new System.Timers.Timer();
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
          {
            ///start
            ///

            {
                SqlConnection strconnection = new SqlConnection();
                strconnection.ConnectionString = @"Data Source=XXXXXX;Initial Catalog=XXXX;User ID=XX;Password=XXXXXX;";
                strconnection.Open();
                // To get the all files placed at the shared path
                DirectoryInfo directory = new DirectoryInfo(@"D:\Impexp\Data\");
                FileInfo[] files = directory.GetFiles("*.xlsx");



                foreach (var f in files)
                {
                    string path = f.FullName;

                    // TO establish connection to the excel sheet
                    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
                    //Create Connection to Excel work book
                    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);



                    excelConnection.Open();
                    //Create OleDbCommand to fetch data from Excel
                    OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection);

                    DbDataReader dr = cmd.ExecuteReader();
                    // OleDbDataReader dReader;
                    // dReader = cmd.ExecuteReader();
                    SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection);
                    //Give your Destination table name
                    sqlBulk.DestinationTableName = "imp_master_test";
                    sqlBulk.WriteToServer(dr);

                    excelConnection.Close();

                    File.Delete(path);




                    // To move error files to the error folder



                    /// end


                    T1.Interval = 20000;
                    T1.Enabled = true;
                    T1.Start();

                    T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
                }
            }
    }


        void T1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            T1.Enabled = false;
            try
            {
                SqlConnection strconnection = new SqlConnection();
                strconnection.ConnectionString = @"Data Source=10.91.XXXXXX;Initial Catalog=XXXXX;User ID=XXXXX;Password=XXXXX;";
                strconnection.Open();
                // To get the all files placed at the shared path
                DirectoryInfo directory = new DirectoryInfo(@"D:\Impexp\Data\");
                FileInfo[] files = directory.GetFiles("*.xlsx");



                foreach (var f in files)
                {
                    string path = f.FullName;

                    // TO establish connection to the excel sheet
                    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
                    //Create Connection to Excel work book
                    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

                try
                {
                    excelConnection.Open();
                    //Create OleDbCommand to fetch data from Excel
                    OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection);

                    DbDataReader dr = cmd.ExecuteReader();
                    // OleDbDataReader dReader;
                    // dReader = cmd.ExecuteReader();
                    SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection);
                    //Give your Destination table name
                    sqlBulk.DestinationTableName = "imp_master_prod";
                    sqlBulk.WriteToServer(dr);

                    excelConnection.Close();

                    File.Delete(path);



                }
                // To move error files to the error folder
                catch (Exception exp)
                {

                    excelConnection.Close();
                    File.Move(path, Path.Combine(@"D:\Impexp\error\", f.Name));
                    string path1 = @"D:\Impexp\error\error.txt";
                    if (File.Exists(path1))
                    {
                        // Create a file to write to. 
                        using (StreamWriter sw = File.AppendText(path1))
                        {
                            sw.WriteLine("File : " + path + " : " + exp.Message);
                            sw.Flush();

                        }
                    }


                        T1.Enabled = true;
                        T1.Start();

                    }
                }
                strconnection.Close();

            // End of TRY 1

            }
            catch (UnauthorizedAccessException UAEx)
            {
                string path1 = @"D:\Impexp\error\error.txt";
                if (File.Exists(path1))
                {
                    // Create a file to write to. 
                    using (StreamWriter sw = File.AppendText(path1))
                    {
                        sw.WriteLine(UAEx.Message);
                        sw.Flush();

                    }
                }
                T1.Enabled = true;
                T1.Start();
            }
            catch (PathTooLongException PathEx)
            {
                string path1 = @"D:\Impexp\error\error.txt";
                if (File.Exists(path1))
                {
                    // Create a file to write to. 
                    using (StreamWriter sw = File.AppendText(path1))
                    {
                        sw.WriteLine(PathEx.Message);
                        sw.Flush();

                    }
                }
                T1.Enabled = true;
                T1.Start();
            }
            T1.Enabled = true;
            T1.Start();


        }

        protected override void OnStop()
        {
        }
    }
}

【问题讨论】:

  • 假设“很多文件”都失败了,那些通过的文件有什么“不同”吗? (例如,工作表的数量、[报告]工作表中的记录数量……)。最坏的情况,仔细查看失败和正常的文件:解压缩并比较所有(相关)文件。 PS:根据我的(有限)经验,由于某些区域设置(在服务器和/或客户端计算机上),从 excel 加载数据可能会失败。 PS:我认为这并不重要,因为它过去曾起作用;但连接字符串不应该说 Excel 12.0 Xml 吗? connectionstrings.com/ace-oledb-12-0
  • @deroby 感谢您的评论先生。您能否说明导致此问题的区域设置?
  • 还有我的连接字符串状态 :: string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";"; Excel 12.0;HDR=Yes;IMEX=1 之前和之后都有一个 \。这是造成麻烦吗??
  • 好吧,理论上 excel 以“本机格式”存储其信息,因此日期在内部存储为双精度。但是,有时用户将内容从一个地方复制粘贴到另一个地方并最终粘贴,例如日期信息作为字符串输入到 Excel 中。即使单元格被格式化为日期,Excel 也接受这一点。在那里有2000-04-18 看起来不错,但要表现出色则没有任何意义,因为区域设置需要2000/04/18(示例)。当您尝试使用 OLEDB 阅读此内容时,您可能会期待给定列的日期时间,但会显示字符串(包含无法识别的日期信息)。
  • 关于连接字符串:没有线索。我只是将您指向 connectionstrings 网站,因为我注意到缺少 Xml 部分。弄清楚事情是否重要的​​最好方法是简单地尝试一下恕我直言的影响。

标签: c# sql-server excel windows-services


【解决方案1】:

我对 OLEDB coms 和更新版本的 Excel 进行了一些搜索。似乎很多人都遇到了兼容性问题。

不幸的是,微软似乎没有对此给予任何关注。微软多年前就宣布放弃 OLEDB 功能,并且已经停止在其 Office 产品和 SQL 服务器中添加任何类型的内部支持。实际上, MSAccess Web 应用程序和 Web 数据库的正式关闭日期是 2018 年 4 月。在这种情况下,服务器的更新、windows 的客户端版本或 Excel 的客户端版本可能会触发此事件,并且看起来不像会有一个修复。我自己已经开始使用 3rd 方包(有免费的)来处理与办公产品的互操作,因为我厌倦了将头撞到墙上来创建解决方法。老实说,如果 Access 取消了以编程方式连接到 Access 数据库的能力,我不知道为什么 Access 仍然存在。

我知道这并不能解决您的问题,但最好面对事实并继续前进,而不是试图解决无法解决的问题。

【讨论】:

  • 感谢您抽出宝贵时间告诉我们您对这个问题的看法。非常感谢。
【解决方案2】:

查看this question,这似乎是读取 excel 文件而不是 SQL 表的问题。尝试更改 Excel 连接字符串。

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;HDR=Yes;IMEX=1\";

另外,查看another answer,根本原因可能是上传更新版本的 excel。

【讨论】:

    【解决方案3】:

    您是否使用带有连接字符串的 Excel 2007 文件:Microsoft.Jet.OLEDB.4.0 和 Extended Properties=Excel 8.0?

    您可以将字符串更改为其他字符串,如下所示:

    public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx";
    public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
    

    从以下链接更改连接字符串: http://www.connectionstrings.com/excel-2007

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多