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

ぃ、小莉子 提交于 2019-12-11 17:32:27

问题


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.


回答1:


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;";



回答2:


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;
    }


来源:https://stackoverflow.com/questions/49046681/how-to-load-an-xml-spreadsheet-2003-xml-in-to-sql-server-using-ssis-file-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!