导出到Excel没有标题 [英] Export to Excel has no headers

查看:112
本文介绍了导出到Excel没有标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TCP上找到了一个很棒的代码片段,其中介绍了如何将datagrid导出到excel文件,该代码很好用,只不过它不会导出标头.

I found a great code snippet here on TCP on how to export a datagrid to an excel file, the code works great except it will not export the headers.

Dim i, j As Integer
Dim xlApp As excel.Application
Dim xlWorkBook As excel.Workbook
Dim xlWorkSheet As excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
Dim ds As DataSet
ds = MultiExportDataSet
For i = 0 To ds.Tables(0).Rows.Count - 1
    For j = 0 To ds.Tables(0).Columns.Count - 1
        xlWorkSheet.Cells(i + 1, j + 1) = ds.Tables(0).Rows(i).Item(j)
    Next
Next
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Excel Files (*.xlsx)|*.xlsx"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
    xlSave = saveFileDialog1.FileName.ToString
End If

xlWorkSheet.SaveAs(xlSave)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)





有谁知道如何获取datagrid标头导出到excel文件?它们不必是excel工作表中的标题",它们可以是第一行值.





does anyone know how to get the datagrid headers to export into the excel file? they do not have to be the "Headers" in the excel sheet, they can be the first row values.

推荐答案

在遍历数据之前,请遍历列一次:

Before you loop through the data, loop through the columns once:

For i = 0 To ds.Tables(0).Columns.Count - 1
   dxlWorkSheet.Cells(1, i) = ds.Tables(0).Columns.ColumnName
Next



更改为数据编写的循环,使其在此下方开始一行.

更改此



Change the loop you wrote for the data to start one row below this.

Change This

<br />
xlWorkSheet.Cells(i + 1, j + 1) = ds.Tables(0).Rows(i).Item(j)<br />



到此



To This

<br />
xlWorkSheet.Cells(i + <big>2</big>, j + 1) = ds.Tables(0).Rows(i).Item(j)<br />


这篇关于导出到Excel没有标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆