vb.net 如何在vb.net中保存excel文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16536372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Save excel file in vb.net
提问by SoumitaP
Previously , I was trying to export gridview value into excel. but with the code given below i am able to export data into excel. but still not able to Save Automaticallythat excel file into a fixed folder suppose in C:/ drive. The code which i have written to export into excel is given below.
以前,我试图将 gridview 值导出到 excel。但是使用下面给出的代码,我可以将数据导出到 excel 中。但仍然无法将该 excel 文件自动保存到C:/ drive 中的固定文件夹中。下面给出了我编写的导出到 excel 的代码。
Private Sub ButtonExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExport.Click
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New Excel.Application
Try
Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
xlApp.Visible = True
rowsTotal = DataGridView1.RowCount - 1
colsTotal = DataGridView1.Columns.Count - 1
With excelWorksheet
.Cells.Select()
.Cells.Delete()
For iC = 0 To colsTotal
.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
Next
For I = 0 To rowsTotal - 1
For j = 0 To colsTotal
.Cells(I + 2, j + 1).value = DataGrid1.Rows(I).Cells(j).Value
Next j
Next I
.Rows("1:1").Font.FontStyle = "Bold"
.Rows("1:1").Font.Size = 10
.Cells.Columns.AutoFit()
.Cells.Select()
.Cells.EntireColumn.AutoFit()
.Cells(1, 1).Select()
End With
Catch ex As Exception
MsgBox("Export Excel Error " & ex.Message)
Finally
'RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
xlApp = Nothing
End Try
End Sub
can anybody over here please help me out from this problem that how to save that excel file automatically in VB.NET??
这里有人可以帮我解决这个问题,即如何在 VB.NET 中自动保存该 excel 文件?
回答by Adrien Lacroix
The SaveAs method is defined for Excel.Workbook
SaveAs 方法定义为 Excel.Workbook
At the end of your Try
, just before the Catch
, write :
在你的末尾Try
,就在 之前Catch
,写:
excelBook.SaveAs(<some path here>, etc...)
Refers to herefor more informations.
请参阅此处了解更多信息。
And to exit properly Excel, write in your Finally
block at the start :
要正确退出Excel,请Finally
在开始时写入块:
xlApp.Workbooks.Close()
xlApp.Quit()
回答by user3612833
i just used :
我刚用过:
Dim oexcel As Object
Dim obook As Object
Dim owrite As New Microsoft.Office.Interop.Excel.Worksheet
< your code >
owrite.SaveAs("c:\" & foldername)
回答by marcus
This is an older question but maybe this'll help someone still. I recently needed to read, parse and write xlsx files. I used the OpenXML SDK with C# for this purpose. MSDN provides some good tutorials on how to do this. If anyone has questions I can provide my code. Just one last note...when I "published" the app an installation of the SDK on the client's computer seemed to be required.
这是一个较旧的问题,但也许这仍然会帮助某人。我最近需要读取、解析和写入 xlsx 文件。为此,我使用了带有 C# 的 OpenXML SDK。MSDN 提供了一些关于如何做到这一点的很好的教程。如果有人有问题,我可以提供我的代码。最后一个说明……当我“发布”该应用程序时,似乎需要在客户端的计算机上安装 SDK。