1---EXCEL文件的创建与写入
private void btnStack_Click(object sender, RoutedEventArgs e) { Microsoft.Office.Interop.Excel.Application MyExcel_Application = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook My_Workbook; //工作薄实例声明 Microsoft.Office.Interop.Excel._Worksheet My_Worksheet; //工作表实例声明 MyExcel_Application.Application.Workbooks.Add(true); //不存在相同文件,则建立一个新的文件 My_Workbook = MyExcel_Application.ActiveWorkbook; //工作薄赋值为excel中的已激活工作薄 My_Worksheet = (Microsoft.Office.Interop.Excel._Worksheet)My_Workbook.ActiveSheet; //工作表赋值为工作簿中已激活的工作表 //获取数据中的行数,并将其保存到excel中 for (int i = 0; i < count.Count; i++) { My_Worksheet.Cells[i + 1, 1] = "count[i].ToString()"; My_Worksheet.Cells[i + 1, 2] = "temperatureValue[i].ToString()"; My_Worksheet.Cells[i + 1, 3] = "humidityValue[i].ToString()"; My_Worksheet.Cells[i + 1, 4] = "lightValue[i].ToString()"; My_Worksheet.Cells[i + 1, 5] = "MyDateTime[i].ToString()"; } My_Worksheet.Name = "温湿度变化"; //给工作表取名字 //将工作簿另存为 string Name = DateTime.Now.ToString(); Name = Name.Replace("/", ""); Name = Name.Replace(":", ""); Name = Name.Replace(" ", ""); My_Workbook.SaveAs(@"D:\" + Name.Trim() + ".xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, 0, Missing.Value, Missing.Value, Missing.Value, Missing.Value); My_Workbook.Close(); //关闭工作簿 MyExcel_Application.Quit(); //退出excel MyExcel_Application = null; //赋值为NULL }
2---C#中不定长数组 用 ArrayList
ArrayList count = new ArrayList(); ArrayList temperatureValue = new ArrayList(); ArrayList humidityValue = new ArrayList(); ArrayList lightValue = new ArrayList(); ArrayList MyDateTime = new ArrayList();