How to convert Excel file into CSV within script task

sujith kumar matharasi 351 Reputation points
2021-04-19T17:08:08.407+00:00

Hello All,

I have a script task which will basically pick up the latest file from the folder and then pass it to the "Data Flow Task" where it will be loaded into the SQL Server, this process runs fine on visual studio however it fails on SQL agent because of the missing drivers. The DBA team is working on it however i was wondering if i can convert my picked up latest excel file into a CSV ? Because the csv automation on Agent runs fine.

Below is the code i have currently in Script task, Can someone please help me with this?

region Namespaces

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;

endregion

namespace ST_9742f4376d5c4b3da8923e028d63a7a8
{

[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// TODO: Add your code here
var directory = new DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());

        FileInfo[] files = directory.GetFiles("*LC_-_State*");
        DateTime lastModified = DateTime.MinValue;

        foreach (FileInfo file in files)
        {
            if (file.LastWriteTime > lastModified)
            {
                lastModified = file.LastWriteTime;
                Dts.Variables["User::VarFileName"].Value = file.ToString();
            }
        }

        //MessageBox.Show(Dts.Variables["User::VarFileName"].Value.ToString());

        Dts.TaskResult = (int)ScriptResults.Success;
    }

    #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

}

}

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,705 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. ZoeHui-MSFT 41,496 Reputation points
    2021-04-20T07:44:44.877+00:00

    Hi @sujith kumar matharasi ,

    Have you installed the Microsoft.ACE.OLEDB.12.0 Provider for Both 64-bit and 32-bit Processing?

    You may install the provider for a try.

    https://datasavvy.me/2017/07/20/installing-the-microsoft-ace-oledb-12-0-provider-for-both-64-bit-and-32-bit-processing/

    https://www.microsoft.com/en-us/download/details.aspx?id=13255

    Also here are two articles about converting-excel-files-into-csv-and-uploading-files-using-ssis & ssis-how-to-convert-excel-file-to-csv.html.

    Just for your reference.

    Regards,

    Zoe


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues October

    1 person found this answer helpful.
    0 comments No comments

  2. Yitzhak Khabinsky 26,586 Reputation points
    2021-04-19T17:49:00.947+00:00

    Hi @sujith kumar matharasi ,

    It is better to stay with the existing DFT to process MS Excel file.

    Though here is how to retrieve a latest fully qualified file name in the directory based on its datetime attribute. There is no need in any loop, like your current implementation is doing.

    c#

    void Main()  
    {  
     var directory = new DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());  
     FileInfo file = directory.GetFiles("*LC_-_State*")  
     .OrderByDescending(d => d.LastWriteTime)  
     .FirstOrDefault();  
      
     Dts.Variables["User::VarFileName"].Value = file.FullName;  
    }  
    
    0 comments No comments

  3. sujith kumar matharasi 351 Reputation points
    2021-04-19T18:34:44.683+00:00

    Hi @Yitzhak Khabinsky ,

    My only concern is i cannot automate the excel file through SQL agent so i need to convert it to csv. My current code already picks up the latest excel file its just i need to add something in here which will convert that picked up file to csv.

    Thanks


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.