CSVWriter.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WpfTest1.Toolkits
  8. {
  9. public class CSVWriter
  10. {
  11. public static void generateWaveToCSV(List<double> leftRed, List<double> leftInfrared, List<double> rightRed, List<double> rightInfrared, SQLite.Patient pa, SQLite.Record re)
  12. {
  13. FileStream fs = new FileStream(Constants.exportWavePath + "\\" + re.recordTime.ToString("yyyyMMddHHmmss") + "_" + pa.p_name + ".csv", FileMode.Create);
  14. StreamWriter sw = new StreamWriter(fs);
  15. //开始写入
  16. sw.Write("leftRed,leftInfrared,rightRed,rightInfrared\n");
  17. int counter = new int[] { leftRed.Count, rightRed.Count, leftInfrared.Count, rightInfrared.Count }.Max();
  18. for(int i = 0; i < counter; ++i)
  19. {
  20. string[] thisLine = new string[] { "","","",""};
  21. if((re.hand == "b" || re.hand=="l"))
  22. {
  23. if(i < leftRed.Count)
  24. {
  25. thisLine[0] = leftRed[i].ToString();
  26. }
  27. if(i < leftInfrared.Count)
  28. {
  29. thisLine[1] = leftInfrared[i].ToString();
  30. }
  31. }
  32. if ((re.hand == "b" || re.hand == "r" ))
  33. {
  34. if (i < rightRed.Count)
  35. {
  36. thisLine[2] = rightRed[i].ToString();
  37. }
  38. if (i < rightInfrared.Count)
  39. {
  40. thisLine[3] = rightInfrared[i].ToString();
  41. }
  42. }
  43. sw.Write(thisLine[0]+","+ thisLine[1] + "," + thisLine[2] + "," + thisLine[3] + "\n");
  44. }
  45. //清空缓冲区
  46. sw.Flush();
  47. //关闭流
  48. sw.Close();
  49. fs.Close();
  50. }
  51. }
  52. }