exporting string data to excel column by column

Posted on 2009-04-27
Last Modified: 2013-12-17
I have an array of C# strings (each string is comma separated). How do I programmatically export each string to consecutive columns in Excel? I don't want to manually transpose row and col inside Excel.
 For example,
col_string[0] = "123, 456, 555"
col_string[1] = "222, 333"
After export to Excel, excel table should look like:

col1    col2
123    222
456    333

Question by:Amitava_B1
    LVL 12

    Accepted Solution

    a csv file you can import into Excel would look like this

    I would build a new data structure then write the file

    the code below may help

    col_string[0] = "123, 456, 555";
                col_string[1] = "222, 333";
                ArrayList<String[]> strArr = new ArrayList<String[]>();
                int maxcolumns = 0;
                for (int i = 0; i < col_string.Length; i++)
                    String[] res = col_string[0].Split(',');
                    if (res.Length > maxcolumns)
                        maxcolumns = res.Length;
                for (int y = 0; y < col_string.Length; i++)
                    String outputLineToFile = "";
                    for (int z = 0; z < maxcolums; z++)
                        if (strArr[y][z] != null)
                            outputLineToFile += strArr[y][z];
                            outputLineToFile += " ";
                        outputLineToFile += ",";
                    // Write line out to file

    Open in new window

    LVL 12

    Expert Comment

    String[] res = col_string[0].Split(',');

    should read

    String[] res = col_string[i].Split(',');

    Author Closing Comment

    Thanks a lot. The overall logic and direction was very good and helpful.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now