Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i need to open already existing excel 2007 file, and add new ,by collecting data from textboxes.but while opening it ,an error has occured as:

Excel cannot open the file 'Copy of New Microsoft Office Excel Worksheet_using.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

and my code is:
C#
<pre lang="c#">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.IO;
using Excel=Microsoft.Office.Interop.Excel;

namespace Hand_Held_Data_Transporter
{
    public partial class AddAccount : Form
    {


        private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
        private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
        private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
        private static Microsoft.Office.Interop.Excel.Application oXL;
        object Missing = System.Reflection.Missing.Value;

        public AddAccount()
        {
            InitializeComponent();
        }

      
        private void btnsave_Click(object sender, EventArgs e)
        {

            oXL = new Microsoft.Office.Interop.Excel.Application();
            oXL.Visible = true;
           oXL.DisplayAlerts = false;
            //error on this line
            mWorkBook = oXL.Workbooks.Open(txtbrowse.Text, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
           // mWorkBook = oXL.Workbooks.Open(txtbrowse.Text);
 
            //Get all the sheets in the workbook
            mWorkSheets = mWorkBook.Worksheets;

            //Get the allready exists sheet
            mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");

            Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;

            int colCount = range.Columns.Count;
            int rowCount = range.Rows.Count;

            //for (int index = 1; index < 15; index++)
            //{
            mWSheet1.Cells[rowCount + 1, 1] = txtid.Text;
            mWSheet1.Cells[rowCount + 1, 2] = txtname.Text;
            mWSheet1.Cells[rowCount + 1, 3] = txtplace.Text;
            mWSheet1.Cells[rowCount + 1, 8] = txtdue.Text;

            //}
            
            mWorkBook.SaveAs(txtbrowse.Text, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
            Missing, Missing, Missing, Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
            Missing, Missing, Missing,
            Missing, Missing);

            //mWorkBook.Close(Missing, Missing, Missing);
           // mWSheet1 = null;

           // mWorkBook = null;

            //oXL.Quit();
            oXL.DisplayAlerts = true;
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
}
        

        private void btnbrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Filter = "All Files(*.*)|*.*";
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                txtbrowse.Text = fdlg.FileName;
                File.ReadAllText(txtbrowse.Text);

            }
        }

     
       
       
    }
}
Posted
Comments
Richard MacCutchan 17-Feb-14 6:22am    
1. Check the file with Excel to see if it is in fact corrupt.
2. What is the line File.ReadAllText(txtbrowse.Text); there for?
Member 10263519 17-Feb-14 6:55am    
am collecting browsed file into txtbrowse (textbox).

yes, that excel file is corrupted after executing this code am not getting what's happening.
Richard MacCutchan 17-Feb-14 7:07am    
The call to File.ReadAllText(txtbrowse.Text); does nothing, because you do not save the text anywhere; and even if you did, it would serve no purpose. And, if the Excel file is corrupt then there is nothing you can do, other than try to get Excel to fix it (which is not always possible).
Member 10263519 17-Feb-14 7:47am    
then how to solve it.
Richard MacCutchan 17-Feb-14 8:29am    
Solve what? Your file is corrupt so that means it is not possible for software to understand its contents. You will need to find a good back up of the file or recreate it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900