【问题标题】:Exporting SQL to Excel (xlsx) using SSIS?使用 SSIS 将 SQL 导出到 Excel (xlsx)?
【发布时间】:2014-11-24 10:12:14
【问题描述】:

我是 SSIS 新手(不到一周的经验)所以请多多包涵。
我正在运行一个存储过程以将其结果导出到 Excel 文件。

根据我的研究,我发现 SSIS 的 Excel 目标不能很好地处理 .xlsx 文件(不能是 xls,因为结果中的行数超过了 ~65K),但我发现我可以使用 OLE用于写入 excel 文件的 DB 目标。

我看到的问题是运行时出现的错误消息:

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])

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

任何帮助都会令人惊叹。谢谢。

【问题讨论】:

  • 我看到当文本数据超过 255 个字符时,JET 提供程序确实混淆了 SSIS/Excel——如果您使用 DT_NTEXT,则暗示会出现这种情况。尝试将长度强制为
  • @Greenspark 是的,其中一些字段是 VARCHAR(max)。其中一个在直接从 SQL 管理工作室中提取时有大约 80,000 个字符。所以一定会发生一些截断。我将测试 255 个字符并报告。
  • @Greenspark 好的,截断作品。现在我的问题是导出没有保留我的模板格式。
  • 你用的是什么模板,@npiani?
  • @Greenspark 我有一个 xlsx 文件,其中只有我设置了列格式的列标题。在运行时,我用模板替换我的文件并将数据插入其中。无论我做了什么,这些值的格式都是“常规”,而不是我在模板中设置的格式。

标签: sql-server excel unicode ssis


【解决方案1】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多