public static bool SaveExperimentDataToExcel(int experiment_record_ID, out string file_name, FileMode file_mode) { file_name = null; string DatabasePath = Directory.GetCurrentDirectory() + DatabaseFolder; if (!Directory.Exists(DatabasePath)) Directory.CreateDirectory(DatabasePath); ExperimentRecord experiment_record; if (!DatabaseHelper.GetExperimentRecord(experiment_record_ID, out experiment_record)) return false; string login_name; if (!DatabaseHelper.GetLoginName(experiment_record.UserInfoID, out login_name)) return false; file_name = string.Format(@"{0}\{1}_{2}_{3}_{4}.xlsx", DatabasePath, experiment_record_ID, experiment_record.ExperimentName.ToString(), login_name, experiment_record.Date.ToString("yyyy-MM-dd")); // 判断文件是否已经存在(理论上应该不会存在) if (File.Exists(file_name)) { if (file_mode == FileMode.CreateNew) { try { File.Delete(file_name); } catch (System.Exception) { throw new Exception("文件" + file_name + "已经打开"); } } else { return false; } } ExperimentData[] experiment_data_collection; if (DatabaseHelper.GetExperimentData(experiment_record_ID, out experiment_data_collection)) { // 创建Excel文件 ExcelStream ExcelOutStream = new ExcelStream(); if (!ExcelOutStream.StartupApp(false)) return false; if (!ExcelOutStream.Creat(file_name)) return false; int row = 1; string[] data = new string[10]; data[0] = "ExperimentRecordID"; data[1] = "Time"; data[2] = "GroupIndex"; data[3] = "SceneNature"; data[4] = "SceneName"; data[5] = "FaceNature"; data[6] = "FaceName"; data[7] = "FacePos"; data[8] = "Judgement"; data[9] = "TrialTime"; if (!ExcelOutStream.WriteLine(row, data)) { ExcelOutStream.Close(); ExcelOutStream.QuitApp(); return false; } // 写入每一行实验数据 foreach (ExperimentData experiment_data in experiment_data_collection) { data[0] = experiment_data.ExperimentRecordID.ToString(); data[1] = experiment_data.Time.ToString(); data[2] = experiment_data.GroupIndex.ToString(); data[3] = (Convert.ToInt32(experiment_data.SceneNature)).ToString(); data[4] = experiment_data.SceneName; data[5] = (Convert.ToInt32(experiment_data.FaceNature)).ToString(); data[6] = experiment_data.FaceName; data[7] = Convert.ToInt32(experiment_data.FacePos).ToString(); data[8] = (Convert.ToInt32(experiment_data.Judgement)).ToString(); data[9] = experiment_data.TrialTime.ToString(); row++; if (!ExcelOutStream.WriteLine(row, data)) { ExcelOutStream.Close(); ExcelOutStream.QuitApp(); return false; } } // 写入备注 data[0] = "注:"; data[1] = "场景或人脸属性: 0-负,1-正,2-中"; data[2] = "人脸位置:0-左,1-右,2-中"; data[3] = ""; data[4] = ""; data[5] = ""; data[6] = ""; data[7] = ""; data[8] = ""; data[9] = ""; ExcelOutStream.WriteLine(row + 2, data); ExcelOutStream.Close(); ExcelOutStream.QuitApp(); return true; } else return false; }
// 导出ImageLibTable 到Excel private bool SaveImageLibTableToExcel() { string imagelibtable_path = Directory.GetCurrentDirectory() + @"\ImageLibTable"; if (!Directory.Exists(imagelibtable_path)) Directory.CreateDirectory(imagelibtable_path); string file_name = imagelibtable_path + @"\ImageLibTable_" + DateTime.Now.ToString("yyyy-mm-dd") + @".xlsx"; // 判断文件是否已经存在(理论上应该不会存在) //if (File.Exists(file_name)) // return false; try { List<List<string>> imagelib_table; if (DatabaseHelper.GetImageVoteTable(ImageVoteType.ImageValanceVote, out imagelib_table)) { // 创建Excel文件 ExcelOutStream = new ExcelStream(); if (!ExcelOutStream.StartupApp(false)) return false; if (!ExcelOutStream.Creat(file_name)) return false; int row = 1; List<string> scene_file_names = ImageDirectory.GetUndefinedSceneNames(); string[] data = new string[scene_file_names.Count + 2]; data[0] = "VoteID"; data[1] = "Date"; for (int i = 0; i < scene_file_names.Count; i++) { int last_index_dot = scene_file_names[i].LastIndexOf(@"."); string scene_name = scene_file_names[i].Substring(0, last_index_dot); data[i + 2] = scene_name; } if (!ExcelOutStream.WriteLine(row, data)) { ExcelOutStream.Close(); ExcelOutStream.QuitApp(); return false; } // 写入每一行实验数据 foreach (List<string> row_data in imagelib_table) { row++; if (!ExcelOutStream.WriteLine(row, row_data.ToArray())) { ExcelOutStream.Close(); ExcelOutStream.QuitApp(); return false; } } // 写入备注 data = new string[scene_file_names.Count + 2]; data[0] = "注:"; data[1] = "打分为1~9分"; data[2] = "-1表示未进行打分"; ExcelOutStream.WriteLine(row + 2, data); ExcelOutStream.Close(); ExcelOutStream.QuitApp(); return true; } else return false; } catch (System.Exception ex) { DatabaseMessage.ShowDatabaseFailure(ex.Message); return false; } }