在导出到Excel按钮中创建标题 [英] Create a header in a export to excel button
本文介绍了在导出到Excel按钮中创建标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我已经创建了一个导出到excel的按钮,用于导出我的datagrid,但是现在的问题是我想知道在创建excel时如何在excel的顶部添加标头
以下是我的代码
我尝试过的事情:
hi all i have created a export to excel button that exports my datagrid, but the question now is i want to know how to add a header on top of my excel when its created
below is my code
What I have tried:
'create empty string
Dim thecsvfile As String = String.Empty
'get the column headers
For Each column As DataGridViewColumn In Datagrid.Columns
thecsvfile = thecsvfile & column.HeaderText & ","
Next
'trim the last comma
thecsvfile = thecsvfile.TrimEnd(",")
'Add the line to the output
thecsvfile = thecsvfile & vbCr & vbLf
'get the rows
For Each row As DataGridViewRow In Datagrid.Rows
'get the cells
For Each cell As DataGridViewCell In row.Cells
thecsvfile = thecsvfile & cell.FormattedValue.replace(",", "") & ","
Next
'trim the last comma
thecsvfile = thecsvfile.TrimEnd(",")
'Add the line to the output
thecsvfile = thecsvfile & vbCr & vbLf
Next
'write the file
My.Computer.FileSystem.WriteAllText("C:\Inventory System\Inventory Datasheet.csv", thecsvfile, False)
End Sub
推荐答案
老实说,您的代码不会将datagridview的数据导出到Excel中.它会将数据以 csv格式 [将datagridview导出为excel列标题 [ ^ ]
To be honest, your code doesn''t export data of datagridview into Excel. It''s exporting data into text file in csv format[^].
If you want to export data into Excel file (*.xls or *.xlsx), check past answers: Export datagridview to excel With Column headers[^]
这篇关于在导出到Excel按钮中创建标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文