Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the best way to save a large multidimensional array to a CSV file?
For example,
double[,] data = new double[10000,650] and each cell is populated with data.
I would like to visualize a 10000 row x 650 column matrix
Posted

This code here saves your array to a csv and may show you how you could achieve it:
C#
private void Form1_Load(object sender, EventArgs e)
{
   double[,] data = new double[10000, 650];
   using (StreamWriter outfile = new StreamWriter(@"C:\Temp\test.csv"))
   {
      for (int x = 0; x < 10000; x++)
      {
         string content = "";
         for (int y = 0; y < 650; y++)
         {
            content += data[x, y].ToString("0.00") + ";";
         }
         outfile.WriteLine(content);
      }
   }
}
 
Share this answer
 
v2
Comments
ThatsAlok 5-Sep-12 1:51am    
though i like your solutions, it would be very memory hungry as reason as follow :
1. string is immutable, so lot of memory consumption going on!
2. instead of keeping everything in memory, you can write to file line by line
JF2015 5-Sep-12 1:59am    
You are totally right. I modified the code to write the file line by line without consuming too much memory.
ThatsAlok 5-Sep-12 5:30am    
you can use StringBuilder instead on String! rest code look fine
Hi,

Here i can see more feasible and efficient code for converting Multidimensional array into CSV file. Please check this link,

Multidimensional Array to CSV[^]

Link show you CSV from string array but hope you can convert this function into double array.

[Update]

Updated Solution : This is what i have created. This may be more efficient.

C#
string path = "mycsv.csv";

double[,] multiDimensionalArray = { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 } };
var enumerator = multiDimensionalArray.Cast<double>()
                .Select((s, i) => (i + 1) % 3 == 0 ? string.Concat(s, Environment.NewLine) : string.Concat(s, ","));

var item = String.Join("", enumerator.ToArray<string>());
File.AppendAllText(path, item);

[/Update]

Thanks
-Amit Gajjar
 
Share this answer
 
v2
Separate the rows by an Environment.NewLine, the cell values within a row by whatever character is guaranteed not to exist within values (tab, space, period, ...).
 
Share this answer
 
If you want to do it in aspx:
My aspx code:
XML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="ExportToExcelFromDataTable._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

</asp:Content>



cs file;

XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Reflection;

namespace ExportToExcelFromDataTable
{
    public partial class _Default : System.Web.UI.Page
    {
        private DataTable _dt;

        public DataTable dt
        {
            get
            {
                return _dt;
            }
            set
            {
                _dt = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            double[,] items = new double[100, 100];
            dt = new DataTable();
            for (int dimension = 0; dimension <= items.GetUpperBound(items.Rank - 1); dimension++)
            {
                dt.Columns.Add("Column" + (dimension + 1));
            }

            for (int element = 0; element <= items.GetUpperBound(items.Rank - 2); element++)
            {
                DataRow row = dt.NewRow();
                for (int dimension = 0; dimension <= items.GetUpperBound(items.Rank - 1); dimension++)
                {

                    row["Column" + (dimension + 1)] = items[element, dimension];
                }
                dt.Rows.Add(row);

            }

            ExporttoExcel(dt);

        }

        private void ExporttoExcel(DataTable table)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            //HttpContext.Current.Response.ContentType = "application/ms-word";
            HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
            // HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.doc");
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
            HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
            HttpContext.Current.Response.Write("<BR><BR><BR>");
            HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
            int columnscount = table.Columns.Count;

            for (int j = 0; j < columnscount; j++)
            {
                HttpContext.Current.Response.Write("<Td>");
                HttpContext.Current.Response.Write("<B>");
                HttpContext.Current.Response.Write(table.Columns[j].ColumnName.ToString());
                HttpContext.Current.Response.Write("</B>");
                HttpContext.Current.Response.Write("</Td>");
            }
            HttpContext.Current.Response.Write("</TR>");
            foreach (DataRow row in table.Rows)
            {
                HttpContext.Current.Response.Write("<TR>");
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write("<Td>");
                    HttpContext.Current.Response.Write(row[i].ToString());
                    HttpContext.Current.Response.Write("</Td>");
                }

                HttpContext.Current.Response.Write("</TR>");
            }
            HttpContext.Current.Response.Write("</Table>");
            HttpContext.Current.Response.Write("</font>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }


    }
}
 
Share this answer
 

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