某些Excel文件不能从共享path移动到SQL Server

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

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

Windows服务器中存在Windows服务,数据库和共享path。 这些年来应用程序运行良好。 但在最近几天,上述问题几乎每一个文件都在发生。

我们也安装了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() { } } } 

看着这个问题 ,这似乎是阅读Excel文件,而不是SQL表的问题。 尝试更改Excel连接string。

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

另外,看另一个答案 ,根本原因可能是上传Excel的新版本。