/// <summary> /// Retrieves formatting information for a line in a fixed length text file. /// </summary> /// <param name="tab">DataTable containing the data.</param> /// <param name="useLineTerminator">String containing one or more characters that will denote the end of a line of data.</param> /// <param name="columnNamesOnFirstLine">If true, a line containing the column names in delimited format will be the first line returned.</param> /// <param name="allowDataTruncation">If true, data longer than defined column length will be truncated; otherwise an exception will be thrown.</param> /// <param name="tableNumber">Arbitrary number used to identify the table.</param> /// <param name="lineTerminatorChars">Default is CR/LF for characters to mark end of line. You can override the default by setting this parameter.</param> /// <returns>PFFixedLengthDataLine object.</returns> public PFFixedLengthDataLine GetFixedLengthLineDefinitionFromTable(DataTable tab, bool useLineTerminator, bool columnNamesOnFirstLine, bool allowDataTruncation, int tableNumber, string lineTerminatorChars) { int colInx = 0; int maxColInx = -1; int colLen = -1; DataColumnCollection cols = tab.Columns; PFFixedLengthDataLine fixedLengthLine = new PFFixedLengthDataLine(cols.Count); maxColInx = tab.Columns.Count - 1; fixedLengthLine.UseLineTerminator = useLineTerminator; fixedLengthLine.LineTerminator = lineTerminatorChars; fixedLengthLine.ColumnNamesOnFirstLine = columnNamesOnFirstLine; fixedLengthLine.AllowDataTruncation = allowDataTruncation; fixedLengthLine.MaxColumnLengthOverride = this.MaxColumnLengthOverride; for (colInx = 0; colInx <= maxColInx; colInx++) { if (PFFixedLengthDataLine.DataTypeIsNumeric(cols[colInx].DataType)) { colLen = PFFixedLengthDataLine.GetNumericTypeMaxExtractLength(cols[colInx].DataType); } else if (PFFixedLengthDataLine.DataTypeIsDateTime(cols[colInx].DataType)) { colLen = PFFixedLengthDataLine.GetDateTimeTypeMaxExtractLength(cols[colInx].DataType); } else { colLen = cols[colInx].MaxLength; } fixedLengthLine.SetColumnDefinition(colInx, cols[colInx].ColumnName, colLen); } if (columnNamesOnFirstLine == true) { if (returnResultAsString != null) { returnResultAsString(fixedLengthLine.OutputColumnNames(), tableNumber); } } return(fixedLengthLine); }
}//end ExtractFixedLengthDataFromTable method /// <summary> /// Extracts data from DataTable into fixed length text format. /// </summary> /// <param name="fixedLengthLineDef">Object containing column definitions to use for the output fixed length text data.</param> /// <param name="tab">DataTable containing the data.</param> /// <param name="useLineTerminator">String containing one or more characters that will denote the end of a line of data.</param> /// <param name="columnNamesOnFirstLine">If true, a line containing the column names in delimited format will be the first line returned.</param> /// <param name="allowDataTruncation">If true, data longer than defined column length will be truncated; otherwise an exception will be thrown.</param> /// <param name="tableNumber">Arbitrary number used to identify the table.</param> /// <param name="lineTerminatorChars">Default is CR/LF for characters to mark end of line. You can override the default by setting this parameter.</param> /// <remarks>This version of ExtractFixedLengthDataFromTable adjusts column definitions to allow for changes to the data lengths in the data table.</remarks> public void ExtractFixedLengthDataFromTable(PFFixedLengthDataLine fixedLengthLineDef, DataTable tab, bool useLineTerminator, bool columnNamesOnFirstLine, bool allowDataTruncation, int tableNumber, string lineTerminatorChars) { int rowInx = 0; int maxRowInx = -1; int dtColInx = 0; int defColInx = 0; int maxColInx = -1; int colLen = -1; DataColumnCollection cols = tab.Columns; PFFixedLengthDataLine dtFixedLengthLine = new PFFixedLengthDataLine(cols.Count); bool colDefOverride = false; maxRowInx = tab.Rows.Count - 1; maxColInx = tab.Columns.Count - 1; dtFixedLengthLine.UseLineTerminator = fixedLengthLineDef.UseLineTerminator; dtFixedLengthLine.LineTerminator = fixedLengthLineDef.LineTerminator; dtFixedLengthLine.ColumnNamesOnFirstLine = fixedLengthLineDef.ColumnNamesOnFirstLine; dtFixedLengthLine.AllowDataTruncation = fixedLengthLineDef.AllowDataTruncation; dtFixedLengthLine.MaxColumnLengthOverride = fixedLengthLineDef.MaxColumnLengthOverride; dtFixedLengthLine.DefaultStringColumnLength = fixedLengthLineDef.DefaultStringColumnLength; for (dtColInx = 0; dtColInx <= maxColInx; dtColInx++) { for (defColInx = 0; defColInx < fixedLengthLineDef.ColumnDefinitions.ColumnDefinition.Length; defColInx++) { colDefOverride = false; if (tab.Columns[dtColInx].ColumnName == fixedLengthLineDef.ColumnDefinitions.ColumnDefinition[defColInx].ColumnName) { dtFixedLengthLine.SetColumnDefinition(dtColInx, fixedLengthLineDef.ColumnDefinitions.ColumnDefinition[defColInx].ColumnName, fixedLengthLineDef.ColumnDefinitions.ColumnDefinition[defColInx].ColumnLength, fixedLengthLineDef.ColumnDefinitions.ColumnDefinition[defColInx].ColumnDataAlignment); colDefOverride = true; break; } } if (colDefOverride == false) { //dynamically build the column definition if (PFFixedLengthDataLine.DataTypeIsNumeric(tab.Columns[dtColInx].DataType)) { colLen = PFFixedLengthDataLine.GetNumericTypeMaxExtractLength(tab.Columns[dtColInx].DataType); } else if (PFFixedLengthDataLine.DataTypeIsDateTime(tab.Columns[dtColInx].DataType)) { colLen = PFFixedLengthDataLine.GetDateTimeTypeMaxExtractLength(tab.Columns[dtColInx].DataType); } else { colLen = tab.Columns[dtColInx].MaxLength; } dtFixedLengthLine.SetColumnDefinition(dtColInx, tab.Columns[dtColInx].ColumnName, colLen, PFDataAlign.LeftJustify); } } if (columnNamesOnFirstLine == true) { if (returnResultAsString != null) { returnResultAsString(dtFixedLengthLine.OutputColumnNames(), tableNumber); } } for (rowInx = 0; rowInx <= maxRowInx; rowInx++) { DataRow row = tab.Rows[rowInx]; if (returnResultAsString != null) { for (dtColInx = 0; dtColInx <= maxColInx; dtColInx++) { dtFixedLengthLine.SetColumnData(dtColInx, row[dtColInx].ToString()); } returnResultAsString(dtFixedLengthLine.OutputColumnData(), tableNumber); } } }//end method
/// <summary> /// Extracts data from DataTable into fixed length text format. /// </summary> /// <param name="tab">DataTable containing the data.</param> /// <param name="useLineTerminator">String containing one or more characters that will denote the end of a line of data.</param> /// <param name="columnNamesOnFirstLine">If true, a line containing the column names in delimited format will be the first line returned.</param> /// <param name="allowDataTruncation">If true, data longer than defined column length will be truncated; otherwise an exception will be thrown.</param> /// <param name="tableNumber">Arbitrary number used to identify the table.</param> /// <param name="lineTerminatorChars">Default is CR/LF for characters to mark end of line. You can override the default by setting this parameter.</param> private void ExtractFixedLengthDataFromTable(DataTable tab, bool useLineTerminator, bool columnNamesOnFirstLine, bool allowDataTruncation, int tableNumber, string lineTerminatorChars) { int rowInx = 0; int maxRowInx = -1; int colInx = 0; int maxColInx = -1; int colLen = -1; DataColumnCollection cols = tab.Columns; PFFixedLengthDataLine fixedLengthLine = new PFFixedLengthDataLine(cols.Count); maxRowInx = tab.Rows.Count - 1; maxColInx = tab.Columns.Count - 1; fixedLengthLine.UseLineTerminator = useLineTerminator; fixedLengthLine.LineTerminator = lineTerminatorChars; fixedLengthLine.ColumnNamesOnFirstLine = columnNamesOnFirstLine; fixedLengthLine.AllowDataTruncation = allowDataTruncation; fixedLengthLine.MaxColumnLengthOverride = this.MaxColumnLengthOverride; fixedLengthLine.DefaultStringColumnLength = this.DefaultStringColumnLength; for (colInx = 0; colInx <= maxColInx; colInx++) { if (PFFixedLengthDataLine.DataTypeIsNumeric(cols[colInx].DataType)) { colLen = PFFixedLengthDataLine.GetNumericTypeMaxExtractLength(cols[colInx].DataType); } else if (PFFixedLengthDataLine.DataTypeIsDateTime(cols[colInx].DataType)) { colLen = PFFixedLengthDataLine.GetDateTimeTypeMaxExtractLength(cols[colInx].DataType); } else { colLen = cols[colInx].MaxLength; } fixedLengthLine.SetColumnDefinition(colInx, cols[colInx].ColumnName, colLen); } if (columnNamesOnFirstLine == true) { if (returnResultAsString != null) { returnResultAsString(fixedLengthLine.OutputColumnNames(), tableNumber); } } for (rowInx = 0; rowInx <= maxRowInx; rowInx++) { DataRow row = tab.Rows[rowInx]; if (returnResultAsString != null) { for (colInx = 0; colInx <= maxColInx; colInx++) { fixedLengthLine.SetColumnData(colInx, row[colInx].ToString()); } returnResultAsString(fixedLengthLine.OutputColumnData(), tableNumber); } } }//end ExtractFixedLengthDataFromTable method