none
想请指点我的代码是不是missing了,我convert 的excel file 有问题:( RRS feed

  • 问题

  •  

    我篇写了将datagridview export 成 excel 的代码。

    我出现的问题是export 出来的excel file 有问题想请帮帮忙看看我的代码是不是出了什么问题感恩

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Dim xlapp As Microsoft.Office.Interop.Excel.Application
            Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
            Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
            Dim misValue As Object = System.Reflection.Missing.Value
            Dim i As Integer
            Dim j As Integer

            xlapp = New Microsoft.Office.Interop.Excel.Application
            xlWorkBook = xlApp.Workbooks.Add(misValue)
            xlWorkSheet = CType(xlWorkBook.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)

            For i = 0 To DataGridView1.RowCount - 2
                For j = 0 To DataGridView1.ColumnCount - 1
                    xlWorkSheet.Cells(i + 1, j + 1) = _
                        DataGridView1(j, i).Value.ToString()
                Next
            Next

            xlWorkSheet.SaveAs("D:\TEST.xls")
            xlWorkBook.Close()
            xlApp.Quit()

            releaseObject(xlApp)
            releaseObject(xlWorkBook)
            releaseObject(xlWorkSheet)

            MsgBox("You can find the file D:\TEST.xls")
        End Sub



    2020年3月14日 14:37

答案

  • Hi christing,

    这是我创建的ExtensionHardening的截图,你可以参考一下:

    尝试直接在Security下创建。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年3月16日 6:36
    版主

全部回复

  • Hi christing,

    根据我的测试,你可以考虑以下两种方案。

    1. 将文件扩展名从'xls'改成‘xlsx’。
    2. 打开注册表项,在‘HKEY_CURRENT_USER \ Software \ Microsoft \ Office \ 12(也有可能是15或16等更高数字,取决于安装的Excel版本) \ Excel \ Security’路径下新建一个32位DWORD类型的'ExtensionHardening',确保其值为0。

    关于造成此问题的原因,可见以下链接:

    Excel 2007 Extension Warning On Opening Excel Workbook from a Web Site

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2020年3月16日 2:26
    版主
  • @Xingyu Zhao

    感谢你的回复。

    Security’路径下新建一个32位DWORD类型

    意思是指create一个32bit  DWORD类型 (file)

    想问是在这里吗?

    \ Security’路径下新建一个32位DWORD类型的'ExtensionHardening',确保其值为0。

    其实,关于这段我不是很明白你所表达的

    不知道可否麻烦你show一张照片呢!pls...

    2020年3月16日 6:33
  • Hi christing,

    这是我创建的ExtensionHardening的截图,你可以参考一下:

    尝试直接在Security下创建。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年3月16日 6:36
    版主