CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jan 2014
    Posts
    29

    Export datagridview to excel 2007 not open by c#(open suddenly for second then close

    I make datagridview to show data then export to excel 2007

    in my computer it work ok no proplem

    when i open exe file in another computer and press button export datagridview to excel

    excel sheet open for one second and close suddenly and
    give me exception as picture below
    another computeres have office 2007

    and my computer also have 2007 and it open in my computer
    and in another computeres not open and have 2007

    this is my code

    using

    System;
    using

    System.Collections.Generic;
    using

    System.ComponentModel;
    using

    System.Data;
    using

    System.Drawing;
    using

    System.Linq;
    using

    System.Text;
    using

    System.Windows.Forms;
    using

    System.Data.SqlClient;
    using

    Excel = Microsoft.Office.Interop.Excel;
    Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
    System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
    Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
    app.Visible = true;

    try
    {

    worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
    worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
    worksheet.Name = "Exported from Ketoan";
    for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
    {
    worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
    }

    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
    {
    for (int j = 0; j < dataGridView1.Columns.Count; j++)
    {
    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
    }
    }


    string fileName = String.Empty;
    saveFileDialog1.Filter = "Excel files |*.xlsx|All files (*.*)|*.*";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;


    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
    fileName = saveFileDialog1.FileName;

    workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    }
    else
    return;


    }
    catch (System.Exception ex)
    {
    Messagebox.show("there are error"+ex);
    }
    finally
    {
    app.Quit();
    workbook = null;
    app = null;
    }

    the code working for me in my computer

    but my question

    Why it open and and show exception in another computeres and not show data

    And how to solve this proplem

    My proplem happen in more computeres not one computers all have office 2007
    and when i put messagebox it show exception
    as picture below
    Name:  proplem11.jpg
Views: 644
Size:  44.4 KB
    Last edited by ahmedsa; July 8th, 2014 at 06:28 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured