/// <summary> /// Creates a new SPSS data document, initializing its dictionary /// by copying the dictionary from an existing SPSS data file. /// </summary> /// <param name="filename"> /// The filename of the new document to create. /// </param> /// <param name="copyDictionaryFromFileName"> /// The filename of the existing SPSS data file to copy the dictionary from. /// </param> /// <returns> /// The newly created <see cref="SpssDataDocument">SPSS data document</see>. /// </returns> public static SpssDataDocument Create(string filename, string copyDictionaryFromFileName) { if (File.Exists(filename)) { throw new InvalidOperationException("File to create already exists."); } if (!File.Exists(copyDictionaryFromFileName)) { throw new FileNotFoundException("File to copy does not exist.", copyDictionaryFromFileName); } using (SpssDataDocument read = SpssDataDocument.Open(copyDictionaryFromFileName, SpssFileAccess.Read)) { SpssDataDocument toReturn = new SpssDataDocument(filename, SpssFileAccess.Create); foreach (SpssVariable var in read.Variables) { toReturn.Variables.Add(var.Clone()); } toReturn.CommitDictionary(); return toReturn; } }
/// <summary> /// 创建Spss文件 /// </summary> /// <param name="doc"></param> public void CreateSpssData(SpssDataDocument doc) { //// Define dictionary //SpssStringVariable v1 = new SpssStringVariable(); //v1.Name = "v1"; //v1.Label = "What is your name?"; //doc.Variables.Add(v1); //SpssNumericVariable v2 = new SpssNumericVariable(); //v2.Name = "v2"; //v2.Label = "How old are you?"; //doc.Variables.Add(v2); //SpssNumericVariable v3 = new SpssNumericVariable(); //v3.Name = "v3"; //v3.Label = "What is your gender?"; //v3.ValueLabels.Add(1, "Male"); //v3.ValueLabels.Add(2, "Female"); //doc.Variables.Add(v3); //SpssDateVariable v4 = new SpssDateVariable(); //v4.Name = "v4"; //v4.Label = "What is your birthdate?"; //doc.Variables.Add(v4); //// Add some data //doc.CommitDictionary(); string dataBase = string.Format("data source={0}", SourcePath); #region 输出变量名 //建立题号与题型、字段对应的字典 Dictionary<string, int> dicQType = new Dictionary<string, int>(); Dictionary<string, int> dicDataType = new Dictionary<string, int>(); Dictionary<string, int> dicOther = new Dictionary<string, int>(); Dictionary<string, string> dicQField = new Dictionary<string, string>(); using (SQLiteConnection conn = new SQLiteConnection(dataBase)) { Visibility = "Visible"; using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = conn; conn.Open(); SQLiteHelper sh = new SQLiteHelper(cmd); DataTable dtField = sh.Select("select Q_Num,TypeID,Q_Field,Q_Lable,Q_OptionsCount,Q_OtherOption,Q_ValueLable,DataTypeID from QuestionInfo"); foreach (DataRow dr in dtField.Rows) { string QNum = dr["Q_Num"].ToString(); string QField = dr["Q_Field"].ToString(); string QLable = dr["Q_Lable"].ToString(); string TypeID = dr["TypeID"].ToString(); string QOptionsCount = dr["Q_OptionsCount"].ToString(); dicQType[QNum] = Convert.ToInt32(TypeID); dicDataType[QNum] = Convert.ToInt32(dr["DataTypeID"]); dicOther[QNum] = Convert.ToInt32(dr["Q_OtherOption"]); if (TypeID == "0" || TypeID == "2") { //var err=SpssDioInterface.spssSetVarName(fh,dr["Q_Field"].ToString(),0); dicQField[QNum] = dr["Q_Field"].ToString(); SpssNumericVariable v = new SpssNumericVariable(); v.Name = QField; v.Label = QLable; v.ValueLabels.Add(-1, "缺失"); v.Alignment = AlignmentCode.SPSS_ALIGN_CENTER; v.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT; if (TypeID == "2") { v.ValueLabels.Add(0, "错"); v.ValueLabels.Add(1, "对"); } else { if (!string.IsNullOrEmpty(dr["Q_ValueLable"].ToString())) { string[] lables = dr["Q_ValueLable"].ToString().Split(','); for (int i = 1; i <= int.Parse(QOptionsCount); i++) { v.ValueLabels.Add(i, lables[i - 1]); } } } doc.Variables.Add(v); } else if (TypeID == "1") { string[] lables = new string[Convert.ToInt32(QOptionsCount)]; if (!string.IsNullOrEmpty(QLable)) { lables = QField.Split(','); } for (int i = 1; i <= Convert.ToInt32(QOptionsCount); i++) { //var err = SpssDioInterface.spssSetVarName(fh, string.Format("{0}_{1}", dr["Q_Field"], i), 0); dicQField[QNum + "_" + i.ToString()] = string.Format("{0}_{1}", QField, i); SpssNumericVariable v = new SpssNumericVariable(); v.Name = string.Format("{0}_{1}", QField, i); v.Label = lables[i - 1]; v.ValueLabels.Add(0, "未选"); v.ValueLabels.Add(1, "选中"); v.Alignment = AlignmentCode.SPSS_ALIGN_CENTER; v.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT; doc.Variables.Add(v); } } else { dicQField[QNum] = QField; switch (dr["DataTypeID"].ToString()) { case "0": { SpssNumericVariable v = new SpssNumericVariable(); v.Name = QField; v.Label = QLable; v.ColumnWidth = GetFNumberLength(dataBase); v.PrintWidth = v.ColumnWidth; v.PrintDecimal = GetFNumDecimal(dataBase); v.Alignment = AlignmentCode.SPSS_ALIGN_CENTER; v.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT; doc.Variables.Add(v); } break; case "1": { SpssStringVariable v = new SpssStringVariable(); v.Name = QField; v.Label = QLable; v.Length = GetStringlength(dataBase, "A_FText"); v.ColumnWidth = v.Length; v.Alignment = AlignmentCode.SPSS_ALIGN_CENTER; v.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_NOM; doc.Variables.Add(v); } break; case "2": { SpssDateVariable v = new SpssDateVariable(); v.Name = QField; v.Label = QLable; v.Alignment = AlignmentCode.SPSS_ALIGN_CENTER; v.PrintFormat = FormatTypeCode.SPSS_FMT_SDATE; v.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT; doc.Variables.Add(v); } break; default: break; } } if (dr["Q_OtherOption"].ToString() == "1") { //var err = SpssDioInterface.spssSetVarName(fh, dr["Q_Field"].ToString() + "_Other", 1); dicQField[QNum + "_Other"] = QField + "_Other"; SpssStringVariable v = new SpssStringVariable(); v.Name = QField + "_Other"; v.Label = QLable + "(其他选项)"; if (TypeID == "0") { v.Length = GetStringlength(dataBase, "A_SingleOther"); } if (TypeID == "1") { v.Length = GetStringlength(dataBase, "A_MultiOther"); } v.ColumnWidth = v.Length; v.Alignment = AlignmentCode.SPSS_ALIGN_CENTER; v.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_NOM; doc.Variables.Add(v); } } doc.CommitDictionary(); conn.Close(); } } #endregion #region 输出数据 using (SQLiteConnection conn = new SQLiteConnection(dataBase)) { using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = conn; conn.Open(); SQLiteHelper sh = new SQLiteHelper(cmd); //try //{ int i = 1; int k = 0; int countRecord = sh.ExecuteScalar<int>("select max(A_Record) from QuestionAnwser"); int countQNum = sh.ExecuteScalar<int>("select count(*) from QuestionInfo"); DataTable dtData = sh.Select("select * from QuestionAnwser order by A_Record"); SpssCase[] cases = new SpssCase[countRecord]; for (int m = 0; m < cases.Length; m++) { cases[m] = doc.Cases.New(); } foreach (DataRow dr in dtData.Rows) { string QNum = dr["Q_Num"].ToString(); switch (dicQType[QNum]) { //单选题 case 0: { cases[k][dicQField[QNum]] = int.Parse(dr["A_Single"].ToString()); if (dicOther[QNum] == 1) { cases[k][dicQField[QNum + "_Other"]] = dr["A_SingleOther"].ToString(); } } break; //多选题 case 1: { if (!string.IsNullOrEmpty(dr["A_Multi"].ToString())) { string[] nums = dr["A_Multi"].ToString().Split('-'); for (int j = 0; j < nums.Length; j++) { cases[k][dicQField[QNum + "_" + (j + 1).ToString()]] = int.Parse(nums[j]); } } if (dicOther[QNum] == 1) { cases[k][dicQField[QNum + "_Other"]] = dr["A_MultiOther"].ToString(); } } break; //判断题 case 2: { cases[k][dicQField[QNum]] = int.Parse(dr["A_TrueOrFalse"].ToString()); } break; //填空题 case 3: { switch (dicDataType[QNum]) { //数字 case 0: { cases[k][dicQField[QNum]] = Decimal.Parse(dr["A_FNumber"].ToString()); } break; //文本 case 1: { cases[k][dicQField[QNum]] = dr["A_FText"].ToString(); } break; //日期 case 2: { if (string.IsNullOrEmpty(dr["A_FDateTime"].ToString()) || dr["A_FDateTime"].ToString() == DateTime.MinValue.ToString()) { cases[k][dicQField[QNum]] = null; } else { cases[k][dicQField[QNum]] = DateTime.Parse(Convert.ToDateTime(dr["A_FDateTime"]).ToString("yyyy/MM/dd")); } } break; default: break; } } break; default: break; } if (i % countQNum == 0) { cases[k].Commit(); Progress = (k + 1) * 100.0 / countRecord; ProgressMessage = string.Format("已完成 {0:0.0}%", Progress); k++; } i++; } //for (int l = 0; l < cases.Length; l++) //{ // cases[l].Commit(); //} //} //catch (Exception) //{ // throw; //} conn.Close(); } Visibility = "Hidden"; } #endregion }