简体   繁体   中英

How to load an XML Spreadsheet 2003(.xml) in to SQL server using ssis (file downloaded from web portal)

Struggling with an issue. Need to download a file from a webportal. Now i managed to do that using SSIS script task. Here comes the real issue.

Now when i download the file as .xls. Downloaded file appears to be Excel 97-2003 Worksheet but actually it isn't. It's an XML Spreadsheet 2003(.xml).

Now when i try to open this downloaded file, it comes up with an error message (Attached). So my SSIS package can't open the File as an Excel source and i can't even use XML source editor as some of the field values in file are not supported by xml editor so it throws an error message.

When i open the downloaded File and save it as a genuine .xls, xlsx or csv file. Then it works perfectly fine and package loads the file.

when i try to download it from Portal as a .csv or .txt, it downloads the file in strange format.

So i am stuck now, I have tried so many different approaches, but its not working. I have also tried "File Task System" functionality in ssis to rename the downloaded file. Even though file extension is changed but source editor still won't like the file format. Any help with would be appreciated.

在此处输入图片说明

I had this same issue. I used the following Excel Connection Manager connection string and it worked for me:


Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\MyXMLFile.xls;Extended Properties="EXCEL 12.0 XML;HDR=YES;";

Below is the solution for above issue. I ended up adding another script task in SSIS package that will change the downloaded xml to xls or csv.I then used "using Excel = Microsoft.Office.Interop.Excel;" from .NET assembly and added below C# script
public void Main() {

        Excel.Application excelapp = new Excel.Application();
        Excel.Workbook Datasource = (Excel.Workbook)excelapp.Workbooks.Add(1);          


        var DownloadPath = Dts.Variables["User::varDownloadPathNew"].Value.ToString();// C:\Test.xml Name of the source/downloaded file in .xml or .xls format
        var ConvertedPath = Dts.Variables["User::varConvertedFileName"].Value.ToString(); //C:\Test.csv Name of converted .xls or .csv file

        string FileName = DownloadPath;
       // var format = Excel.XlFileFormat.xlCSV;

        Datasource = excelapp.Workbooks.Open(FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

        //  Datasource.SaveAs(ConvertedPath, format, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
        // Excel.XlSaveConflictResolution.xlUserResolution, true,
        // Missing.Value, Missing.Value, Missing.Value);



     //   Datasource.SaveAs(ConvertedPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.Missing, true);

        Datasource.SaveAs(ConvertedPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.Missing, Type.Missing);

        //Clean
        Datasource.Close(true);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(Datasource);

        excelapp.Quit();
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelapp);


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM