如何在asp.net中将报表输出到excel中(不用客户端代码, 不使用控件传递?  
以前我问过,请“浪人“再帮一次忙。
请大家多帮忙

解决方案 »

  1.   

    Private Sub CreateExcelWorkbook()
            Dim missing As Object = System.Reflection.Missing.Value
            Dim strCurrentDir As String = Server.MapPath(".") + "\"
            Dim strFile As String = "割当エラー" + System.DateTime.Now.Ticks.ToString() + ".xls"
            Dim AppFoldPath As String = strCurrentDir & "割当エラー"
            Dim oXL As Excel.Application
            Dim oWB As Excel._Workbook
            Dim oSheet As Excel._Worksheet
            Dim oRng As Excel.Range
            Dim Rows As Integer
            Dim Cols As Integer
            Dim i As Integer
            Try
                '//////'ExcelApplicationを形成する(必要だサーバー最高の権限Web.Config---> 
                '//////<identity impersonate="true" userName="adminuser" password="adminpass"/> 
                System.GC.Collect()
                oXL = New Excel.Application
                oXL.Visible = False
                oWB = CType((oXL.Workbooks.Add(missing.Value)), Excel._Workbook)
                oSheet = CType(oWB.ActiveSheet, Excel._Worksheet)
                oSheet.Activate()
                oSheet.Range("A1").Value = "記入日"
                oSheet.Range("B1").Value = "郵便番号"
                oSheet.Range("C1").Value = "県名"
                oSheet.Range("D1").Value = "地区名"
                oSheet.Range("E1").Value = "媒体名"
                oSheet.Range("F1").Value = "受付番号"
                oSheet.Range("G1").Value = "サービス"
                For Rows = 0 To Cart.Rows.Count - 1
                    oSheet.Range("A" & (i + 2)).Value = NulltoStr(Cart.Rows(Rows).Item(0))
                    oSheet.Range("B" & (i + 2)).Value = NulltoStr(Cart.Rows(Rows).Item(1))
                    oSheet.Range("C" & (i + 2)).Value = NulltoStr(Cart.Rows(Rows).Item(2))
                    oSheet.Range("D" & (i + 2)).Value = NulltoStr(Cart.Rows(Rows).Item(3))
                    oSheet.Range("E" & (i + 2)).Value = NulltoStr(Cart.Rows(Rows).Item(4))
                    oSheet.Range("F" & (i + 2)).Value = NulltoStr(Cart.Rows(Rows).Item(5))
                    oSheet.Range("E" & (i + 2)).Value = NulltoStr(Cart.Rows(Rows).Item(6))
                    i = i + 1
                Next
                MakeAngelDir(AppFoldPath)
                oWB.SaveAs(strCurrentDir + "割当エラー\" & strFile, Excel.XlFileFormat.xlWorkbookNormal, Nothing, Nothing, False, False, Excel.XlSaveAsAccessMode.xlShared, False, False, Nothing, Nothing)
                Dim AppPath As String = strCurrentDir + "割当エラー\" & strFile
                oXL.UserControl = False
          
                oXL.Visible = False
                oWB.Close()
                oXL.Workbooks.Close()
                oXL.Quit()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oRng)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL)
                oSheet = Nothing
                oWB = Nothing
                oXL = Nothing
           
                System.GC.Collect()
            Catch theException As Exception
                Response.Write(theException.Message)
                Response.End() 
                Dim errorMessage As String
                errorMessage = "Error: "
                errorMessage = String.Concat(errorMessage, theException.Message)
                errorMessage = String.Concat(errorMessage, " Line: ")
                errorMessage = String.Concat(errorMessage, theException.Source)
                Page.RegisterStartupScript("focus", "<script language=javascript>alert(""" & errorMessage & """)</script>")
            End Try
        End Sub
      

  2.   

    执行oXL = New Excel.Application时
    提示“ ASP.NET访问未授权的资源。
    请问如何解决。
      

  3.   

    假设你在用Windows Server 2003 English Edition(抱歉我没有装中文版),进入“Component Services”后,找到“Root\Component Services\Computers\My Computer\DCOM Config”,找到“Microsoft Excel Application”,右击选择“properties”,在对话框中选择“Security”,在“Launch Permissions”中选定“Customize”,然后点击“Edit...”按钮,在新对话框中点“Add”,输入“NETWORK SERVICE”(如果是Windows 2000/XP,则输入“ASPNET”),确定(注意要选择本机的NETWORK SERVICE帐号,如果你加入了域,会去域中搜索这个帐号,结果就会找不到)。确定后可以看到帐号出现在列表中,选择这个帐号,确保下面的Launch Permission被选中。完成后在Access Permissions中做同样的事情。这样就不会出现“ASP.NET访问未授权的资源”的错误了
      

  4.   

    http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp
      

  5.   

    请问
    该方法在服务端是否需要安装office
      

  6.   

    webdiyer转贴的文章好,推荐看!