在.net中将数据导入到EXCEL中,在客户机上运行程序时,客户机不能打开EXCEL,服务器上却启动了EXCEL。以前测试时,在服务器上装有.net的程序,代码运行正常没有出现问题,但重新装服务器后,只在服务器上安装了从微软网站上下的.net SDK,就出现了问题,代码如下:
//将模板文件复制到一个新文件中
string filename;
filename=Server.MapPath("\\web_file\\xls\\") +System.DateTime.Now.Ticks.ToString() + ".xls";
//将模板文件copy到新位置,建议实际开发时用相对路径,如Application.StartupPath.Trim()+"\\report\\normal.xls"
string filenameold=Server.MapPath("\\web_file\\统计表1模板.xlt");
File.Copy(filenameold,filename);

//打开复制后的文件
Excel.Application myExcel=new Excel.Application ( );
object missing=System.Reflection.Missing.Value;
//Excel.Workbook myBook;
myExcel.Visible=true;
//打开新文件
myExcel.Application.Workbooks.Open(filename,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing); 

//逐行写入数据,数组中前五行列标题,忽略,EXCEl中的行列号是从1开始
int row=6,DGRow;
Excel_num=0;

myExcel.Cells[row++,1]="'一、";
DGRow=DataGrid1.Items.Count;
if (DGRow>0)
{
DGtoExcelSheet(DataGrid1,myExcel,DGRow,row,"Label5");
row=row+DGRow;
}

//myBook.Saved=true;
//myExcel.UserControl = false;
//myBook.Save();
//myExcel.Quit();
//missing=null;
//myBook=null;
//myExcel=null;
GC.Collect(); RemoveFiles(Server.MapPath("\\web_file\\xls\\"));
}各位大虾帮帮忙了

解决方案 »

  1.   

    Response.Clear();
    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.Buffer= true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";
    this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    DataGrid1.RenderControl(oHtmlTextWriter);
    Response.AppendHeader("Content-Disposition", "attachment;filename= "+ Title + ".xls");
    Response.Write(oStringWriter.ToString());
    Response.End();
      

  2.   

    >>在.net中将数据导入到EXCEL中,在客户机上运行程序时,客户机不能打开EXCEL,服务器上却启动了EXCEL。
    服务器启动excel是正常的。>>以前测试时,在服务器上装有.net的程序,代码运行正常没有出现问题,但重新装服务器后,只在服务器上安装了从微软网站上下的.net SDK,就出现了问题,什么问题?有没有安装office?>>代码如下:
    ...
    GC.Collect();
    ...excel的gc有bug,完整的写法如下:
    GC.Collect();
    System.Diagnostics.Process[] myProcesses ;
    myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
    foreach(System.Diagnostics.Process myProcess in myProcesses)
    {
         myProcess.Kill();
    }
    >>...
    Response.ContentType = "application/vnd.ms-excel";
    ...
    DataGrid1.RenderControl(oHtmlTextWriter);
    ...你有没有重载RenderControl这个函数?
      

  3.   

    楼主的问题我觉得不太好解决,服务器端代码如果权限够的话启动服务器程序应该问题不大,但是启动客户端excel,那需要客户端权限的开放的。楼上的这种直接将网页输出成excel的方法肯定不能满足楼主的要求,但是楼主何必这么麻烦呢?为什么不直接recirect 那个excel文件。不是简单。
      

  4.   

    怎么看不懂呢,说详细点吧,我想是不是服务器配置上有问题,因为这个在另外一个装了.net的WEB服务器上都能用,只是换了一个没有装.net ,只装了.net运行包的WEB服务器上才出现这个问题的,而且问题是客户机上没有启动EXCEL程序,在服务器上启动了EXCEL程序
      

  5.   

    给你段代码:
    //导入excel中
    private void excel_btn_Click(object sender, System.EventArgs e)
    {
    Response.Clear(); 
    Response.Buffer= true; 
    Response.Charset="GB2312";
    string str =star_text.Text.Trim()+"--"+end_text.Text.Trim(); //设置文件名
    Response.AppendHeader("Content-Disposition","attachment;filename="+str+".xls"); 
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
    Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
    this.EnableViewState = false;    
    System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    MyDataGrid.RenderControl(oHtmlTextWriter); 
    Response.Write(oStringWriter.ToString());
    Response.End();
    }
      

  6.   

    using System.Data.OleDb;string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Server.MapPath("UpLoadFile/book1.xls") + ";Extended Properties=Excel 8.0";  
    OleDbConnection conn = new OleDbConnection(strConn); 
    OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [Sheet1$]",conn); 
    DataSet ds = new DataSet(); 
    adp.Fill(ds,"Book1"); 
    ExlDataGrid.DataSource = ds.Tables["Book1"].DefaultView;
    ExlDataGrid.DataBind(); 按照这个做你就明白了
      

  7.   

    我要利用EXCEL的模板,在程序中往EXCEL填入数据,要控制写入的单元格,再在客户机上打开该EXCEL文件,让用户可以编辑。
      

  8.   

    没这样用过。帮你定一下。
    不过你考虑一下用xmlhttp可以否?