データベーステーブルからExcelファイルを生成するのにEPPLUSを使用していますが、作成されたファイルは完全なSSISプロセスが停止するまで読み取り専用モードのままです。 私は、後の工程で後にファイルを移動する必要があり、これは常にSSISに次のメッセージで失敗します:私は、「使用中のファイル」を得たExcelでファイルを開こうとするとEPPLUSを使用してデータベーステーブルからExcelファイルを生成するとき、ファイルは常に読み取り専用です
私はこの問題をカバーするために縫い目はありません、私は4.1に更新する予定EPPLUS 4.0.5が、リリースノートを使用してい[File System Task] Error: An error occurred with the following error message: "The process cannot access the file because it is being used by another process.".
public void Main()
{
try
{
String FilePath = Dts.Variables["$Package::DestinationFileName"].Value.ToString();
String TableName = Dts.Variables["$Package::SourceTableName"].Value.ToString();
String ConnStr = Dts.Variables["$Project::ConnStr_DataWarehouse"].Value.ToString();
//SqlConnection Conn = (SqlConnection)(Dts.Connections["DW"].AcquireConnection(Dts.Transaction) as SqlConnection);
using (SqlConnection Conn = new SqlConnection(ConnStr))
{
String Sql = "SELECT * FROM " + TableName;
if (File.Exists(FilePath))
{
try { File.Delete(FilePath); }
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); Dts.TaskResult = (int)ScriptResults.Failure; }
}
using (DataTable dt = new DataTable())
{
using (SqlCommand cmd = new SqlCommand(Sql, Conn))
{
Conn.Open();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
FileInfo newFile = new FileInfo(FilePath);
using (ExcelPackage p = new ExcelPackage(newFile))
{
using (ExcelWorksheet ws = p.Workbook.Worksheets.Add("RejectetionReport"))
{
ws.Cells["A1"].LoadFromDataTable(dt, true);
p.Save();
}
}
}
Conn.Close();
}
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
:
book1.xlsx is locked for editing by 'another user'. Open 'Read-Only' or click 'Notify' to open read-only and receive notification when the document is no longer in use. I hope you'll be able to help me.
は、ここに私のコードです。
編集: 4.1にアップグレードしましたが、引き続き問題があります。
これは長く報告されている問題に関連しているようです: https://epplus.codeplex.com/workitem/14919 – Kilren
あなたのコードとepplusのドキュメントを見て、あなたが必要としているように見えますが、おそらくepplus {}ブロックがオブジェクトを正しく処分していないというバグがあります。あなたは自分自身を処分して、使用を排除しようとすることができます。 – Matt
私はすでに、すべての使い捨て可能なオブジェクトを手動で廃棄しようとしました。 オブジェクトをヌルに設定しようとします。 – Kilren