"I hear and I forget. I see and I remember. I do and I understand."
Confucius
Jorge Novo Development Solutions,Ideas, and Crazy Thoughts.
Email: ETLDEVDBA(at)gmail.com
Tweeter: @ETLDEVDBA
April 17, 2013
Convert Excel Document into a PIPE Delimite File
After a few struggles "THANKS MICROSOFT.." I fix the code to be more stable
#region Help: Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
* a .Net application within the context of an Integration Services control flow.
*
* Expand the other regions which have "Help" prefixes for examples of specific ways to use
* Integration Services features within this script task. */
#endregion
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;
using InteropServices = System.Runtime.InteropServices;
using Encoding = System.Text.Encoding;
using System.Text;
using Microsoft.CSharp;
using System.Collections.Generic;
using System.IO;
#endregion
namespace ST_231969532bcc458c80ea76f87a8d6885
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region Help: Using Integration Services variables and parameters in a script
/* To use a variable in this script, first ensure that the variable has been added to
* either the list contained in the ReadOnlyVariables property or the list contained in
* the ReadWriteVariables property of this script task, according to whether or not your
* code needs to write to the variable. To add the variable, save this script, close this instance of
* Visual Studio, and update the ReadOnlyVariables and
* ReadWriteVariables properties in the Script Transformation Editor window.
* To use a parameter in this script, follow the same steps. Parameters are always read-only.
*
* Example of reading from a variable:
* DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
*
* Example of writing to a variable:
* Dts.Variables["User::myStringVariable"].Value = "new value";
*
* Example of reading from a package parameter:
* int batchId = (int) Dts.Variables["$Package::batchId"].Value;
*
* Example of reading from a project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].Value;
*
* Example of reading from a sensitive project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
* */
#endregion
#region Help: Firing Integration Services events from a script
/* This script task can fire events for logging purposes.
*
* Example of firing an error event:
* Dts.Events.FireError(18, "Process Values", "Bad value", "", 0);
*
* Example of firing an information event:
* Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, ref fireAgain)
*
* Example of firing a warning event:
* Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0);
* */
#endregion
#region Help: Using Integration Services connection managers in a script
/* Some types of connection managers can be used in this script task. See the topic
* "Working with Connection Managers Programatically" for details.
*
* Example of using an ADO.Net connection manager:
* object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
* SqlConnection myADONETConnection = (SqlConnection)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
*
* Example of using a File connection manager
* object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
* string filePath = (string)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
* */
#endregion
/// <summary>
/// This method is called when this script task executes in the control flow.
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
/// To open Help, press F1.
/// </summary>
public void Main()
{
// TODO: Add your code here
Variables vars = null;
try
{
Dts.VariableDispenser.LockForRead("User::strFullFilePath");
Dts.VariableDispenser.LockForRead("User::strArchiveFilePath");
Dts.VariableDispenser.LockForRead("User::strLoadFilePath");
Dts.VariableDispenser.GetVariables(ref vars);
String fullFilePath = vars["User::strFullFilePath"].Value.ToString();
String archiveFilePath = vars["User::strArchiveFilePath"].Value.ToString();
String loadFilePath = vars["User::strLoadFilePath"].Value.ToString();
String today = DateTime.Now.ToString("yyyyMMddHHmm");
Encoding ae = Encoding.GetEncoding("us-ascii", new EncoderReplacementFallback(""), new DecoderReplacementFallback(""));
int numberofEncodedBytes = 0;
FileInfo _FileInfo = new FileInfo(fullFilePath);
if (_FileInfo.Exists)
{
int RowMax = 0;
int ColMax = 0;
String NewFileName = _FileInfo.Name;
String FileName = NewFileName;
String FileType = ".Pipe";
NewFileName = NewFileName.Replace("13.1.xls", "");
CheckFileDestination(loadFilePath);
Excel._Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(fullFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Excel.XlPlatform.xlWindows,Type.Missing, false, false, 0, true, 1, 0);
/* Create list downhere for the first Row */
/* Find the last where len = 0 */
foreach (Excel.Worksheet xlWorkSheet in xlApp.Worksheets)
{
xlApp.Visible = false;
xlApp.ScreenUpdating = false;
xlApp.DisplayAlerts = false;
int cnt = 0;
for ( int y = 1; y <= xlWorkSheet.UsedRange.Columns.Count;y++)
{
if (xlWorkSheet.Columns.Cells[1, y].Text != "")
{
String Header = xlWorkSheet.Columns.Cells[1, y].Value.ToString();
cnt++;
}
else
{
break;
}
}
object[,] rangeData = (object[,])xlWorkSheet.UsedRange.Cells.get_Value(Type.Missing);
if (rangeData != null)
{
int ColumnCount = cnt;//xlWorkSheet.UsedRange.ListHeaderRows;
int RowCount = xlWorkSheet.UsedRange.Rows.Count;
RowMax = RowCount; //rangeData.GetUpperBound(0);
ColMax =ColumnCount;
using (StreamWriter writer_ = new StreamWriter(loadFilePath+NewFileName+"_"+xlWorkSheet.Name+FileType))
{
for (int iRow = 1; iRow <= RowMax; iRow++)
{
for (int iCol = 1; iCol <= ColMax; iCol++)
{
// String data = rangeData[iRow, iCol] != null ? rangeData[iRow, iCol].ToString() : String.Empty;
String data = Convert.ToString(rangeData[iRow, iCol]) ?? String.Empty;
String decodedString = "";
byte[] encodedBytes = new byte[ae.GetByteCount(data)];
numberofEncodedBytes = ae.GetBytes(data, 0, data.Length, encodedBytes, 0);
decodedString = ae.GetString(encodedBytes);
if (iCol != ColMax)
if (decodedString.Trim().Length > 0)
writer_.Write("^" + decodedString.Trim() + "^" + "|");
else
writer_.Write("|");
else
if (decodedString.Trim().Length > 0)
writer_.Write("^" + decodedString.Trim() + "^");
else
writer_.Write("");
encodedBytes = null;
numberofEncodedBytes = 0;
decodedString = null;
}
writer_.WriteLine();
}
}
}
//if (rangeData != null)
//{
// rangeData = null;
//}
//if (xlWorkBook != null)
//{
// xlWorkBook.Close(false, Type.Missing, Type.Missing);
// xlWorkBook = null;
//}
}
if (xlApp != null)
{
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
_FileInfo.MoveTo(archiveFilePath + _FileInfo.Name);
//* Move File to Archive *//
}
vars.Unlock();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception e)
{
Dts.Events.FireError(-1, "", "Error Converting Excel Document to Pipe:" + e.Message.ToString(), "", 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
private void CheckFileDestination(string FileDirectoryPath)
{
FileInfo _FileInfo = new FileInfo(FileDirectoryPath);
if (_FileInfo.Exists)
_FileInfo.Delete();
_FileInfo = null;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment