這兩天在研究怎樣將DataGridView和listview 中的數據導出到Excel中
這兩個控件基本方法是一樣,只需要將 DGV(iY, iX) 改成listview.item即可
'把datagridview中的數據導出到excel
Public 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(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