簡體   English   中英

使用SSIS將SQL導出到Excel(xlsx)?

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

我是SSIS noob(不到一周的經驗)所以請耐心等待。
我正在運行存儲過程以將其結果導出到Excel文件。

根據我的研究,我發現SSIS的Excel目標與.xlsx文件不兼容(不能是xls,因為我在結果中有超過65K的行),但我發現我可以使用OLE DB目的地寫入excel文件。

我看到的問題是運行時出現的錯誤消息:

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

錯誤的字段以文本流([DT_TEXT])形式出現,由於我無法在unicode和非unicode之間進行轉換,因此我使用數據轉換將其轉換為Unicode文本流([DT_NTEXT])

如果它有幫助,我的設置如下:

在此輸入圖像描述

任何幫助都會很棒。 謝謝。

您應該考慮使用腳本組件執行此操作,請記住,在數據流任務中,您無法直接調試,但可以使用mbox snipped來檢查結果。 還要記住,excel總是會嘗試自動設置列數據類型,例如當你嘗試從excel導入一個文件時,其中一個列以一個數字開頭,但是在行3455中有一個字符,它將導入如果列為數字,您將丟失char值,您將在數據庫中找到它為null。

我將給你一些代碼來編譯你需要的編程文件,也許它可以給你一個想法。 (此示例將文件作為一列讀取,然后它將拆分為好像您選擇在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