104 lines
3.5 KiB
C#
104 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data;
|
|
|
|
namespace MatrixCalculator
|
|
{
|
|
public static class CsvParser
|
|
{
|
|
/// <summary>
|
|
/// Converts a CSV string into a DataTable
|
|
/// </summary>
|
|
/// <param name="csv">CSV</param>
|
|
/// <param name="separators">Separator characters</param>
|
|
/// <param name="combine">Should separators be combined?</param>
|
|
public static DataTable GetTable(string csv, char[] separators, bool combine = false)
|
|
{
|
|
DataTable table = new DataTable("table");
|
|
table.Columns.Add();
|
|
|
|
csv = csv.TrimEnd('\n', '\r');
|
|
string[] lines = csv.Split('\n');
|
|
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
string[] vals = lines[i].Split(separators, (combine ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None));
|
|
table.Rows.Add("");
|
|
|
|
for (int j = 0; j < vals.Length; j++)
|
|
{
|
|
if (j >= table.Columns.Count) table.Columns.Add();
|
|
table.Rows[i][j] = vals[j];
|
|
}
|
|
}
|
|
|
|
return table;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts matrix to CSV string
|
|
/// </summary>
|
|
/// <param name="m">The matrix</param>
|
|
/// <param name="sep">CSV separator character</param>
|
|
public static string ToCSV (MathNet.Numerics.LinearAlgebra.Matrix<double> m, char sep = ',')
|
|
{
|
|
StringBuilder str = new StringBuilder();
|
|
|
|
for (int i = 0; i < m.RowCount; i++)
|
|
{
|
|
for (int j = 0; j < m.ColumnCount; j++) {
|
|
str.Append(m[i, j]);
|
|
if (j < m.ColumnCount - 1) str.Append(sep);
|
|
}
|
|
|
|
str.Append('\n');
|
|
}
|
|
|
|
return str.ToString();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Converts the data in a DataGridView to CSV format
|
|
/// </summary>
|
|
/// <param name="m">The DataGridView</param>
|
|
/// <param name="sep">CSV separator</param>
|
|
public static string ToCSV(System.Windows.Forms.DataGridView m, char sep = ',')
|
|
{
|
|
return ToCSV(m, 0, 0, m.Rows.Count, m.Columns.Count, false, sep);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the data in a DataGridView to CSV format
|
|
/// </summary>
|
|
/// <param name="m">The DataGridView</param>
|
|
/// <param name="startRow">First row in range.</param>
|
|
/// <param name="startCol">First column in range.</param>
|
|
/// <param name="endRow">Last row in range.</param>
|
|
/// <param name="endCol">Last column in range.</param>
|
|
/// <param name="just_selected">Only selected cells will be taken.</param>
|
|
/// <param name="sep">CSV separator</param>
|
|
public static string ToCSV(System.Windows.Forms.DataGridView m, int startRow, int startCol, int endRow, int endCol, bool just_selected = false, char sep = ',')
|
|
{
|
|
StringBuilder str = new StringBuilder();
|
|
|
|
for (int i = startRow; i < endRow; i++)
|
|
{
|
|
for (int j = startCol; j < endCol; j++)
|
|
{
|
|
if (!just_selected || (just_selected && m[j, i].Selected))
|
|
str.Append(m[j, i].Value);
|
|
|
|
if (j < m.ColumnCount - 1) str.Append(sep);
|
|
}
|
|
|
|
str.Append('\n');
|
|
}
|
|
|
|
return str.ToString();
|
|
}
|
|
}
|
|
}
|