asp.net - 该进程无法访问 file.xlsx',因为它正被另一个进程使用。将数据从 ASPX 导出到 EXCEL 文件时
问题描述
当我尝试导出时,我得到一个异常,它说“进程无法访问文件“Mypath\Filename.xlsx”,因为它正被另一个进程使用”当我尝试导出时抛出异常,它被追溯到Line byte[] content = File.ReadAllBytes(fileName); 所以我不明白这个问题。我没有使用文件流来读取数据
这是我使用的代码
protected void BtnExport_Click(object sender, EventArgs e)
{
using (MydatabaseEntities dc = new MydatabaseEntities())
{
List<EmployeeMaster> emList = dc.EmployeeMasters.ToList();
StringBuilder sb = new StringBuilder();
if (emList.Count > 0)
{
string fileName = Path.Combine(Server.MapPath("~/ImportDocument"), DateTime.Now.ToString("ddMMyyyyhhmmss") + ".xlsx");
string conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + fileName + "; Extended Properties='Excel 12.0;HDR=yes'";
using (OleDbConnection con = new OleDbConnection(conString))
{
string strCreateTab = "Create table EmployeeData(" +
"[Employee ID] varchar(50)," +
"[Company Name] varchar(200)," +
"[Contact Name] varchar(200)," +
"[Contact Title] varchar(200)," +
"[Employee Address] varchar(200)," +
"[Postal Code] varchar(50))";
if (con.State == ConnectionState.Closed)
{
con.Open();
}
OleDbCommand cmd = new OleDbCommand(strCreateTab, con);
cmd.ExecuteNonQuery();
string StrInsert = "Insert into EmployeeData([Employee ID],[Company Name]," + "[Contact Name],[Contact Title],[Employee Address], [Postal Code]" +
")values(?,?,?,?,?,?)";
OleDbCommand cmdIns = new OleDbCommand(StrInsert, con);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 50);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 200);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 200);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 200);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 200);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 50);
foreach (var i in emList)
{
cmdIns.Parameters[0].Value = i.EmployeeId;
cmdIns.Parameters[1].Value = i.CompanyName;
cmdIns.Parameters[2].Value = i.ContactName;
cmdIns.Parameters[3].Value = i.ContactTitle;
cmdIns.Parameters[4].Value = i.EmployeeAddress;
cmdIns.Parameters[5].Value = i.PostalCode;
cmdIns.ExecuteNonQuery();
}
//create Downloadable File
byte[] content = File.ReadAllBytes(fileName);
HttpContext context = HttpContext.Current;
context.Response.BinaryWrite(content);
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=EmployeeData.xlsx");
context.Response.End();
con.Close();
}
}
}
}
}
所以请帮助我
解决方案
我真的很高兴能够通过我尝试的一个简单技巧来解决我的问题。我得到的错误是文件已经打开,是由我在创建可下载文件之前没有关闭的连接引起的!!这是我在粗体中所做的更改。:-)
protected void BtnExport_Click(object sender, EventArgs e)
{
using (MydatabaseEntities dc = new MydatabaseEntities())
{
List<EmployeeMaster> emList = dc.EmployeeMasters.ToList();
StringBuilder sb = new StringBuilder();
if (emList.Count > 0)
{
string fileName = Path.Combine(Server.MapPath("~/ImportDocument"), DateTime.Now.ToString("ddMMyyyyhhmmss") + ".xlsx");
string conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + fileName + "; Extended Properties='Excel 12.0;HDR=yes'";
using (OleDbConnection con = new OleDbConnection(conString))
{
string strCreateTab = "Create table EmployeeData(" +
"[Employee ID] varchar(50)," +
"[Company Name] varchar(200)," +
"[Contact Name] varchar(200)," +
"[Contact Title] varchar(200)," +
"[Employee Address] varchar(200)," +
"[Postal Code] varchar(50))";
if (con.State == ConnectionState.Closed)
{
con.Open();
}
OleDbCommand cmd = new OleDbCommand(strCreateTab, con);
cmd.ExecuteNonQuery();
string StrInsert = "Insert into EmployeeData([Employee ID],[Company Name]," + "[Contact Name],[Contact Title],[Employee Address], [Postal Code]" +
")values(?,?,?,?,?,?)";
OleDbCommand cmdIns = new OleDbCommand(StrInsert, con);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 50);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 200);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 200);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 200);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 200);
cmdIns.Parameters.Add("?", OleDbType.VarChar, 50);
foreach (var i in emList)
{
cmdIns.Parameters[0].Value = i.EmployeeId;
cmdIns.Parameters[1].Value = i.CompanyName;
cmdIns.Parameters[2].Value = i.ContactName;
cmdIns.Parameters[3].Value = i.ContactTitle;
cmdIns.Parameters[4].Value = i.EmployeeAddress;
cmdIns.Parameters[5].Value = i.PostalCode;
cmdIns.ExecuteNonQuery();
}
//create Downloadable File
**con.Close();**
byte[] content = File.ReadAllBytes(fileName);
HttpContext context = HttpContext.Current;
context.Response.BinaryWrite(content);
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=EmployeeData.xlsx");
context.Response.End();
}
}
}
}
}
推荐阅读
- javascript - 如何在Javascript中使用键值查找数组中的最新日期
- vim - 使用当前文件名和文件路径从 vim 执行 bash 命令
- android-studio - Android Studio 3.2.12 中的 .idea 文件夹是什么?
- php - 如何在mysql中编辑序列化内容
- c++ - 数组类指针在 main 中丢失了值
- mysql - Zapier + MySQL + PythonAnywhere
- javascript - 带有 Dc.Js 和更新 Jquery 数据表的过滤功能
- matlab - 在 MacOS 上运行为 Windows 编码的 Matlab p 文件
- github - 如何在 Github 中使用语法高亮编写 diff 代码
- reactjs - 无法在 react native 中连接 redux store