简体   繁体   English

使用SSIS将SQL导出到Excel(xlsx)?

[英]Exporting SQL to Excel (xlsx) using SSIS?

I'm an SSIS noob (less than a week experience) so please bear with me. 我是SSIS noob(不到一周的经验)所以请耐心等待。
I am running a stored procedure to export its result to an Excel file. 我正在运行存储过程以将其结果导出到Excel文件。

From my research I have found that SSIS's Excel Destination does not play nicely with .xlsx files (can't be xls since I have more than the ~65K rows in the result), but I found that I can use a OLE DB Destination to write to an excel file. 根据我的研究,我发现SSIS的Excel目标与.xlsx文件不兼容(不能是xls,因为我在结果中有超过65K的行),但我发现我可以使用OLE DB目的地写入excel文件。

The issue I am seeing is an error message that occurs on run that says: 我看到的问题是运行时出现的错误消息:

OLE DB Destination [212]] Error: 
An error occurred while setting up a binding for the "Main Job Notes" column. 
The binding status was "DT_NTEXT"."

The fields that are erroring are coming in as Text Streams ([DT_TEXT]), and since I was getting an error around not being able to convert between unicode and non-unicode, I use a Data Conversion to transform it into a Unicode text stream ([DT_NTEXT]) 错误的字段以文本流([DT_TEXT])形式出现,由于我无法在unicode和非unicode之间进行转换,因此我使用数据转换将其转换为Unicode文本流([DT_NTEXT])

If it helps at all, my setup is as follows: 如果它有帮助,我的设置如下:

在此输入图像描述

Any help would be amazing. 任何帮助都会很棒。 Thank you. 谢谢。

You should consider doing this using a script component, keep in mind that when in data flow task you cannot debug directly but you can use mbox snipped to check results. 您应该考虑使用脚本组件执行此操作,请记住,在数据流任务中,您无法直接调试,但可以使用mbox snipped来检查结果。 Also keep in mind that excel will always try to suppose your column data types automatically, for example when you try to import a file from excel that one of its columns starts with a number but in the row 3455 there's a character, it will import the column as a number and you will lose the char value, you will find it as null in your database. 还要记住,excel总是会尝试自动设置列数据类型,例如当你尝试从excel导入一个文件时,其中一个列以一个数字开头,但是在行3455中有一个字符,它将导入如果列为数字,您将丢失char值,您将在数据库中找到它为null。

I will give you some code to construct the file you need programmatically, maybe it can give you an idea. 我将给你一些代码来编译你需要的编程文件,也许它可以给你一个想法。 (This example reads a file as one column, then it will split in as if you chose fixed with delimited values in excel and will output in a csv file. (此示例将文件作为一列读取,然后它将拆分为好像您选择在Excel中使用分隔值固定并将在csv文件中输出。

/* Microsoft SQL Server Integration Services Script Component
*  Write scripts using Microsoft Visual C# 2008.
*  ScriptMain is the entry point class of the script.*/

using System;
using System.IO;
using System.Linq;
using System.Text;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

    #region Variables
    private string _jumexDailyData;
    private string[] _jumexValues;
    private string[] _jumexWidthValues;      
    #endregion

    /// <summary>
    /// Default constructor
    /// </summary>
    public ScriptMain()
    {        
        this._jumexValues = new string[22];        
    }

    public override void PreExecute()
    {
        base.PreExecute();
        /*
          Add your code here for preprocessing or remove if not needed
        */
    }

    public override void PostExecute()
    {
        base.PostExecute();
        /*
          Add your code here for postprocessing or remove if not needed
          You can set read/write variables here, for example:
          Variables.MyIntVar = 100
        */
    }

    public override void JumexDailyData_ProcessInput(JumexDailyDataBuffer Buffer)
    {        
        while (Buffer.NextRow())
            JumexDailyData_ProcessInputRow(Buffer);        
    }

    public override void JumexDailyData_ProcessInputRow(JumexDailyDataBuffer Row)
    {
        this._jumexDailyData = Row.JumexDailyData;
        if (this._jumexDailyData != null)
        {
            this._jumexWidthValues = this.Variables.JUMEXLOADSALESATTACHMENTFILEWIDTHVALUES.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            if (this._jumexWidthValues != null && this._jumexWidthValues.Count() > 0)
                for (int i = 0; i < this._jumexWidthValues.Count(); i++)
                {
                    this._jumexValues[i] = this._jumexDailyData.Substring(0, int.Parse(this._jumexWidthValues[i])).Trim();
                    this._jumexDailyData = this._jumexDailyData.Substring(int.Parse(this._jumexWidthValues[i]), (this._jumexDailyData.Length - int.Parse(this._jumexWidthValues[i])));
                }

            if (string.IsNullOrEmpty(this._jumexValues[3].Trim()) == false &&
                string.IsNullOrEmpty(this._jumexValues[17].Trim()) == false &&
                !this._jumexValues[3].Contains("---") &&
                !this._jumexValues[17].Contains("---") &&
                !this._jumexValues[3].Trim().ToUpper().Contains("FACTURA") &&
                !this._jumexValues[17].Trim().ToUpper().Contains("PEDIDO"))                
                using (StreamWriter streamWriter = new StreamWriter(this.Variables.JUMEXFULLQUALIFIEDLOADSALESATTACHMENTFILENAME.Replace(".TXT", ".CSV"), true, Encoding.Default))
                {
                    streamWriter.WriteLine(string.Join("|", this._jumexValues));
                }
        }        
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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