Export to Excel 2007 (xlsx) format RRS feed

  • Question

  • Hi All,


    I need to export the Reports from Report Viewer to Excel 2007 (xlsx) format.


    Can anyone please provide some inputs on this?


    Thanks & Regards


    Wednesday, May 23, 2007 12:45 PM

All replies

  • Can anyone take this query please?
    Tuesday, June 5, 2007 12:50 PM
  • Are you asking how to save in Excel 2007 (xlsx) format, or are you asking how to save from Report Viewer?
    Wednesday, July 30, 2008 4:23 PM
  • I'm assuming you want to know how to export into excel 2007 format

    First you have to add the Microsoft Excel X.0 Object Library COM reference, where X is the version number.  Visual studio 2008 comes with version 12.  To do this go to Project -- > Add reference in the menu.  A window will pop up.  Select the 'COM' tab, then double click 'Microsoft Excel X.0 Object Library'.

    In order to use this in your project you have to type:

    Imports Excel = Microsoft.Office.Interop.Excel

    Then you have to make a new object instance of Excel, which I would do as such:

    Dim objExcel As New Microsoft.Office.Interop.Excel.Application()

    Then when you want to save the report you can set your sheet up as follows:

    objBook = objExcel.Workbooks.Add()
    objSheet = objBook.Worksheets(1)

    with objSheet
        objSheet.Range("A1").Value = someText
    ' Saves a value to the cell
        objSheet.Range("A1", "B6").Merge
    ' Merges cells from A1 - B6
        objSheet.Range("A1", "B6").BorderAround(1) ' Puts a border around the cells from A1 - B6
        objSheet.Range("A1").HorizontalAlignment = Excel.Constants.xlCenter ' Center's the cell horizontally
        . etc
        objSheet.Visible = True ' Shows the completed sheet if you want it shown
    end with

    objBook.SaveAs(filename) ' Will save the file in xlsx format.  I think version 11.0 and 12.0 of the Excel COM
                             ' reference will save in xlsx format.  I'm not positive about this though.

    Anyway, I hope this is what you were looking for.  The above example is just dealing with a few cells, but you can manipulate all the cells the same way.
    Wednesday, July 30, 2008 4:45 PM