原文代码:
private void Button1_Click(object sender, System.EventArgs e)
{
  //写入Excel的方法:
  //定义需要参数。
  string SourceFile="Data.XLS";                                //源文件名称。
  string TemplatePath=Server.MapPath("ExcelTemplate");    //存放源文件的文件夹路径。
  string DownloadPath=Server.MapPath("ExcelDownload");    //副本的文件夹路径。
  //副本的文件名。
  string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";  
  object missing = System.Reflection.Missing.Value;
  Excel.Application myExcel=new Excel.Application();
  //打开新文件
  myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing); 
  Excel.Workbook myBook=myExcel.Workbooks[1];
  Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];

  string DownloadFilePath=DownloadPath+"\\"+TempFileName;

  int i=0;
  while (i<=10)
  {
    myExcel.Cells[4+i,2]=i.ToString();
    myExcel.Cells[4+i,3]=i.ToString();
    myExcel.Cells[4+i,4]=i.ToString();
    myExcel.Cells[4+i,5]=i.ToString();
    myExcel.Cells[4+i,6]=i.ToString();
    i++;
  }   

  myBook.Saved=true;
  //myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);

  myBook.PrintPreview(0);
  //myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
  myBook.Close(false, null,null);
  myExcel.Quit();
  System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
  System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
  myBook = null;
  myExcel = null;
  GC.Collect();
  //Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}

解决方案 »

  1.   

    我又要绝望了,Csdn上竟然没人解决这个问题吗?
      

  2.   

    myBook.PrintPreview(0);
      //myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
      myBook.Close(false, null,null);
    这一句发生在服务端,这是基本概念。
    简单可行的方法是:myBook的onOpen()事件中加个预览的vba,再myBook.Save(.FileName....),然后将这个文件发送到客户端。客户端选择打开。
      

  3.   

    shangwg(shangwg) 说的详细一点好吗?
      

  4.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
      

  5.   

    Dim SourceFile As String = "Data.XLS"
            Dim TemplatePath As String = Server.MapPath("ExcelTemplate")
            Dim DownloadPath As String = Server.MapPath("ExcelDownload")        'Dim TempFileName As String = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS"
            Dim missing As Object = System.Reflection.Missing.Value
            Dim myExcel As Excel.Application = New Excel.Application        '      myExcel.Application.Workbooks.Open(TemplatePath + "\\" + SourceFile, missing, missing, missing, missing, _
            'missing, missing, missing, missing, missing, missing, missing, missing)
            myExcel.Application.Workbooks.Open(TemplatePath + "\\" + SourceFile)
            Dim myBook As Excel.Workbook = myExcel.Workbooks(1)        Dim curSheet As Excel.Worksheet = CType(myBook.Sheets(2), Excel.Worksheet)        'Dim DownloadFilePath As String = DownloadPath + "\\" + TempFileName        Dim i As Integer = 0
            While (i <= 10)            myExcel.Cells(4 + i, 2) = i.ToString()
                myExcel.Cells(4 + i, 3) = i.ToString()
                myExcel.Cells(4 + i, 4) = i.ToString()
                myExcel.Cells(4 + i, 5) = i.ToString()
                myExcel.Cells(4 + i, 6) = i.ToString()
                i = i + 1
            End While        myBook.Saved = True
            'myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);        myBook.PrintPreview(0)
            'myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
            myBook.Close(False, Nothing, Nothing)
            myExcel.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel)
            myBook = Nothing
            myExcel = Nothing
            GC.Collect()我式了一下Access is denied. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.UnauthorizedAccessException: Access is denied. 
      

  6.   

    你遇到过吗
    我的目录权限都是EVeryone
      

  7.   

    这个我知道了 加个<identity impersonate="true"/> 就行了 我接着式
      

  8.   

    to: "作  者:  whblxl (流行雨)  信 誉 值:  83  "别人敢回答吗, 你回答了,不结帖, 咋办呢?
      

  9.   

    xhunanpp(囨囚囨図) 我不是不想结贴,你可以看看我发的贴子,
         得到满意答案的贴自我全都结了,没得到答案的贴子都没结,我觉得没必要,没得到答案为什末要给分呢?你的信誉分不是也少了吗?
      

  10.   


    以下代码是用EXCEL导出数据 
    1. 文件生成当前调用的页面的文件夹目录下,* 你把得把你当前页面的文件夹的权限改成可修改, 不然会出现脚本出错
    2. 在服务端生成.XLS后,调用一个window.open()方法,直接打开当前.XLS文件
    3. 调用方法: 
       直接调用 BindExcels(DataSet ds, String str, System.Web.UI.Page page)方法
       里面有3个参数, 
       ds 是当前你页面的DataSet 对象
       str 是一个字段串数组,也就是DataSet 的例名, 如果你的 DataSet有4个列,你就得写成 String[] str = {"列1","列2","列3","列4"}; 
       page 是你当前页的Page对象注: 这些方法,有一个却点, 就是数据量大时,很慢,这也是用EXCLE做报表的却点  ----------------------------------------
    ---- .CS
    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.Web.Security;
    using OWC;
    using System.IO;namespace EF.School.WEB.include
    { public class GridToExcel: System.Web.UI.UserControl
    {
    public GridToExcel()
    {} private string sFilename;
    private string sDir;
    private bool CreateFile(System.Web.UI.Page page)
    {
    sDir=page.Server.MapPath(".")+"\\";
    sFilename=page.Request.ServerVariables["REMOTE_ADDR"].Replace(".","")+".xls";
    if(System.IO.File.Exists(sDir+sFilename))
    {
    System.IO.File.Delete(sDir+sFilename);
    }
    return true;
    } private bool ExportDataSet2Excel(DataSet dsGrid, String sHeadnames,System.Web.UI.Page page)
    {
    try
    {
    SpreadsheetClass xlsheet = new SpreadsheetClass();
    DataTable dtGrid=dsGrid.Tables[0];
    int iRow=1;
    int iCol=0; //输出标题
    string [] sHeadname=sHeadnames.Split();
    for(int i=0;i<sHeadname.Length;i++)
    {
    xlsheet.ActiveSheet.Cells[1,i+1]=sHeadname[i];
    //设置格式
    //xlsheet.get_Range(xlsheet.Cells[1,1],xlsheet.Cells[1,i+1]).Font.Bold =true;
    //xlsheet.get_Range(xlsheet.Cells[1,1],xlsheet.Cells[1,i+1]).Font.Color="red";
    } foreach(DataRow row in dtGrid.Rows)
    {
    iRow++;
    iCol=0;
    foreach(DataColumn col in dtGrid.Columns)
    {
    iCol++;
    xlsheet.ActiveSheet.Cells[iRow,iCol]=row[col.ColumnName].ToString();
    }
    }

    if(CreateFile(page))
    {
    // sFilename=@"c:/inetpub/wwwroot/EF.School/Web/AppraiseModule/StudentInfoInOrg/Excel/11.xls";
    xlsheet.ActiveSheet.Export(sDir+sFilename,OWC.SheetExportActionEnum.ssExportActionNone); return true;
    }
    else
    {
    return false;
    }

    }
    catch
    {
    return false;
    }
    } public void BindExcels(DataSet ds, String str, System.Web.UI.Page page)
    {
    if(!ExportDataSet2Excel(ds,str,page))
    {
    PageBase error = new PageBase();
    error.Alert("导出失败",page);
    }
    else
    {
    PageBase script = new PageBase();
    script.ShowScript("window.open('"+sFilename+"','','Height=500,Width=700,Top=100,Left=100,edge=1,center=1,resizable=1,status=0,toolbar=1,menubar=1');",page);
    }
    }
    }
    }
      

  11.   

    经过n天的搜索试验学习,最后决定用水晶报表,在这里谢谢xhunanpp(囨囚囨図),meixiaofeng(sean) ,goody9807() 三位老兄的在线技术支持,尤其是meixiaofeng(sean)
    提供软件,再次深表谢意!