string newpath = Server.MapPath(".") + @"\Excel\" + Guid.NewGuid() + ".xls";
            FileInfo mode = new FileInfo(Server.MapPath(".") + @"\Excel\详细资料.xls");
            Excel.Application app = new Excel.Application();
            app.Application.DisplayAlerts = false;
            app.Visible = false;
            if (mode.Exists)
            {
                Excel.Workbook tworkbook;
                Object missing = System.Reflection.Missing.Value;
                app.Workbooks.Add(missing);
                tworkbook = app.Workbooks.Open(mode.FullName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                for (int i = 0; i < this.GridView1.Rows.Count; i++)
                {
                    Excel.Worksheet tworksheet = (Excel.Worksheet)tworkbook.Sheets[1];
                    tworksheet.Cells[3, 2] = this.GridView1.Rows[i].Cells[0].Text;
                    tworksheet.Cells[3, 7] = Convert.ToDateTime(this.GridView1.Rows[i].Cells[1].Text).ToShortDateString();
                    if (this.GridView1.Rows[i].Cells[2].Text == "&nbsp;")
                    {
                        this.GridView1.Rows[i].Cells[2].Text = "";
                    }
                    tworksheet.Cells[4, 2] = this.GridView1.Rows[i].Cells[2].Text;
                    tworksheet.Cells[4, 4] = this.GridView1.Rows[i].Cells[3].Text;
                    tworksheet.Cells[4, 6] = this.GridView1.Rows[i].Cells[4].Text;
                    tworksheet.Cells[5, 2] = this.GridView1.Rows[i].Cells[5].Text;
                    tworksheet.Cells[5, 4] = this.GridView1.Rows[i].Cells[6].Text;
                    tworksheet.Cells[5, 6] = this.GridView1.Rows[i].Cells[7].Text;
                    tworksheet.Cells[6, 2] = this.GridView1.Rows[i].Cells[8].Text;
                    tworksheet.Cells[6, 4] = this.GridView1.Rows[i].Cells[9].Text;
                    tworksheet.Cells[6, 6] = this.GridView1.Rows[i].Cells[10].Text;
                    tworksheet.Cells[6, 8] = this.GridView1.Rows[i].Cells[11].Text;
                    tworksheet.Cells[7, 2] = this.GridView1.Rows[i].Cells[12].Text;
                    tworksheet.Cells[7, 4] = this.GridView1.Rows[i].Cells[13].Text;
                    tworksheet.Cells[8, 2] = this.GridView1.Rows[i].Cells[14].Text;
                    tworksheet.Cells[8, 4] = this.GridView1.Rows[i].Cells[15].Text;
                    tworksheet.Cells[8, 6] = this.GridView1.Rows[i].Cells[16].Text;
                    tworksheet.Cells[8, 8] = this.GridView1.Rows[i].Cells[17].Text;
                    tworksheet.Cells[9, 2] = this.GridView1.Rows[i].Cells[18].Text;
                    tworksheet.Cells[9, 4] = this.GridView1.Rows[i].Cells[19].Text;
                    tworksheet.Cells[9, 6] = this.GridView1.Rows[i].Cells[20].Text;
                    tworksheet.Cells[9, 8] = this.GridView1.Rows[i].Cells[21].Text;
                    tworksheet.Cells[10, 2] = this.GridView1.Rows[i].Cells[22].Text;
                    tworksheet.Cells[10, 4] = this.GridView1.Rows[i].Cells[23].Text;
                    tworksheet.Cells[10, 6] = this.GridView1.Rows[i].Cells[24].Text;
                    tworksheet.Cells[10, 8] = this.GridView1.Rows[i].Cells[25].Text;
                    tworksheet.Cells[11, 2] = this.GridView1.Rows[i].Cells[26].Text;
                    tworksheet.Cells[17, 2] = this.GridView1.Rows[i].Cells[27].Text;
                    tworksheet.SaveAs(newpath, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                }
                tworkbook.Close(false, mode.FullName, missing);
                app.Workbooks.Close();
                app.Quit();
                if (app != null)
                {
                    foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Excel"))
                    {
                        //先判断当前进程是否是excel   
                        if (!p.CloseMainWindow())
                        {
                            p.Kill();
                        }
                    }
                }
                tworkbook = null;
                app = null;
                GC.Collect();
            }
            //打开保存对话框
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.Charset = "UTF-8";
            Response.ContentType = "application/ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(mode.Name));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Length", mode.Length.ToString());
            Response.Charset = "";
            this.EnableViewState = false;
            Response.WriteFile(newpath);
            //删除创建的Excel文件
            FileInfo fileinf = new FileInfo(newpath);
            fileinf.Delete();
            //关闭连接
            Response.Flush();
            Response.End();这段代码里那有问题?为什么发布后打开Excel会说“不能访问“详细资料.xls”,该文件可能是只读的,或者您要访问的位置是只读的,或者文件所在的服务器没有响应”。我的权限都设定好了。我肯定是代码里的问题,请各位高手帮的给解决以下。急~~~~~

解决方案 »

  1.   

    依据你上面的代码,你要打开服务器上的EXCEL文件,权限肯定要有,另外还要考虑服务器是否已经安装EXCEL了。
      

  2.   

    服务器上已经安装Excel并在组件里设定好了。权限也没问题,可是现在从服务器上下载下来以后,打开excel的时候会提示  “不能访问“详细资料.xls”,该文件可能是只读的,或者您要访问的位置是只读的,或者文件所在的服务器没有响应”。 不知道为什么。 我要是单从服务器上下载模板不会出现这种错误。 因此我判断是代码写的有问题,就从打开后到清空资源里那个地方少什么东西或是多了什么
      

  3.   

    实验成功,单用户导出时正常,多用户同时用就不行了,这是个关键的问题,如何让多用户能同时导出。excel共享我设置过了,无效