I am trying to take an existing SSIS package that imports an Excel file, transforms the data and exports as a fixed width text file and move it to a Windows application. I am able to import the file to SQL Server but I am not sure how to export the data as a fixed width file. I was able to use the following code to export to a tab delimited file but it must be a fixed width file for our vendor to read. I'm not sure if the code below can achieve this with a little modification or if a completely different approach is required. I would also be willing to do this in SQL server if anyone has a resource on how to do it using t-SQL. Thanks.
tab delimited export:
using (SqlCommand cmd = new SqlCommand("SELECT Col1, Col2, Col3, Col4, Col5, Col6 FROM myTable"))
{
using (SqlDataAdapter adapter = new SqlDataAdapter())
{
cmd.Connection = myDB;
adapter.SelectCommand = cmd;
using (DataTable mAdj = new DataTable())
{
adapter.Fill(mAdj);
string str = string.Empty;
str += "\r\n";
foreach (DataRow row in mAdj.Rows)
{
foreach (DataColumn column in mAdj.Columns)
{
str += row[column.ColumnName].ToString() + "\t\t";
}
str += "\r\n";
}
StreamWriter file = new StreamWriter(@"\\..\Export_files\myFile.txt");
file.WriteLine(str.ToString());
file.Close();
}
}
}