我从网上找了许多操作EXCEL的例子,照着做都不能成功,下面是其中一个例子,
加入COM组件,也写了using Excel后Excel.Application app = new Excel.ApplicationClass();
Excel.Workbooks wbs = app.Workbooks;
Excel.Workbook wb = wbs.Add(System.Reflection.Missing.Value);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
app.Cells[2,2] = "abcd" ;  
app.Visible = true;   //执行此句后EXCEL程序没有显示出来
ws.PrintPreview(0) ;  //此句执行不了,网页的进度条走了一点就不往前走了单步执行到最后一条语句时ws.PrintPreview(0) ; 就不行了,网页的进度条中止,看
内存中有一个EXCEL进程,但EXCEL出不来。
---------------------------------------------------------------------------
下面是VB6时代操作EXCEL,往模板a.xls中写数据,启动预览,然后不保存关闭退出的例子Dim myExcel As Excel.Application
Dim x1book As Excel.Workbook
Dim x1sheet As Excel.Worksheet
Set myExcel = CreateObject("Excel.Application")
Set x1book = myExcel.Workbooks.Open(App.Path + "\a.xls")
Set x1sheet = x1book.Worksheets(1)x1sheet.Cells(1,1) = "abcdefghijk"'启动预览,用户点关闭后不提示保存直接退出。
myExcel.Visible = True
myExcel.Worksheets.PrintPreview
myExcel.AlertBeforeOverwriting = False
myExcel.DisplayAlerts = False
Set x1book = Nothing
myExcel.Application.Quit
'****************************
如今学ASP.NET,怎么用C#实现同样功能?

解决方案 »

  1.   

    你可以dataset 导入 excel参考
    http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
    http://www.dev-club.com/club/bbs/showEssence.asp?id=26350http://dev.csdn.net/Develop/article/18/18623.shtm
    http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
    http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
    http://expert.csdn.net/Expert/TopicView1.asp?id=2928057www.foxhis.com/powermjtest/
    原文代码:
    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); //下载文件
    }
      

  2.   

    /// <summary>
      /// 导出数据到一个excel文件
      /// </summary>
      /// <returns>文件名称</returns>
      public string ExportToExcel()
      {
       //You use these variables throughout the application.
       string fileExcel;
       string fileName;
       string strLine;
       FileStream objFileStream;
       StreamWriter objStreamWriter;
       Random nRandom = new Random(DateTime.Now.Millisecond);
       //Create a random file name.
       fileExcel = "t" + nRandom.Next().ToString() + ".xls";                    
       //Set a virtual folder to save the file.
       fileName = filePath + "\\" + fileExcel;
       //Use FileSystem objects to create the .xls file.
       objFileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);          
       objStreamWriter = new StreamWriter(objFileStream,System.Text.Encoding.Default); 
       objStreamWriter.WriteLine(fileHeader);
       //Reinitialize the string for data.
       strLine = "";
       //Enumerate the database that is used to populate the file.
       for (int j=0;j<=DataDt.Rows.Count-1;j++) 
       {
        for (int i = 0; i <=DataDt.Columns.Count-1; i++) 
        {
         strLine = strLine +DataDt.Rows[j][i].ToString()+Convert.ToChar(9);
        }                    
        objStreamWriter.WriteLine(strLine);
        strLine="";
       }
       //Clean up.
       objStreamWriter.Close();
       objFileStream.Close();
       return filePath + "\\" + fileExcel;
      //return fileExcel;
      }
      

  3.   

    二楼的例子我试过了,EXCEL也出不来啊
      

  4.   

    即使换装了EXCEL 2003也不行,为什么啊?