Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7627549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:28:28+00:00 2026-05-31T05:28:28+00:00

I am trying to create a program whereby i import data from an excel

  • 0

I am trying to create a program whereby i import data from an excel file(.xlsx), make changes to it and then save it back to the excel file. I have a couple of issues im stuck with which i hope you could help me with :

  1. When i export the datagridview back to Excel, it misses out the headers, how can i get it to export these as well? Everything else exports fine.

  2. At the moment it seems i can only save the export as a .xls file, how can i save it as a .xlsx file. I have changed the filename to xlsx, it creates the file but i cant view it in excel because the format is incorrect.

  3. Finally as above i wanted to save it to the original Excel file where i imported to the datagridview, however i get an error saying the “cannot access …../debug folder, it may be corrupt or read only”.

The following is the code for importing the the Excel (.xlsx) data to the datagridview

public Form1()
    {
        InitializeComponent();

        // populate the dataGridView with the Excel File
        string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;
                                               """, @"C:\Documents and Settings\User\Desktop\Visual Studio\GBstock\GBstock\bin\Debug\FORM TEST.xlsx");
        string query = String.Format("select * from [{0}$]", "Sheet1");
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);
        dataGridView1.DataSource = dataSet.Tables[0];

        // populates the comboBox (cbSuppList) with all column headers
        for (int i = 3; i < dataGridView1.Columns.Count; i++)
            {
                cbSuppList.Items.Add(dataGridView1.Columns[i].HeaderText);
            }
    }

The following is the “Save” code from datagridview to Excel, allbeit .xls format

private void btnSave_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        int i = 0;
        int j = 0;

        for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
        {
            for (j = 0; j <= dataGridView1.Columns.Count - 1; j++)
            {
                DataGridViewCell cell = dataGridView1[j, i];
                xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
            }
        }
        xlWorkBook.SaveAs("C:/Documents and Settings/User/Desktop/Visual Studio/GBstock/GBstock/bin/Debug/FORM TEST.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:28:29+00:00Added an answer on May 31, 2026 at 5:28 am
    1. You never use the HeaderText, like:
      dataGridView1.Columns[X].HeaderText
      Before your first for loop, you may loop the column (as your second for loop) using the previous line (with X the column’s index).
      The basic idea:

      for (int index = 0; index <= dataGridView1.Columns.Count – 1; index ++)
      {
      //dataGridView1.Columns[index ].HeaderText
      }

    2. Before Office 2007, you can use your method.
      But, with Office 2007 and more, you have to use OpenXML
      You can see an example : http://msdn.microsoft.com/en-us/library/bb508943(v=office.12).aspx
      You also may find some ressources in code project.

    3. Have you disposed all ressources used?
      Also, if you write xls data to xslx file, it’s not the correct format (as you see)…
      If your dataAdapter is not disposed, the file may be lock (you cannot overwrite it).
      But, it may be an other error (where do you have the exception? what is the exception’s type?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a python program (using pyUNO ) to make some changes
I'm trying to create a program that takes a text file of c++ code
I'm trying to create a program that reads a .docx file and posts it
I am trying to create a program that will open a text file with
I am trying to create program that simply opens file on sdcard. I tried
I am trying to create a program that reads multiple choice answers from a
I've having problems with my Makefile. I'm trying to create a program from 2
I am trying to create a program to copy all the files from one
I am trying to create a program that loads in over 100 tables from
I am trying to create a program which, given an input file, returns the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.