【发布时间】: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