static public void SaveAnswer(List <Answer> answers, List <byte[]> photos, string guid) { try { AnswerStorge ans = new AnswerStorge(); ans.hospital = selectedHospital; ans.department = selectedDepartment; ans.answers = answers; ans.photos = new List <byte[]>(); foreach (byte[] d in photos) { ans.photos.Add(d); } // photos.CopyTo(ans.photos); //ans.photos = photos; ans.guid = guid; string hash = ans.hospital.name + "%" + ans.department.name + "%" + Path.GetFileNameWithoutExtension(selectedDepartment.questionID); List <AnswerStorge> slist = null; if (Answers.ContainsKey(hash)) { slist = Answers[hash]; } else { slist = new List <AnswerStorge>(); Answers[hash] = slist; } slist.Add(ans); string folderPath = Application.persistentDataPath + "/" + ans.hospital.name; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } folderPath += "/" + ans.department.name; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } folderPath += "/" + Path.GetFileNameWithoutExtension(selectedDepartment.questionID); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } FileStream fs = new FileStream(folderPath + "/" + "answer.bin", FileMode.OpenOrCreate); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, slist); fs.Close(); fs = new FileStream(Application.persistentDataPath + "/" + "answer.bin", FileMode.OpenOrCreate); bf = new BinaryFormatter(); bf.Serialize(fs, Answers); fs.Close(); } catch (Exception e) { Debug.LogError(e.Message); } }
public void ExportData(string path) { try { //AnswerStorge ans = null; foreach (KeyValuePair <string, List <AnswerStorge> > pair in Answers) { ES2Spreadsheet sheet = new ES2Spreadsheet(); AnswerStorge _ans = pair.Value[0]; //统计有多少个单选 /*int numType2 = 0; * for (int i = 0; i < pair.Value[0].answers.Count; i++) * { * if (pair.Value[0].answers[i].type == 2) * numType2++; * }*/ // Add data to cells in the spreadsheet. //List<Question> qs = QuestionMap[pair.Value[0].department.questionPath]; for (int row = 0; row < pair.Value.Count + 1; row++) { int tcol = 0; for (int col = 0; col < pair.Value[0].answers.Count + 6; col++, tcol++) { if (row == 0) { switch (col) { case 0: sheet.SetCell(tcol, row, "设备id"); break; case 1: sheet.SetCell(tcol, row, "病人id"); break; case 2: sheet.SetCell(tcol, row, "调查科室"); break; case 3: sheet.SetCell(tcol, row, "调查开始时间"); break; case 4: sheet.SetCell(tcol, row, "调查结束时间"); break; case 5: sheet.SetCell(tcol, row, "耗时(秒)"); break; default: if (pair.Value[0].answers[col - 6].type == 2) { sheet.SetCell(tcol, row, (col - 5).ToString() + "." + pair.Value[0].answers[col - 6].shorttxt); sheet.SetCell(tcol + 1, row, (col - 5).ToString() + "." + pair.Value[0].answers[col - 6].shorttxt); sheet.SetCell(tcol + 2, row, (col - 5).ToString() + "." + pair.Value[0].answers[col - 6].shorttxt); tcol += 2; } else { sheet.SetCell(tcol, row, (col - 5).ToString() + "." + pair.Value[0].answers[col - 6].shorttxt); } break; } } else { switch (col) { case 0: if (AndroidDeviceInfo.SERIAL == null) { sheet.SetCell(tcol, row, "dummy"); } else { sheet.SetCell(tcol, row, AndroidDeviceInfo.SERIAL); } break; case 1: sheet.SetCell(tcol, row, pair.Value[row - 1].guid); break; case 2: sheet.SetCell(tcol, row, pair.Value[row - 1].department.name); break; case 3: sheet.SetCell(tcol, row, pair.Value[row - 1].answers[0].startTime.ToShortDateString().ToString() + " " + pair.Value[row - 1].answers[0].startTime.ToShortTimeString().ToString()); break; case 4: sheet.SetCell(tcol, row, pair.Value[row - 1].answers[pair.Value[row - 1].answers.Count - 1].endTime.ToShortDateString().ToString() + " " + pair.Value[row - 1].answers[pair.Value[row - 1].answers.Count - 1].endTime.ToShortTimeString().ToString()); break; case 5: sheet.SetCell(tcol, row, (pair.Value[row - 1].answers[pair.Value[row - 1].answers.Count - 1].endTime - pair.Value[row - 1].answers[0].startTime).TotalSeconds); break; default: if (pair.Value[row - 1].answers[col - 6].type != 2) { sheet.SetCell(tcol, row, pair.Value[row - 1].answers[col - 6].longtxt); } else { UInt32 t = 0; for (int i = 0; i < MAX_ANS_COUNT; ++i) { if (pair.Value[row - 1].answers[col - 6].answers[i] == 1) { t += (UInt32)Math.Pow(2, i); } } //sheet.SetCell(col, row, t.ToString()); string[] sArray = pair.Value[row - 1].answers[col - 6].longtxt.Split(','); if (sArray.Length == 4) { sheet.SetCell(tcol, row, sArray[0]); sheet.SetCell(tcol + 1, row, sArray[1]); sheet.SetCell(tcol + 2, row, sArray[2]); } else if (sArray.Length == 3) { sheet.SetCell(tcol, row, sArray[0]); sheet.SetCell(tcol + 1, row, sArray[1]); sheet.SetCell(tcol + 2, row, ""); } else if (sArray.Length == 2) { sheet.SetCell(tcol, row, sArray[0]); sheet.SetCell(tcol + 1, row, ""); sheet.SetCell(tcol + 2, row, ""); } tcol += 2; } break; } } } } string _folderPath = path + "/" + _ans.hospital.name; _folderPath += "/" + _ans.department.name; _folderPath += "/" + Path.GetFileNameWithoutExtension(_ans.department.questionID); sheet.Save(_folderPath + "/" + "output.csv"); foreach (AnswerStorge ans in pair.Value) { for (int i = 0; i < ans.photos.Count; ++i) { string imgstring = "data:image / jpg; base64," + Convert.ToBase64String(ans.photos[i]); File.WriteAllBytes(_folderPath + "/" + ans.guid + "-" + (i + 1).ToString() + ".jpg", ans.photos[i]); } } #if UNITY_ANDROID //Toast.ShowToast("导出数据成功,导出路径为 :" + _folderPath); #endif } //SimpleExport_ScoreCSV.ExportCSV() } catch (Exception e) { #if UNITY_ANDROID Toast.ShowToast("导出本地数据错误,请退出后重试"); #endif Debug.LogError(e.Message); } }