Appending Array to Excel Using C#

Experts,

I'm trying to append an array of strings to an already existing Excel 2007 file - there's already data in the file, and I'd like to add my new data to the first empty row.

As File.AppendAllText doesn't exist in this context, is there something that will give me the same usability? Any tips? Thanks!
ak5745Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael FowlerSolutions ConsultantCommented:
While I am sure there are other methods I would create a csv file with C# and then use a macro in the excel document to import the data from the csv. While this is a two step process it is easy to implement

Michael
0
ak5745Author Commented:
Thanks for the reply. I'd like to have the capability of opening Excel and having all my data there already without having any additional steps.

If this is not possible, then I'll probably lean towards the csv route.

Any other ideas?
0
Michael FowlerSolutions ConsultantCommented:
The only thing I can think of is

Save as csv
Create an excel document with a macro that opens the required file and the csv and then appends the data the existing file
Set the macro to run on file open
Call this document from your application

Problem is that this method is messy and there must be a better way to solve the issue

Michael
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ak5745Author Commented:
You're right, it is messy. I know what I'm describing here is essentially a log in excel. Given I can do this, I'd like entries to be added with most recent being placed on top (below a header), where I'll eventually use excel to do analysis of this data.
0
ak5745Author Commented:
Does anyone already do this in any apps they manage? It doesn't strike me as cosmic, but maybe it is. Anyone have any tips/code snippets that might help me out?

Thanks.
0
ak5745Author Commented:
Here's what I found that works for me. Your mileage may vary:

public static void WriteArray(Dictionary<string, object[]> data, string stock)
        {
            Excel.Application excelApp = new Excel.Application();
            Excel.Workbook excelbk = excelApp.Workbooks.Open("C:\\TestSheet.xlsx",

             Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,

             Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,

             Type.Missing, Type.Missing);
            Excel.Worksheet worksheet = (Excel.Worksheet)excelbk.Worksheets["Sheet1"];

            worksheet.get_Range("A3", "A3").EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown, false);

            //Array starting at 3,1 (row 3 column 1).
            var startCell = (Excel.Range)worksheet.Cells[3, 1];

            //Array ending at 3, 18 (row 3 column 18).
            var endCell = (Excel.Range)worksheet.Cells[3, 18];

            var writeRange = worksheet.Range[startCell, endCell];

            //Write array.
            writeRange.Value2 = data[stock];

            excelbk.Save();
            excelbk.Close();
            excelApp.Quit();
            releaseObject(worksheet);
            releaseObject(excelbk);
            releaseObject(excelApp);
        }

        private static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 

Open in new window


Sources:
http://www.clear-lines.com/blog/post/Write-data-to-an-Excel-worksheet-with-C-fast.aspx 
http://csharp.net-informations.com/excel/csharp-open-excel.htm

Hope this helps anyone else who might need it.

If anyone would like to optimize this, please do so before it's closed.

Thanks
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ak5745Author Commented:
I found a solution that works for me. Hope it's useful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.