2014-09-29 218 views
4

我是SSIS noob(不到一周的经验),请耐心等待。
我正在运行一个存储过程将其结果导出到Excel文件。使用SSIS将SQL导出到Excel(xlsx)?

从我的研究中我发现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])

如果有帮助的所有,我的设置如下:

enter image description here

任何帮助将是惊人的。谢谢。

+1

我所看到的是Jet提供真正混淆SSIS/Excel中时文本数据超过255个字符 - 如果您使用DT_NTEXT,则暗示为这种情况。尝试将长度限制为<255作为测试,以确定这是否是您的问题。如果是,则必须决定是否需要生活在截断,或者可能只是出口到CSV或平面文件(我经常发现它是最好的路线) – Greenspark 2014-09-29 22:28:10

+1

@Greenspark是的,这些字段是VARCHAR(最大),其中一个从SQL直接拉管理工作室大约有80,000个字符,所以有些截断必然会发生,我会测试255个字符并报告回去。 – npiani 2014-09-29 22:46:27

+0

@Greenspark好吧,截断工程。现在我的问题是导出不保存我的模板格式。 – npiani 2014-10-03 16:07:34

回答

1

您应该考虑使用脚本组件执行此操作,请记住,在数据流任务中,您无法直接进行调试,但可以使用mbox剪切来检查结果。同时请记住,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)); 
       } 
     }   
    } 

}