Em mới học C#. Em có làm arduino đo giá trị cảm biến liên kết với winform em muốn làm sau tầm 1 phút thì dữ liệu tự lưu lại. Em dùng timer để làm việc đó, nhưng sau khoảng 1 phút thì lại tạo thêm một file excel để lưu dữ liệu mới, có cách nào để lưu dữ liệu
đo được lúc sau vào file excel được tạo lần đầu không ạ. Em xin cảm ơn mọi người. Đoạn code em làm như thế này ạ:
private void timer6_Tick(object sender, EventArgs e)
{
timer6.Start();
if (serialPort1.IsOpen)
{
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = true;
Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.ActiveSheet;
Microsoft.Office.Interop.Excel.Range rg = (Microsoft.Office.Interop.Excel.Range)ws.get_Range("A1", "C1");
ws.Cells[1, 1] = "Thời gian (s)";
ws.Cells[1, 2] = "Dữ liệu ";
ws.Cells[1, 3] = "Control ";
rg.Columns.AutoFit();
int i = 2;
int j = 1;
foreach (ListViewItem comp in listView1.Items)
{
ws.Cells[i, j] = comp.Text.ToString();
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)
{
ws.Cells[i, j] = drv.Text.ToString();
j++;
}
j = 1;
i++;
}
listView1.Items.Clear();
}
}
Cái này thuộc về lựa chọn giải pháp. Thay vào lưu vào Excel sao bạn không thử lưu vào DB, SQLite chẳng hạn, rất nhẹ.
Sau đó viết thêm một vài dòng code xuất từ DB ra excel thì nó sẽ ổn hơn.