using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO ; using System.Reflection ; using System.Runtime.InteropServices; using System.Runtime .InteropServices .Expando ; using Excel;namespace WebApplication3 { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1;
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO ; using System.Reflection ; using System.Runtime.InteropServices; using System.Runtime .InteropServices .Expando ; using Excel;namespace WebApplication3 { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1;
<identity impersonate="true"/>
FileInfo mode=new FileInfo(Server.MapPath(".")+"\\bin\\normal.xls");
mode.CopyTo(filename);
Excel.Application myExcel=new Excel.Application ( );
myExcel.Visible =false;
myExcel.Cells[5,5]="'"+"123";
我上边的这个代码每次执行第一次后就提示excel不可再用了,怎么办?》??????
//string filename="ac.xls";
FileInfo mode=new FileInfo(Server.MapPath(".")+"\\bin\\normal.xls");
mode.CopyTo(filename);
Excel.Application myExcel=new Excel.Application ( );
object missing=Missing.Value;
myExcel.Application.Workbooks.Open(filename,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
myExcel.Visible =false;
myExcel.Cells[5,5]="'"+"123";
这是我写的完整代码
if (m_xlApp != null){
m_xlApp.ActiveWorkbook.Close(false,missing,missing);
}
Excel.Workbooks workbooks = null;
Excel.Workbook workbook = null;
Excel.Sheets sheets = null;
Excel.Worksheet worksheet = null;excel = new Excel.ApplicationClass();
workbooks = excel.Workbooks;
workbook = workbooks.Open(FileName, System.Type.Missing,<参数......>);
sheets = workbook.Sheets;
worksheet = (Excel.Worksheet)sheets[1];
Application对象是没有Cells的
请看清层次关系用System.Runtime.InteropServices.Marshal.ReleaseComObject释放掉不用的COM对象
删除文件
然后把缓存数据送到客户端
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO ;
using System.Reflection ;
using System.Runtime.InteropServices;
using System.Runtime .InteropServices .Expando ;
using Excel;namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
string filename=Server.MapPath(".")+"\\bin\\af.xls";
//string filename="ac.xls";
FileInfo sTemplate=new FileInfo(Server.MapPath(".")+"\\bin\\normal.xls");
sTemplate.CopyTo(filename,true);
object missing=Missing.Value;
Excel.Application excel = null;
Excel.Workbooks workbooks = null;
Excel.Workbook workbook = null;
Excel.Sheets sheets = null;
Excel.Worksheet worksheet = null;
Excel.Range oCells=null;
excel = new Excel.ApplicationClass(); excel.Visible = false;
excel.DisplayAlerts = false; workbooks = excel.Workbooks;
workbook = workbooks.Open(filename,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
sheets = workbook.Worksheets;
worksheet = (Excel.Worksheet)sheets[1];
worksheet.Name = "First Sheet";
oCells = worksheet.Cells; //DumpData(dt, oCells); worksheet.Cells[5,5]="'"+"99999999";
workbook.Save ();
//worksheet.SaveAs(filename);
// workbook.Close(); //Quit Excel and thoroughly deallocate everything
excel.Quit();
System.Runtime.InteropServices.Marshal .ReleaseComObject(oCells) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal .ReleaseComObject(sheets) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal .ReleaseComObject(workbooks) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(excel);
excel = null ; workbooks = null ; workbook = null;
sheets = null ; worksheet = null ; oCells = null;
System.GC.Collect(); Response.Redirect(filename);
} }
}我的这个代码在asp.net中执行,因为excel进程处理不了,下载功能就实现不了,帮忙啊,谢谢了!!!!!!!!!!!!!!!!!!!!!!公司赶任务啊!!!!!!!!
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO ;
using System.Reflection ;
using System.Runtime.InteropServices;
using System.Runtime .InteropServices .Expando ;
using Excel;namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
string filename=Server.MapPath(".")+"\\bin\\af.xls";
//string filename="ac.xls";
FileInfo sTemplate=new FileInfo(Server.MapPath(".")+"\\bin\\normal.xls");
sTemplate.CopyTo(filename,true);
object missing=Missing.Value;
Excel.Application excel = null;
Excel.Workbooks workbooks = null;
Excel.Workbook workbook = null;
Excel.Sheets sheets = null;
Excel.Worksheet worksheet = null;
Excel.Range oCells=null;
excel = new Excel.ApplicationClass(); excel.Visible = false;
excel.DisplayAlerts = false; workbooks = excel.Workbooks;
workbook = workbooks.Open(filename,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
sheets = workbook.Worksheets;
worksheet = (Excel.Worksheet)sheets[1];
worksheet.Name = "First Sheet";
oCells = worksheet.Cells; //DumpData(dt, oCells); worksheet.Cells[5,5]="'"+"99999999";
workbook.Save ();
//worksheet.SaveAs(filename);
// workbook.Close(); //Quit Excel and thoroughly deallocate everything
excel.Quit();
System.Runtime.InteropServices.Marshal .ReleaseComObject(oCells) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal .ReleaseComObject(sheets) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal .ReleaseComObject(workbooks) ;
System.Runtime.InteropServices.Marshal .ReleaseComObject(excel);
excel = null ; workbooks = null ; workbook = null;
sheets = null ; worksheet = null ; oCells = null;
System.GC.Collect(); Response.Redirect(filename);
} }
}我的这个代码在asp.net中执行,因为excel进程处理不了,下载功能就实现不了,帮忙啊,谢谢了!!!!!!!!!!!!!!!!!!!!!!公司赶任务啊!!!!!!!!