Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

exporting string data to excel column by column

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
555



0
Amitava_B1
Asked:
Amitava_B1
  • 2
1 Solution
 
williamcampbellCommented:
a csv file you can import into Excel would look like this
123,222
456,333
555,

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(',');
                strArr.Add(res);
                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];
                    }
                    else
                    {
                        outputLineToFile += " ";
                    }
                    outputLineToFile += ",";
                }
 
                // Write line out to file
            }

Open in new window

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

should read

String[] res = col_string[i].Split(',');
0
 
Amitava_B1Author Commented:
Thanks a lot. The overall logic and direction was very good and helpful.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now