复制数据表中的表行会导致 Excel 输出报告中的数据类型不同吗?
我从 SQL Server 中提取了一份报告,因为我不喜欢游标,所以我在代码隐藏文件中处理该表服务器端。因此,我提取了这份地址标签报告,我的客户希望每人有 X 个标签。所以我编写了这个函数:
Private Function ProcessX(ByVal dt As DataTable, ByVal X As Integer) As DataTable
Dim dtProcessed As DataTable = dt.Copy
dtProcessed.Clear()
For Each dr As DataRow In dt.Rows
For i As Integer = 0 To X - 1
Dim drHolder As DataRow = dtProcessed.NewRow
drHolder.ItemArray = dr.ItemArray
dtProcessed.Rows.Add(drHolder)
Next
Next
Return dtProcessed
End Function
该函数运行良好,并且比存储过程中的游标处理速度快得多。这个特定报告的问题是,当我将其导出到 Excel 时,它会截掉以 0 开头的邮政编码的前导 0。我还有在同一拉取中返回的 zip+4 和国际邮政编码。通常情况下,我只是将其归因于标准 Excel 如何处理前导 0 的故障,但是当我不通过上述函数处理报告时,Excel 不会截掉我的前导 0。
我单步执行了该函数,并且相关列的数据类型在该过程之前和之后是相同的,并且与原始表的数据类型匹配。我唯一能想到的可能是因为我传递的是数据表对象 ByVal 而不是 ByRef?我不知道我已经尝试了几乎所有其他方法,包括按值复制每个行项目并手动重新创建数据表。似乎没有什么可以改变结果。
I pull a report from SQL Server being not a fan of cursors I process that table server side in my code behind file. So I pull this report that is an address label report and my client wants there to be X number of labels per person. So I coded this function:
Private Function ProcessX(ByVal dt As DataTable, ByVal X As Integer) As DataTable
Dim dtProcessed As DataTable = dt.Copy
dtProcessed.Clear()
For Each dr As DataRow In dt.Rows
For i As Integer = 0 To X - 1
Dim drHolder As DataRow = dtProcessed.NewRow
drHolder.ItemArray = dr.ItemArray
dtProcessed.Rows.Add(drHolder)
Next
Next
Return dtProcessed
End Function
Which the function works beautifully and it processes much faster than a cursor in a sproc. The problem with this particular report is that when I export it to excel it chops off the leading 0s of zip codes that start with 0. I also have zip+4 and international zip codes that are returned in the same pull. Normally I would just chalk this up to a standard excel glitch on how it handles leading 0s, but when I don't process the report through the above function Excel does not chop off my leading 0s.
I stepped through the function and the datatype for the column in question is the same before and after the process and matches the datatype of the original table. The only thing I can think of is maybe because I am passing the datatable object ByVal instead of ByRef? I don't know I have tried almost everything else including copying each row item by value and recreating the datatable by hand. Nothing seems to change the outcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方案,我对此并不满意,但它有效。
在线报告和 Excel 都将其解释为空白,并且不会扰乱我的格式。
I found a solution and I am not happy with it, but it works.
Both the online report and excel interpret it as an empty space and it doesn't mess with my formatting.