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

我是一个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"." 

错误的字段是作为文本stream([DT_TEXT])进来,由于我得到一个错误,无法在Unicode和非Unicode之间进行转换,我使用数据转换将其转换为Unicode文本stream([DT_NTEXT])

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

在这里输入图像说明

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

您应该考虑使用脚本组件执行此操作,请记住,在数据stream任务中,您无法直接进行debugging,但可以使用mbox剪切来检查结果。 另外请记住,excel将总是试图自动假设你的列数据types,例如,当你试图从excel中导入一个文件时,其中一列以数字开头,但在第3455行有一个字符,它会导入列作为一个数字,你将失去的char值,你会发现它在你的数据库为空。

我会给你一些代码来构build你需要编程的文件,也许它可以给你一个想法。 (这个例子读取一个文件作为一列,然后它将分裂,就像你select固定的分隔值在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)); } } } }