datagridview中的数据导出到excel过程
来源:互联网 发布:网络教学的优势是 编辑:程序博客网 时间:2024/05/13 08:29
这两天在研究怎样将DataGridView和listview 中的数据导出到Excel中
这两个控件基本方法是一样,只需要将 DGV(iY, iX) 改成listview.item即可
'把datagridview中的数据导出到excel
Public Sub dgExportToExcel()Sub dgExportToExcel(ByVal DGV As DataGridView)
Dim wapp As New Microsoft.Office.Interop.Excel.Application
Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
Dim wbook As Microsoft.Office.Interop.Excel.Workbook
'Dim wapp As New Excel.Application
'Dim wsheet As Excel.Worksheet
'Dim wbook As Excel.Workbook
On Error Resume Next
wapp.Visible = True
'打已有的excel模板文件
wbook = wapp.Workbooks.Open(System.IO.Directory.GetCurrentDirectory().ToString() & "HTXX.xlt")
'方式一:
'wsheet = wbook.ActiveSheet
'方式二:
wsheet = wbook.Worksheets("Sheet1")
Dim iX As Integer
Dim iY As Integer
Dim iC As Integer
'写DV控件头名称
'For iC = 0 To DGV.Columns.Count - 1
' wsheet.Cells(1, iC + 1).Value = DGV.Columns(iC).HeaderText
' wsheet.Cells(1, iC + 1).Font.Bold = True
'Next
'选择表格第三行
wsheet.Rows(3).select()
For iX = 0 To DGV.Rows.Count - 1 'DataGridView 总行数
wsheet.Cells(iX + 3, 1).value = (iX + 1).ToString
For iY = 0 To DGV.Columns.Count - 1
wsheet.Cells(iX + 3, iY + 2).value = DGV(iY, iX).Value.ToString
'wsheet.Cells(iX + 3, iY + 2).Borders(xlEdgeTop).
'.LineStyle = xlContinuous
'.Weight = xlThin
'.ColorIndex = xlAutomatic
Next
Next
'画表格线
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlDiagonalDown).LineStyle = XlLineStyle.xlLineStyleNone
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlDiagonalUp).LineStyle = XlLineStyle.xlLineStyleNone
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeLeft).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeLeft).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeTop).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeTop).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeBottom).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeRight).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeRight).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideVertical).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideVertical).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideHorizontal).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideHorizontal).ColorIndex = XlColorIndex.xlColorIndexAutomatic
'页脚处理
With wsheet.PageSetup
'.PaperSize = XlPaperSize.xlPaperA3
'.Orientation = XlPageOrientation.xlLandscape
'.LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" '& Gsmc
'.CenterHeader = "&""楷体_GB2312,常规""宋体,常规""" & strBT & Chr(10) & "&""楷体_GB2312,常规"""
'.RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"
.LeftFooter = "&""楷体_GB2312,常规""&10制表人:"
.CenterFooter = "&""楷体_GB2312,常规""&10制表日期:" & Now.Date.ToString
.RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"
End With
End Sub
Public Sub dgExportToExcel()Sub dgExportToExcel(ByVal DGV As DataGridView)
Dim wapp As New Microsoft.Office.Interop.Excel.Application
Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
Dim wbook As Microsoft.Office.Interop.Excel.Workbook
'Dim wapp As New Excel.Application
'Dim wsheet As Excel.Worksheet
'Dim wbook As Excel.Workbook
On Error Resume Next
wapp.Visible = True
'打已有的excel模板文件
wbook = wapp.Workbooks.Open(System.IO.Directory.GetCurrentDirectory().ToString() & "HTXX.xlt")
'方式一:
'wsheet = wbook.ActiveSheet
'方式二:
wsheet = wbook.Worksheets("Sheet1")
Dim iX As Integer
Dim iY As Integer
Dim iC As Integer
'写DV控件头名称
'For iC = 0 To DGV.Columns.Count - 1
' wsheet.Cells(1, iC + 1).Value = DGV.Columns(iC).HeaderText
' wsheet.Cells(1, iC + 1).Font.Bold = True
'Next
'选择表格第三行
wsheet.Rows(3).select()
For iX = 0 To DGV.Rows.Count - 1 'DataGridView 总行数
wsheet.Cells(iX + 3, 1).value = (iX + 1).ToString
For iY = 0 To DGV.Columns.Count - 1
wsheet.Cells(iX + 3, iY + 2).value = DGV(iY, iX).Value.ToString
'wsheet.Cells(iX + 3, iY + 2).Borders(xlEdgeTop).
'.LineStyle = xlContinuous
'.Weight = xlThin
'.ColorIndex = xlAutomatic
Next
Next
'画表格线
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlDiagonalDown).LineStyle = XlLineStyle.xlLineStyleNone
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlDiagonalUp).LineStyle = XlLineStyle.xlLineStyleNone
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeLeft).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeLeft).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeTop).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeTop).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeBottom).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeRight).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlEdgeRight).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideVertical).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideVertical).ColorIndex = XlColorIndex.xlColorIndexAutomatic
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideHorizontal).Weight = XlBorderWeight.xlThin
wsheet.Range(wsheet.Cells(3, 1), wsheet.Cells(DGV.RowCount - 1 + 3, DGV.ColumnCount + 1)).Borders(XlBordersIndex.xlInsideHorizontal).ColorIndex = XlColorIndex.xlColorIndexAutomatic
'页脚处理
With wsheet.PageSetup
'.PaperSize = XlPaperSize.xlPaperA3
'.Orientation = XlPageOrientation.xlLandscape
'.LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" '& Gsmc
'.CenterHeader = "&""楷体_GB2312,常规""宋体,常规""" & strBT & Chr(10) & "&""楷体_GB2312,常规"""
'.RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"
.LeftFooter = "&""楷体_GB2312,常规""&10制表人:"
.CenterFooter = "&""楷体_GB2312,常规""&10制表日期:" & Now.Date.ToString
.RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"
End With
End Sub
- datagridview中的数据导出到excel过程
- 将DataGridView中的数据导出到Excel
- DataGridView导出数据到Excel
- datagridview数据导出到excel
- DataGridView数据导出到Excel
- DataGridView导出数据到Excel
- 将DataGridView控件中的数据导出到Excel
- c# 实现将dataGridView中的数据导出到Excel
- DataGridview中的数据如何导出到Excel中
- DataGridview中的数据如何导出到Excel中
- VB.net 将dataGridView中的数据导出到excel
- 如何将datagridview中的数据导出到Excel中(转)
- 将DatagridView中的数据完美导出到Excel
- 如何将datagridview中的数据导出到Excel中
- 把DatagridView中的数据导出到Excel中
- 将DataGridVIew数据导出到Excel中的两种方法
- 将dataGridView中的数据导出到Excel中
- C# DataGridView 数据导出到Excel
- S3C2410:中斷異常介紹
- 我的资源链接
- 开始学习J2EE
- Ajax基础教程 (相当好,适合温习)
- 代码重构
- datagridview中的数据导出到excel过程
- 让人晕倒的9个英语句子
- 坚持十种饮食习惯可助延长寿命延缓衰老
- BitDefender:07年10大恶意软件和垃圾邮件排行
- SSD硬盘
- 未能加载文件或程序集“Com.JJHua.SQLServerDAL”或它的某一个依赖项。系统找不到指定的文件
- 串口通信(MFC)
- 初涉设计模式
- 使用ASP.NET 2.0 Profile存储用户信息_2[翻译]