Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Exporting DataGrid to Excel

Exporting DataGrid to Excel

Scheduled Pinned Locked Moved C#
helpcomdata-structurestestingdebugging
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pssuresh
    wrote on last edited by
    #1

    Hello Friends, I use this link to export datagrid values to excel sheet. http://support.microsoft.com/kb/306023/EN-US/ and iam following this topic Use Automation to Transfer an Array of Data to a Range on a Worksheet The problem is , if the no of characters are more in the datagrid cell and if i make transfer to the excel sheet, then it throws error.When i debug , i found out if the characters in a datagrid cell exceed 910, then it throws error. If any of you, have faced a problem like this before,please share your thoughts. Suresh.P.S

    S 1 Reply Last reply
    0
    • P pssuresh

      Hello Friends, I use this link to export datagrid values to excel sheet. http://support.microsoft.com/kb/306023/EN-US/ and iam following this topic Use Automation to Transfer an Array of Data to a Range on a Worksheet The problem is , if the no of characters are more in the datagrid cell and if i make transfer to the excel sheet, then it throws error.When i debug , i found out if the characters in a datagrid cell exceed 910, then it throws error. If any of you, have faced a problem like this before,please share your thoughts. Suresh.P.S

      S Offline
      S Offline
      Sandeep Akhare
      wrote on last edited by
      #2

      HI Are you Editing the datagrid ? What throwing the Exception datagrid or Excel sheet ?

      Thanks and Regards Sandeep

      P 1 Reply Last reply
      0
      • S Sandeep Akhare

        HI Are you Editing the datagrid ? What throwing the Exception datagrid or Excel sheet ?

        Thanks and Regards Sandeep

        P Offline
        P Offline
        pssuresh
        wrote on last edited by
        #3

        Hi, I am not editing the datagrid. I am trying to export to excel.I am very sure, it is because of the no of characters presented in a cell. The exception is COM Exception "Exception from HRESULT: 0x800A03EC." Suresh.P.S

        S 1 Reply Last reply
        0
        • P pssuresh

          Hi, I am not editing the datagrid. I am trying to export to excel.I am very sure, it is because of the no of characters presented in a cell. The exception is COM Exception "Exception from HRESULT: 0x800A03EC." Suresh.P.S

          S Offline
          S Offline
          Sandeep Akhare
          wrote on last edited by
          #4

          NO i don't think Suresh its because of it i just add near about 1000 char in one cell and also saved it succesfully ! Again you can you try again for some small data then go for bigger one can you post the code ? so that i can see what you have done I also had COM error that time it was due to Security issue :) Its very difficult resovle COM error as errors are in number no description :)

          Thanks and Regards Sandeep

          P 1 Reply Last reply
          0
          • S Sandeep Akhare

            NO i don't think Suresh its because of it i just add near about 1000 char in one cell and also saved it succesfully ! Again you can you try again for some small data then go for bigger one can you post the code ? so that i can see what you have done I also had COM error that time it was due to Security issue :) Its very difficult resovle COM error as errors are in number no description :)

            Thanks and Regards Sandeep

            P Offline
            P Offline
            pssuresh
            wrote on last edited by
            #5

            Hi, use this link.i made my code from this link. http://support.microsoft.com/kb/306023/EN-US/ and the following is my code This is the windows application.when i truncate the data, it saves successfully, but when i increase the limit , then it throws the error. // Start a new workbook in Excel. excel = new Excel.Application(); books = (Excel.Workbooks)excel.Workbooks; book = (Excel._Workbook)(books.Add(optionalValue)); sheets = (Excel.Sheets)book.Worksheets; sheet = (Excel._Worksheet)(sheets.get_Item(1)); System.Data.DataView customizeGridView=(System.Data.DataView)CustomizeExportDataGrid.DataSource; // Create an array for the headers and add it to cells A1:C1. object[] headers=new object[customizeGridView.Table.Columns.Count]; //Read the column names from the view and add it as a column headers to excel sheet. if(customizeGridView!=null) { for(int i=0;i < customizeGridView.Table.Columns.Count;i++) { headers[i]=customizeGridView.Table.Columns[i].ColumnName; } } range = sheet.get_Range("A1", optionalValue); if(customizeGridView.Table.Columns.Count > 0) range = range.get_Resize(1,customizeGridView.Table.Columns.Count); range.Value = headers; range.EntireColumn.AutoFit(); font = range.Font; font.Bold=true; if(customizeGridView!=null) { // Create an array with 3 columns and 100 rowxs and add it to // the worksheet starting at cell A2. int rowcount=customizeGridView.Table.Rows.Count; int ColumnCount=customizeGridView.Table.Columns.Count; object[,] data = new Object[rowcount,ColumnCount]; int rowLoop=0; int ColumnLoop=0; //First the object values are copied and they are saved in to excel as a stretch to increase performance. for(rowLoop=0;rowLoop <= rowcount-1;rowLoop++) { for(ColumnLoop=0;ColumnLoop <= ColumnCount-1;ColumnLoop++) { if(CustomizeExportDataGrid[rowLoop,ColumnLoop]==System.DBNull.Value) data[rowLoop,ColumnLoop]=string.Empty; else { data[rowLoop,ColumnLoop] = CustomizeExportDataGrid[rowLoop,ColumnLoop]; } } } range = sheet.get_Range("A2", optionalValue); range = range.get_Resize(rowcount,ColumnCount); range.Value = data; excel.DisplayAlerts=false; book.SaveAs(filename, optionalValue, optionalValue, optionalValue, optionalValue, optionalValue, Excel.XlSaveAsAccessMode.xlNoChange, optionalValue, optionalValue, optionalValue, optionalValue); Thanks, Suresh.P.S -- modified at 10:08 Thursday 8th February, 2007

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups