将报表导入到EXCEL中,出现问题,在客户机上运行代码时,却在服务器上启动了EXCEL,而没有客户机上启动EXCEL。以前是服务器上装了.net2003没有出现问题,现在是新装了服务器,只装了.net的23M的运行包,就出现了问题。代码发下:
//将模板文件复制到一个新文件中
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.   

    给你段代码:
    //导入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();
    }
      

  2.   

    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(); 按照这个做你就明白了
      

  3.   

    应该不是代码的问题吧,是不是服务器上设置DCOM有问题
      

  4.   

    在客户端打开这个excel得用response输出,MIME类型为“application/vnd.ms-excel”