积极答复者
想请指点我的代码是不是missing了,我convert 的excel file 有问题:(

问题
-
我篇写了将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
- 已编辑 christing 2020年3月16日 0:47
答案
-
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.- 已建议为答案 Xingyu ZhaoMicrosoft contingent staff, Moderator 2020年3月17日 2:02
- 已标记为答案 christing 2020年3月17日 5:31
全部回复
-
Hi christing,
根据我的测试,你可以考虑以下两种方案。
- 将文件扩展名从'xls'改成‘xlsx’。
- 打开注册表项,在‘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,
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.- 已编辑 Xingyu ZhaoMicrosoft contingent staff, Moderator 2020年3月16日 2:27
-
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.- 已建议为答案 Xingyu ZhaoMicrosoft contingent staff, Moderator 2020年3月17日 2:02
- 已标记为答案 christing 2020年3月17日 5:31