locked
Working with excel RRS feed

  • Question

  • User24901372 posted

    hi,

    I have a ASP.Net application developed in Visual Studio 2008. The
    application reads and writes to Excel files. To do this thing i am using reference of

    Microsoft.Office.Interop.Excel from .net tab in add reference dialog. It will take three dlls in my

    bin folder.

    Microsoft.Office.Interop.Excel.dll

    Microsoft.Vbe.Interop.dll

    office.dll

    The application works fine with Excel 2007 but does not work with Excel 2000. While running
    with Excel 2000, it gives the following error message:

    "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".


    Is the installation of office 2007 is must?

    If we register these dlls to system, will it work?

    Can it possible to run this program without any office version.

    Any solution to the above problem will be much appreciated.

    Thanks 

    Saturday, January 9, 2010 12:26 AM

All replies

  • User-2119480821 posted

     

    1. How you reading the file.. into a datatable then to DB table?

    2. You can avoid writing to excel.. another option is to generate runtime html tables dynamically

       and rendring it to users as excel file..

    example:

    <table align="center" width="100%" border="0">
      <tr>
        <td align="center"><asp:Button Visible="True" id="btnExport" runat="server" Text="Export"></asp:Button></td>
      </tr>
      <tr>
        <td><asp:Label id="Label1" runat="server"></asp:Label></td>
      </tr>
    </table>
    
    
    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
    Dim sb As New StringBuilder
    sb.Append("<table border='0' cellpadding='0' cellspacing='0'>")
    sb.Append("<tr><td><table border='0' cellpadding='0' cellspacing='0'>")
    sb.Append("<tr><td>TestingTestingTestingTestingTestingTestingTesting<br>TestingTestingTestingTestingTestingTestingTesting<br>")
    sb.Append("TestingTestingTestingTestingTestingTestingTesting<br>TestingTestingTestingTestingTestingTestingTesting<br></td></tr></table></td>")
    sb.Append("<td valign='top'><table border='1' cellpadding='0' cellspacing='0'>")
    sb.Append("<tr><td>Hello1</td></tr><tr><td>Hello2</td></tr></table></td></tr></table>")
    sb.Append("</table>")
    Label1.Text = sb.ToString()
    
      Response.Clear()
      Response.Charset = ""
      Response.ContentEncoding = System.Text.Encoding.UTF8
      Response.Cache.SetCacheability(HttpCacheability.NoCache)
      Response.ContentType = "application/vnd.xls"
      Response.AddHeader("content-disposition", "attachment;filename=" & "test.xls")
    
      Dim sw As New System.IO.StringWriter
      Dim htw As New HtmlTextWriter(sw)
      Label1.RenderControl(htw)
      Response.Write(sw.ToString())
      sw = Nothing
      htw = Nothing
      Response.Flush()
      Response.End()
    End Sub


     

    Saturday, January 9, 2010 1:02 AM
  • User24901372 posted

    i have to sorry to say that i cant use your approch.

    Because i have a excel tamplate with a lot of formulas and lot of sheets(26).

    So we have to read and write only 5 sheets.

    That why i am using the dlls thats i mention above.....

    Saturday, January 9, 2010 1:10 AM
  • User-2119480821 posted

    Then you need to have MSO 2007 installed on server with necessary rights..

     

    Saturday, January 9, 2010 1:27 AM
  • User-792119821 posted

    hear is sample code for export from Grid to XLS and CSV

    sample code is in vb.net and C# both

    and there is ready made DLL for this function

    http://www.codeproject.com/KB/aspnet/ExportClassLibrary.aspx


    and its free and no limit for fix records import/export.

    Saturday, January 9, 2010 2:13 AM
  • User-1392959185 posted

    This is a great article It will help you

    http://www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx 

    Saturday, January 9, 2010 2:21 AM