TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Liu ChiaChi
NA
18
2.9k
Read a csv file using 2D array
Nov 20 2020 3:33 AM
I want to read a csv file using 2D array, there will be an exception error, please help me
private
void
button1_Click(
object
sender, EventArgs e)
{
//new a excel
Microsoft.Office.Interop.Excel.Application excel =
new
Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(
true
);
if
(openFileDialog1.ShowDialog() ==System.Windows.Forms.DialogResult.OK)
{
string
theFile = openFileDialog1.FileName;
Encoding enc = Encoding.GetEncoding(
"UTF-8"
);
//var readText1 = File.ReadAllLines(theFile);
var readText1 = System.IO.File.ReadAllLines(theFile);
var readText = File.ReadAllLines(theFile).Select(l =>
l.Split(
','
).ToArray()).ToArray();
var rows = readText1.Count();
//Number of rows columns
var columns = readText1[0].Split(
','
).Count();
//var columns = readText.Count();
excel.Visible =
true
;
for
(
int
i = 0; i < rows; i++)
{
for
(
int
j = 0; j < columns; j++)
{
excel.Cells[i + 1, j + 1] = readText[j][i];
//Console.WriteLine();
}
}
}
}
Attachment:
Source_code.zip
Reply
Answers (
2
)
Calculate Sum Total in Column Gridview by Key Enter
How to convert string "1,500" to integer value