62,046
社区成员
发帖
与我相关
我的任务
分享
DataSet ds = PowerGisDB.GetDataSet(sql);
string filePath = Server.MapPath("~/Report_1/doc/" + Guid.NewGuid().ToString() + ".xls");
File.Copy(Server.MapPath("~/Report_1/tww.xls"), filePath);
// 使用OleDb驱动程序连接到副本
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;");
using (conn)
{
try
{
conn.Open();
// 增加记录
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([序号],[线路名称], [户名], [容量],[配变型式],[idKey],[年份],[月份],[实际电量]) VALUES(@t0,@t1,@t2, @t3,@t4,@t5,@t6,@t7,@t8)", conn);
cmd.Parameters.AddWithValue("@t0", i + 1);
cmd.Parameters.AddWithValue("@t1", ds.Tables[0].Rows[i]["线路名称"].ToString());
cmd.Parameters.AddWithValue("@t2", ds.Tables[0].Rows[i]["户名"].ToString());
cmd.Parameters.AddWithValue("@t3", ds.Tables[0].Rows[i]["容量"].ToString());
cmd.Parameters.AddWithValue("@t4", ds.Tables[0].Rows[i]["配变型式"].ToString());
cmd.Parameters.AddWithValue("@t5", ds.Tables[0].Rows[i]["idKey"].ToString());
cmd.Parameters.AddWithValue("@t6", ds.Tables[0].Rows[i]["年份"].ToString());
cmd.Parameters.AddWithValue("@t7", ds.Tables[0].Rows[i]["月份"].ToString());
cmd.Parameters.AddWithValue("@t8", ds.Tables[0].Rows[i]["实际电量"].ToString());
cmd.ExecuteNonQuery();
}
}
//运行到这里Guid.NewGuid().ToString().xls这个文件就被 WebDev。Webserver.exe占用 有时候释放 的 早,有时候释放的晚 就会导致File.ReadAllBytes(这个excel被进程还用着呢 当然会出错了哦)
catch(System.Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
//this.KillProcess("EXCEL.EXE");
//输出副本的二进制字节流
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=superengine.xls");
/* tww add at 20100506
我认为之所以出现进程占用的的错误,是因为当excel
写入的内容较多时,操作系统(WINDOWS2000)关闭excel
进程时间要长一些,而.NET下载代码运行很快,
马上会要求读取excel 的文档,这时操作系统尚未处理完excel 的进程,
因此excel 文档处于收保护状态,因此造成.NET进程和excel 进程的冲突。
*/
this.killProcess();
System.Threading.Thread.Sleep(3000);//为了延缓
byte[] tww = File.ReadAllBytes(。。。));
Response.BinaryWrite(tww);
// 删除副本
try
{
File.Delete(filePath);
}
catch(System.IO.IOException ex)
{
}
finally
{
File.Delete(Server.MapPath("~/Report_1/doc/tww123.xls"));
}
Response.End();
DataSet ds = PowerGisDB.GetDataSet(sql);
string filePath = Server.MapPath("~/Report_1/doc/" + Guid.NewGuid().ToString() + ".xls");
File.Copy(Server.MapPath("~/Report_1/tww.xls"), filePath);
// 使用OleDb驱动程序连接到副本
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;");
using (conn)
{
try
{
conn.Open();
// 增加记录
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([序号],[线路名称], [户名], [容量],[配变型式],[idKey],[年份],[月份],[实际电量]) VALUES(@t0,@t1,@t2, @t3,@t4,@t5,@t6,@t7,@t8)", conn);
cmd.Parameters.AddWithValue("@t0", i + 1);
cmd.Parameters.AddWithValue("@t1", ds.Tables[0].Rows[i]["线路名称"].ToString());
cmd.Parameters.AddWithValue("@t2", ds.Tables[0].Rows[i]["户名"].ToString());
cmd.Parameters.AddWithValue("@t3", ds.Tables[0].Rows[i]["容量"].ToString());
cmd.Parameters.AddWithValue("@t4", ds.Tables[0].Rows[i]["配变型式"].ToString());
cmd.Parameters.AddWithValue("@t5", ds.Tables[0].Rows[i]["idKey"].ToString());
cmd.Parameters.AddWithValue("@t6", ds.Tables[0].Rows[i]["年份"].ToString());
cmd.Parameters.AddWithValue("@t7", ds.Tables[0].Rows[i]["月份"].ToString());
cmd.Parameters.AddWithValue("@t8", ds.Tables[0].Rows[i]["实际电量"].ToString());
cmd.ExecuteNonQuery();
}
}
//运行到这里Guid.NewGuid().ToString().xls这个EXCEL文件就被
//WebDeV.Webserver.exe占用 有时候释放的早,有时候释放的晚,施放的晚的话,就会导致
//File.ReadAllBytes(Guid.NewGuid().ToString().xls)
//这个excel被进程还用着呢 当然会出错了哦)
//假如这时 把 占用 Guid.NewGuid().ToString().xls的进程给结束掉 然后再 readallbytes 就好了
//但是本人愚昧 希望高手帮忙啊
catch(System.Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
//this.KillProcess("EXCEL.EXE");
//输出副本的二进制字节流
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=superengine.xls");
/* tww add at 20100506
我认为之所以出现进程占用的的错误,是因为当excel
写入的内容较多时,操作系统(WINDOWS2000)关闭excel
进程时间要长一些,而.NET下载代码运行很快,
马上会要求读取excel 的文档,这时操作系统尚未处理完excel 的进程,
因此excel 文档处于收保护状态,因此造成.NET进程和excel 进程的冲突。
*/
this.killProcess();
System.Threading.Thread.Sleep(3000);//为了延缓
byte[] tww = File.ReadAllBytes(。。。));
Response.BinaryWrite(tww);
// 删除副本
try
{
File.Delete(filePath);
}
catch(System.IO.IOException ex)
{
}
finally
{
File.Delete(Server.MapPath("~/Report_1/doc/tww123.xls"));
}
Response.End();