复制数据表中的表行会导致 Excel 输出报告中的数据类型不同吗?

发布于 2024-09-30 11:00:40 字数 892 浏览 1 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪 2024-10-07 11:00:40

我找到了一个解决方案,我对此并不满意,但它有效。

SELECT [ZipCode] = ' ' + ISNULL(TABLE.ZipCode,'')
FROM   TABLE
...

在线报告和 Excel 都将其解释为空白,并且不会扰乱我的格式。

I found a solution and I am not happy with it, but it works.

SELECT [ZipCode] = ' ' + ISNULL(TABLE.ZipCode,'')
FROM   TABLE
...

Both the online report and excel interpret it as an empty space and it doesn't mess with my formatting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文