Solved

How do I save data to a specific version of Excel in C#?

Posted on 2008-09-29
2
310 Views
Last Modified: 2008-10-02
Below I have attached my present code which works fine, but saves data to an unrecognized Excel format.  When opening the file in Excel, the following error is produced:

"The file you are trying to open, 'filename.xls', is in a different format than specified by the file extension.  Verify that the file is not corrupted and is from a trusted source before opening the file.  Do you want to open the file now?"

Answering "Yes" causes the file to open normally.  But, I have another application that reads in Excel files and this format issue prevents the import of Excel data.  

Trying different parameters for the key line:

m_objBook.SaveAs(m_strSampleFolder + todaysDate + OrgSelection + ".xls", m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

has not yet revealed the solution.  Thoughts?

//************************************* The following code creates an excel spreadsheet and deposits data. *******************************
            // Start a new workbook in Excel.
            m_objExcel = new Excel.Application();
            m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
            m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
            m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
            m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
            m_objSheet.Name = "GPS";
 
            // Create an array for the headers and add it to cells A1:L1.
            object[] objHeaders = { "AccountNumber", "Code", "Contract Price", "Date Time", "Extended Price", "ManualPrice", "NumberOfSets", "NumberOfSheets", "PO Number", "Project", "Size", "SqFeet Bond", "SqFeet Mylar", "SqFeet Vellum", "Total Sq ft" };
            m_objRange = m_objSheet.get_Range("A1", "L1");
            m_objRange.Value2 = objHeaders;
            m_objFont = m_objRange.Font;
            m_objFont.Bold = true;
 
            // Get array and add it to the worksheet starting at cell A1.
            m_objRange = m_objSheet.get_Range("A1", m_objOpt);
            m_objRange = m_objRange.get_Resize(20000, 15);
            m_objRange.Value2 = excelOutputArray;
 
            // Format cells to numeric format.  (remaining issue:  array data is string, need to create decimal valued array for numeric data)
            m_objRange = m_objSheet.get_Range("C2", "C20001");
            m_objRange.NumberFormat = "###0.0000";
            m_objRange = m_objSheet.get_Range("E2", "E20001");
            m_objRange.NumberFormat = "###0.0000";
            m_objRange = m_objSheet.get_Range("F2", "F20001");
            m_objRange.NumberFormat = "###0.0000";
            m_objRange = m_objSheet.get_Range("G2", "G20001");
            m_objRange.NumberFormat = "###0";
            m_objRange = m_objSheet.get_Range("H2", "H20001");
            m_objRange.NumberFormat = "###0";
            m_objRange = m_objSheet.get_Range("K2", "K20001");
            m_objRange.NumberFormat = "###0";
            m_objRange = m_objSheet.get_Range("L2", "L20001");
            m_objRange.NumberFormat = "###0.0000";
            m_objRange = m_objSheet.get_Range("M2", "M20001");
            m_objRange.NumberFormat = "###0.0000";
            m_objRange = m_objSheet.get_Range("N2", "N20001");
            m_objRange.NumberFormat = "###0.0000";
            m_objRange = m_objSheet.get_Range("O2", "O20001");
            m_objRange.NumberFormat = "###0.#";
 
            StartWizardBtn.Text = "Saving Data to Disk...";
            Application.DoEvents(); 
 
            // Save the Workbook and quit Excel.
            m_objBook.SaveAs(m_strSampleFolder + todaysDate + OrgSelection + ".xls", m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
            m_objBook.Close(false, m_objOpt, m_objOpt);
            m_objExcel.Quit();
 
 
 
            StartWizardBtn.Text = "Start Wizard";
            Application.DoEvents(); 
 
        }

Open in new window

0
Comment
Question by:dparkes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
johnaryan earned 350 total points
ID: 22601107
It depends on the verdion of the Excel Libraries your using.
The latest i think is Microsoft.Office.Interop.Excel Version 12.

Good Reference:http://msdn.microsoft.com/en-us/library/aa163987.aspx
0
 
LVL 1

Author Comment

by:dparkes
ID: 22605043
The intended users will all be using a 2007 version of Excel.  Is there alternate code and/or methods that will allow a C# 2008 program to automatically save array data to a programmatically specified older Excel format (ie. Excel '97)?
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 48
C# Linq - Join two objects into one 3 55
I need help setting tabindex to textboxes in my MVC.Net application 3 37
async questions 5 50
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question