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. C# - Fill a grid to Excelfile

C# - Fill a grid to Excelfile

Scheduled Pinned Locked Moved C#
questioncsharpcsscomhelp
3 Posts 3 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.
  • M Offline
    M Offline
    Mschauder
    wrote on last edited by
    #1

    Hey, I want to create a .xlsx file based on data from a grid. My problem: I have values like 01234 (starts with zero) exporting them make 1234 X| ... so, how can I export it with right format? :confused: I fill the file with:

                if (Excelpfad == "")
                    return false;
    
                Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Sheets excelSheets = newWorkbook.Worksheets;
                string currentSheet = "Tabelle1";
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get\_Item(currentSheet);
    
                for (int i = 0; i < UsingGrid.Cols; i++)
                {
                    for (int j = 0; j < UsingGrid.Rows; j++)
                    {
                        worksheet.Cells\[j + 1, i + 1\] = Convert.ToString(UsingGrid\[j, i\]);
                    }
                }
                
                // Autosize cols
                for (int iExcel = 0; iExcel < UsingGrid.Cols; iExcel++)
                {
                    ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns\[iExcel + 1, t\]).ColumnWidth = UsingGrid.get\_ColWidth(iExcel) \* 0.13171875;
                }
                
                if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xls")
                    worksheet.SaveAs(Excelpfad, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, t, t, t, t, t, t, t, t);
                else if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xlsx")
                    worksheet.SaveAs(Excelpfad, t, t, t, t, t, t, t, t, t);
    

    and another question: how can I close that file correct? I have:

    newWorkbook.Close(true, n, n);
    excelApp.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);

    but the "EXCEL.EXE" process still run thank you

    L D 2 Replies Last reply
    0
    • M Mschauder

      Hey, I want to create a .xlsx file based on data from a grid. My problem: I have values like 01234 (starts with zero) exporting them make 1234 X| ... so, how can I export it with right format? :confused: I fill the file with:

                  if (Excelpfad == "")
                      return false;
      
                  Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                  Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                  Microsoft.Office.Interop.Excel.Sheets excelSheets = newWorkbook.Worksheets;
                  string currentSheet = "Tabelle1";
                  Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get\_Item(currentSheet);
      
                  for (int i = 0; i < UsingGrid.Cols; i++)
                  {
                      for (int j = 0; j < UsingGrid.Rows; j++)
                      {
                          worksheet.Cells\[j + 1, i + 1\] = Convert.ToString(UsingGrid\[j, i\]);
                      }
                  }
                  
                  // Autosize cols
                  for (int iExcel = 0; iExcel < UsingGrid.Cols; iExcel++)
                  {
                      ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns\[iExcel + 1, t\]).ColumnWidth = UsingGrid.get\_ColWidth(iExcel) \* 0.13171875;
                  }
                  
                  if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xls")
                      worksheet.SaveAs(Excelpfad, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, t, t, t, t, t, t, t, t);
                  else if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xlsx")
                      worksheet.SaveAs(Excelpfad, t, t, t, t, t, t, t, t, t);
      

      and another question: how can I close that file correct? I have:

      newWorkbook.Close(true, n, n);
      excelApp.Quit();
      System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);

      but the "EXCEL.EXE" process still run thank you

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Answer for the 2nd question : ApplicationClass app = new ApplicationClass(); Workbook workBook = app.Workbooks.Open(... app.Workbooks.Close(); app.Quit(); Marshal.FinalReleaseComObject(app);

      1 Reply Last reply
      0
      • M Mschauder

        Hey, I want to create a .xlsx file based on data from a grid. My problem: I have values like 01234 (starts with zero) exporting them make 1234 X| ... so, how can I export it with right format? :confused: I fill the file with:

                    if (Excelpfad == "")
                        return false;
        
                    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                    Microsoft.Office.Interop.Excel.Sheets excelSheets = newWorkbook.Worksheets;
                    string currentSheet = "Tabelle1";
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get\_Item(currentSheet);
        
                    for (int i = 0; i < UsingGrid.Cols; i++)
                    {
                        for (int j = 0; j < UsingGrid.Rows; j++)
                        {
                            worksheet.Cells\[j + 1, i + 1\] = Convert.ToString(UsingGrid\[j, i\]);
                        }
                    }
                    
                    // Autosize cols
                    for (int iExcel = 0; iExcel < UsingGrid.Cols; iExcel++)
                    {
                        ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns\[iExcel + 1, t\]).ColumnWidth = UsingGrid.get\_ColWidth(iExcel) \* 0.13171875;
                    }
                    
                    if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xls")
                        worksheet.SaveAs(Excelpfad, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, t, t, t, t, t, t, t, t);
                    else if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xlsx")
                        worksheet.SaveAs(Excelpfad, t, t, t, t, t, t, t, t, t);
        

        and another question: how can I close that file correct? I have:

        newWorkbook.Close(true, n, n);
        excelApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);

        but the "EXCEL.EXE" process still run thank you

        D Offline
        D Offline
        Dan Mos
        wrote on last edited by
        #3

        Mschauder wrote:

        for (int j = 0; j < UsingGrid.Rows; j++) { worksheet.Cells[j + 1, i + 1] = Convert.ToString(UsingGrid[j, i]); } }

        first question: something like:

        r.NumberFormat = "@";//where r is a Excel.Range

        Note this is language specific(system language and settings). Best way to do it is record a macro while you set the Format to text and look at the VBA produced and translate it to C#. This silly article is especially about formatting. And the export is a lot faster than the cell by cell aproach.

        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