Tôi cũng muốn tham khảo xem giải pháp nào tốt hơn cho việc đọc file Excel trong C#.
- Vấn đề cần hỏi thêm là bạn đang dùng thư viện nào để đọc file Excel? Bạn dùng COM interop (Microsoft.Office.Interop.Excel) để đọc, hay thư viện ExcelDataReader, ADO.Net...? Nếu dùng COM interop thì khỏi phải bàn về tốc độ rồi vì nó chậm nhất, dùng để xử lý các file chút chút thôi, rồi còn phải giải phóng nó chứ không nó treo Excel.
- Nếu dữ liệu lớn mà đọc hết vô Memory thì máy yếu yếu là mệt mỏi.
a thường dùng cách nào để đưa data từ Sheet vào Array và trả ngược lại?
E dùng cách này và thấy nó chậm khi dữ liệu lớn - interop (Microsoft.Office.Interop.Excel)
hàm để nhận array:
public string[,] GetNewArrays(Excel.Worksheet ws, string Start_cell, string Right_Columns, int LastRow)
{
int row_top = ws.Range[Start_cell].Row;
int row_below = ws.Range[Right_Columns + LastRow].Row;
int col_left = ws.Range[Start_cell].Column;
int col_right = ws.Range[Right_Columns + LastRow].Column;
int row_arr = row_below - row_top + 1;
int col_arr = col_right - col_left + 1;
string[,] array_getvalue = new string[row_arr, col_arr];
for (long i = 0; i < row_arr; i++)
{
for (long j = 0; j < col_arr; j++)
{
//Dua sheet vao mang
if (ws.Cells[row_top + i, col_left + j].Value2 == null)
{
array_getvalue[i, j] = "";
continue;
}
array_getvalue[i, j] = ws.Cells[row_top + i, col_left + j].Value.ToString();
}
}
return array_getvalue;
}
Gọi hàm để sử dụng,
string[,] data_test1 = Globals.ThisAddIn.GetNewArrays(Sh1, "A1", "B",lastRow_B);
for (int i = 0; i < lastRow_B; i++)
{
for (int j = 0; j < 2; j++)
{
Sh1.Cells[i+1,j + 6].Value = data_test1[i,j];
}
}
//-------------------------------------------------------------------------------
E đang tìm cách khác hiệu quả và nhanh hơn, mn cho e tham khảo ý kiến