Cannot import long text from Excel to SQL Server using SSIS - sql-server

Environment:
Microsoft® Excel® for Microsoft 365 MSO (Version 2112 Build 16.0.14729.20254) 64-bit
Microsoft SQL Server 2019 (RTM-CU14) (KB5007182) - 15.0.4188.2 (X64)
Microsoft Visual Studio 2019
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Office\16.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows set to 0
In Excel I formated the cell as "Text" and I also filled up the 2nd and 3rd rows with some super-long dummy text... (1st row is title row)
When I go to Excel Source/Advanced editor, I can set the Output to Unicode text stream [DT_NTEXT] or anything else but the External column cannot be changed to something else than Unicode string [DT_WSTR] (255 characters) despite the setting in registry that normally should allow it and despite having super-long strings in first two rows (other than the 1st row which holds the field names)
Then of course when I try to execute the SSIS task it throws a normal truncation error.
Question: What I am doing wrong or what else should be done here to actually be able to import the data? By the way, this is supposed to be automated at some point.

Since an Excel workbook is not a database, the OLE DB provider tries to detect the most relevant metadata from the Excel worksheet and read it as tabular data, which is mostly inaccurate when handling medium and large Excel files. After spending years on creating SSIS packages, I will convert the Excel file to a CSV file and import it using a Flat File Connection Manager instead. Or I will use a C# script to import the data.
I. Converting Excel to CSV
You can automate the process of converting Excel to CSV using a C# script:
Converting XLSX file using to a CSV file
Convert .xlsx & .xls to .csv
How to Convert Excel to CSV using Interop
After converting the Excel file to a CSV file, you can dynamically import it using a Flat File Connection Manager:
Dynamic Flat File Connections in SQL Server Integration Services
II. Using a C# script
It is good to check the following class, which is a part of the SchemaMapper project:
SchemaMapper - MsExcelImport.cs
Besides, a step-by-step guide on how to use this library can be found in the following link:
Import data from multiple files into one SQL table step by step guide
III. Editing the Excel connection string
If you don't have the choice to convert Excel to flat files, then you can force the Excel connection manager to ignore headers from the first row by adding IMEX=1 to tell the OLEDB provider to specify data types from the first row (which is the header - all string most of the time).
To edit the connectionstring property, click on the Excel Connection Manager and press on the F4 key. In the Properties Tab, you can edit the connectionstring property.
SSIS Excel Import Columns with More or Less than 255 Characters
IV. Changing columns length from advanced editor
Try changing the Excel Source column metadata from the advanced editor:
In SSIS excel datasource not taking more characters than 255
Importing Excel using SSIS may cause a headache! You can check the following question:
Workaround for exporting data to Excel with more than 255 columns
Dynamically Creating Excel table through SSIS
SQL Server Import Wizard doesn't support importing from excel sheet with more than 255 columns
Importing Excel Data Seems to Randomly Give Null Values
Failing to read String value from an excel column
Importing Excel Data Seems to Randomly Give Null Values
SSIS - Excel data shows as scientific notations and Null Values

Related

SSIS Export 1 table to multiple Excel sheets

I want to export a large table from SQL Server to excel using SSIS. I have a col called Num that I want all the same values into the same excel sheet e.g. all the 1s in 1 sheet etc. How to do this with SSIS? Do I write a SQL command text?
Num
--
1
1
2
2
2
3
4
4
Export data from SQL Server to Excel and Excel file via using SSIS package :
Configure ADO.NET connection :
To create a “Source” connection, right click “Connection Managers” and select “New ADO.NET Connection”.
a. Server Name – is the hostname or IP address of the database server where SQL is installed.
b. Authentication – is an “authentication type” used to connect Server that is selected on “Server Name” drop-down box. It can be either SQL Authentication or Windows Authentication.
c. Database Name – is the name of the database which we are going to use for data extracting.
Configure ADO.NET Source :
To populate data from SQL Server table, you must configure ADO.Net source component.
Double-click on “Data flow task” to do that. The window “Configure Data flow task” will open. Alternatively, you can select “Data Flow” that is located next to “Control Flow”. As we already configured SQL connection, we will drag and drop ADO.NET Source to data flow window.
Then select “SQL Command”, it will open a multi-line text box where we can write the Ad-Hoc query to extract data from the database.
Configure excel destination
Once we configure ADO.NET source, we will configure Excel destination.
In SSIS tool-box, drag and drop excel destination in the data-flow window and rename it as “Sales Excel File”.
a. Excel connection manager – used to connect the excel file.
b. Data access method. Excel worksheet is considered as a table or view. If you select “Table or View”, drop down box “Name of excel sheet” will be filled with all worksheets that are created in the excel workbook.
c. Name of excel sheet. By default, this drop-down box is disabled. It will be enabled automatically when we select “Table or View” as data access mode. We also can select appropriate excel worksheet.
Then, choose :
a. Excel File Path – the location of the excel file.
b. Excel version – can select excel version from drop down box.
In order to export data from SQL table to excel file, you must map the fields of SQL table and Excel file.
To do that, drag blue arrow beneath the ADO.NET component and drop it on Excel Destination component.
Double click on “Excel destination” to map the fields. “Excel destination editor” will open. Select “Mappings” option from the left pane of the dialog box. In mapping, input columns will be the columns of SQL Query output and destination columns will be the columns of the excel file. It is suggested to keep the same name of SQL Query output and excel columns. This will make mapping much easier because we do not need to map fields manually. SSIS will map columns automatically.

SSIS Excel Destination is Empty

I'm having an issue exporting a large dataset (500k+) to Excel via SSIS, where the output file ends up with 0 rows exported. Before saying that I shouldn't be exporting that many records to Excel, let me state that I know and normally wouldn't. Accounting does not want a CSV and is unwilling to open a CSV in Excel.
Using Visual Studio 2012 SSDT, here are the components involved.
Execute SQL Task -> Creates the empty file with headers
Data Flow Task ->
OLE DB Source -> SQL Query
Excel Destination
While the package is running, you can see records flowing from the source to the destination. The package completes without error, but when you open the file, it's empty. The only thing in there is the header.
If I select the Top 1000 records and export to Excel, it works as intended.
Some things I've tried:
Export to Excel on the network
Export to Excel locally
Export to CSV to Excel on both network and locally
Export to Ole DB Destination using Office Access Database Engine 12.0 with "Excel 12.0" extended properties.
Tried running as different users
All with the same outcome.
Can anyone provide any insight into why this may be happening and how to proceed?
We experienced a similar behaviour, when runnig the ETL in a SQL Server Agent job. Debugging it in Visual Studio, worked, however. So I do not know, whether this solution applies to you.
The reason was that the user, under which the package ran, did not have access to C:\users\Default.
I found this out by using sysinternals process monitor.
I was inspired by that post: Empty Excel File permissions issue: SSIS Excel Destination buffers large record sets through C:\Users\Default
[I explained my search for the bug in my blog: https://www.csopro.de/biblog/2018/04/ssis-fehlerbehebung-bei-excel-destination-schreibt-keine-zeilen/ Unfortunately it is in German]

Workaround for exporting data to Excel with more than 255 columns

SSMS and SSRS to Excel enable for more than 255 columns when copy-pasting.
SSIS does not allow for more than 255 columns to be exported to Excel 2007.
Is there a way to override this?
Problem
There are a lot of Limitations when exporting to an Excel Files using Sql server data tools
Workarounds
You can do some workaround to achieve this:
Create a dataflowtask that export your data into a FlatFile (csv)
Store your Destination FileName in a Variable
Create another Dataflowtask that convert your csv file to an Excel File using a script task with a similar Function
Note: you have to add Microsoft.Office.Interop.Excel.dll file to the following directories (.Net Framework dll directory) C:\Windows\Microsoft.NET\Framework\v2.0.50727 and (sql server data tools dll directory) C:\Program Files\Microsoft SQL Server\100\DTS\Binn (using vs 2005 and sql 2008) and then add this dll as a reference in your script task
Imports Microsoft.Office.Interop
Public Sub ConvertCSVToExcel(Fromcsv As String, Toxlsx As String)
Dim Exl As New Excel.Application()
Try
Dim wb1 As Excel.Workbook = Exl.Workbooks.Open(Fromcsv, Format:=4)
wb1.SaveAs(Toxlsx, FileFormat:=XlFileFormat.xlOpenXMLWorkbook)
wb1.Close()
Exl.Quit()
Catch ex As Exception
Exl.DisplayAlerts = False
Exl.Quit()
End Try
End Sub
Third party components
Or you have to use a third party components like cozyRoc SSIS+
Side Note
if you are looking to Import data from excel with more than 255 columns you can follow this Link
References
Third party components
SSIS: Export more than 255 columns from SQL table to Exce
Cozyroc website
Workaround
convert csv to xlsx
trying to use custom assembly with script task in SSIS 2008 - can't find correct version of GACUtil
Refer the link. Best would be to create an script in SSIS to copy the content as csv format. You can use c# or VB.Net.
just for anyone come to this page and use SQL Server Import and Export Wizard to export to excel
in tab Review Data Type Mapping
section Data Type Mapping: ==> double click your column that has length more than 255 char open its Column Conversion Details Dialog then close it
then next to end...Lool

SSIS Excel Connection String Extended Properties: HDR=YES; Appears Twice

I am able to execute my SSIS via a batch file but I am unable to execute it in Visual Studio 2010. We are using this for our scheduled jobs so it is fine.
I am extracting records from SQL Server and generate to an Excel document. I am using a DTS Configuration File when I execute the batch file thus there are no errors.
However, I would like to run this in Visual Studio as well.
This is because the Excel Connection String in Visual Studio is as follows:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xxx.xls;Extended Properties=HDR=YES;EXCEL 8.0;HDR=YES;
May I know why does HDR=YES appears twice and how do I to solve the issue above?
Source
Connecting to Excel
The Microsoft Jet provider is used to connect to an Excel workbook. In the following connection string, the Extended Properties keyword sets properties that are specific to Excel. "HDR=Yes;" indicates that the first row contains column names, not data, and "IMEX=1;" tells the driver to always read "intermixed" data columns as text.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""
Note that the double quotation character required for the Extended Properties must also be enclosed in double quotation marks.

Import DATA to SQL Server from CSV file

I have a CSV file with 10 columns. I want to import to SQL Server table with only 5 columns using powershell. Can anyone help in this ?
I suggest that Change your excel file to 5 sight column, and then using SQL Server Import\Export wizard or SSIS (if your convert is each day periodic) or PowerShell to convert your data.
You can you following reference in order to use PowerShell to convert your file:
SQL SERVER – Powershell – Importing CSV File Into Database

Resources