public static Boolean WriteCSVFile(Values3D pData, string pFileName, string pEncoding = "UTF-8") { try { // Setup the two encoding objects var mSourceEncoding = Encoding.GetEncoding("UTF-8"); var mTargetEncoding = Encoding.GetEncoding(pEncoding); // Determine whether transcoding is necessary bool mTranscode = false; if (mSourceEncoding != mTargetEncoding) { mTranscode = true; } // Create a new streamwriter with the required output encoding var mStreamWriter = new StreamWriter(pFileName, false, mTargetEncoding); string mCsvHeader; if (mTranscode) { mCsvHeader = mTargetEncoding.GetString(mSourceEncoding.GetBytes(pData.GetCSVHeader())); } else { mStreamWriter.Write(mSourceEncoding.GetPreamble()); mCsvHeader = pData.GetCSVHeader(); } mStreamWriter.WriteLine(mCsvHeader); foreach (var mRow in pData.Keys) { foreach (var mCol in pData[mRow].Keys) { string mLine; if (mTranscode) { mLine = mTargetEncoding.GetString(mSourceEncoding.GetBytes(pData[mRow][mCol].ToCSV())); } else { mLine = pData[mRow][mCol].ToCSV(); } mStreamWriter.WriteLine(mLine); } } mStreamWriter.Flush(); mStreamWriter.Close(); mStreamWriter = null; return true; } catch (Exception ex) { Debug.WriteLine(ex.Message); return false; } }
/// <summary> /// Parse the selection with the current settings /// </summary> public void ParseSelectionWithCurrentSettings() { if (this.StatVarProperties != null) { int mFirstDataCol = 1, mFirstDataRow = 1; this.GetRowColOffset( ref mFirstDataRow, ref mFirstDataCol); this.ParsedData = Table.ParseSelectionWithCurrentSettings( this.SelectedRange, this, mFirstDataRow, mFirstDataCol); } }
public static Values3D ParseSelectionWithCurrentSettings(Range pSelection, UploadForm pFrm, int mFirstDataRow, int mFirstDataCol) { var mMessages = new MessageObject(); pFrm.Log("Freistar å prosessere utvalet med gjeldande innstillingar", true); var mTimer = new Stopwatch(); mTimer.Start(); var mSelection = pSelection.Cells.Value; // Create data object to hold values var mData = new Values3D(); // For each row for (int mR = mFirstDataRow; mR <= pSelection.Rows.Count; mR++) { // For each column for (int mC = mFirstDataCol; mC <= pSelection.Columns.Count; mC++) { // Get the value (as a double) var mAdaptiveValue = new AdaptiveValue(Table.GetNullOrDoubleString(mSelection[mR, mC])); // Get manual settings for statistical variable var mManualStatVarProps = pFrm.ParseStatVarProperties(mR, mC, pFrm.StatVarProperties.DataOrientation); // Get/set the corresponding statvar if (mManualStatVarProps.StatVar1 != null) { mAdaptiveValue.variable1 = mManualStatVarProps.StatVar1.var1; if (String.IsNullOrWhiteSpace(mManualStatVarProps.StatVar1.var1)) { mMessages.AddMessage(MessagesNotices.StatVarParseUsingDefaultNotice); } } if (mManualStatVarProps.StatVar2 != null) { mAdaptiveValue.variable2 = mManualStatVarProps.StatVar2.var2; } if (mManualStatVarProps.StatVar3 != null) { mAdaptiveValue.variable3 = mManualStatVarProps.StatVar3.var3; } if (mManualStatVarProps.StatVar4 != null) { mAdaptiveValue.variable4 = mManualStatVarProps.StatVar4.var4; } if (mManualStatVarProps.StatVar5 != null) { mAdaptiveValue.variable5 = mManualStatVarProps.StatVar5.var5; } mAdaptiveValue.fk_variable = mManualStatVarProps.fk_variable; // Get/set the measurement unit mAdaptiveValue.enhet = mManualStatVarProps.MeasurementUnit; if (mAdaptiveValue.enhet == null) { mMessages.AddMessage(MessagesErrors.MUnitNotSet); } // Add date, if available if (pFrm.StatDatumProperties != null) { // Add logic to parse the date value into its individual parts here var mStatDatum = new StatDateParser((string)pFrm.StatDatumProperties.GetValue(mR, mC), (string)pFrm.cbStatDatumFormat.SelectedValue); if (mStatDatum.Success == true) { mAdaptiveValue.ar = mStatDatum.Year; mAdaptiveValue.mnd = mStatDatum.Month; //mAdaptiveValue.Day = mStatDatum.Day; mAdaptiveValue.kvartal = mStatDatum.Quarter; } else { mMessages.AddMessage(MessagesErrors.AutoDateParseError); } } else { // Get the values from the datagridview with statistical variables if (mManualStatVarProps.Year != null) { mAdaptiveValue.ar = mManualStatVarProps.Year; mMessages.AddMessage(MessagesErrors.ManualDateNotSet); } if (mManualStatVarProps.Quarter != null) { mAdaptiveValue.kvartal = mManualStatVarProps.Quarter; mMessages.AddMessage(MessagesNotices.ManualQuarterNotSet); } if (mManualStatVarProps.Month != null) { mAdaptiveValue.mnd = mManualStatVarProps.Month; mMessages.AddMessage(MessagesNotices.ManualMonthNotSet); } if (mManualStatVarProps.Day != null) { mAdaptiveValue.Day = mManualStatVarProps.Day; mMessages.AddMessage(MessagesNotices.ManualDayNotSet); } } if (pFrm.StatAreaIDsProperties != null || pFrm.StatAreaNameProperties != null || pFrm.StatAreaGroupProperties != null) { // Add statareaID if available if (pFrm.StatAreaIDsProperties != null) { mAdaptiveValue.krets_id = (string)pFrm.StatAreaIDsProperties.GetValue(mR, mC); } // Statareaname is only relevant if id is present if (pFrm.StatAreaNameProperties != null) { mAdaptiveValue.krets_navn = (string)pFrm.StatAreaNameProperties.GetValue(mR, mC); } // Statarea group is only relevant if id is present if (pFrm.StatAreaGroupProperties != null) { mAdaptiveValue.region = (string)pFrm.StatAreaGroupProperties.GetValue(mR, mC); } } // If no auto-properties, pick values from manual form fields else { mAdaptiveValue.krets_id = pFrm.tbStatUnitID.Text; mAdaptiveValue.krets_navn = pFrm.tbStatUnitName.Text; mAdaptiveValue.region = pFrm.tbStatUnitGroup.Text; } mData.AddByKey(mAdaptiveValue, mR, mC); } } mTimer.Stop(); pFrm.Log(mMessages.GetMessages()); pFrm.Log(String.Format("Ferdig på {0} ms", mTimer.ElapsedMilliseconds)); return mData; }