VB.NET中 Excel 输出。

xiao850404 2009-12-19 05:58:39
Excel 输出时,如何将光标焦点,移到初始默认位置(例:cell(1,1))。
...全文
208 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
elvafans_hehe 2010-07-25
  • 打赏
  • 举报
回复
我也备用一下
xiao850404 2009-12-19
  • 打赏
  • 举报
回复
谢谢,大家的帮助。恩不错,问题解决了!
a854468521 2009-12-19
  • 打赏
  • 举报
回复
添加EXCEL的引用后:
Range("a2").Select'激活指定单元。
破折号 2009-12-19
  • 打赏
  • 举报
回复
给你看个以前做的
参考下吧

Imports System.Windows.Forms
Imports System.IO

Public Class OutputExcel
Dim excelApp As New Microsoft.Office.Interop.Excel.Application
Dim excelBook As Microsoft.Office.Interop.Excel.Workbook = excelApp.Workbooks.Open(System.Windows.Forms.Application.StartupPath & "\Mode.xls")
Dim excelWorksheet As Microsoft.Office.Interop.Excel.Worksheet
Dim SqlObject As New SQL_Conn
Dim SqlTable As DataTable
Dim ExcelSaveFileDia As New SaveFileDialog

Public Sub OutputSalaryView()
excelWorksheet = excelBook.Sheets.Item(1)
Try
SqlTable = SqlObject.SQL_Ada("select * from Salary_In_All")
With excelWorksheet
.Range(.Cells(1, 1), .Cells(1, SqlTable.Columns.Count)).Merge()
.Range(.Cells(2, 1), .Cells(2, SqlTable.Columns.Count)).Merge()
.Range("A1").Value = Now.Month.ToString + " 月工资表"
.Range("A1").Font.Size = 17
.Range("A1").Font.Name = "黑体"
.Range("A2").Value = Now.ToString + " 制"
For i = 0 To SqlTable.Columns.Count - 1
.Cells(3, i + 1) = SqlTable.Columns.Item(i).ColumnName.ToString
Next i
For i = 0 To SqlTable.Rows.Count - 1
For j = 0 To SqlTable.Columns.Count - 1
.Cells(i + 4, j + 1) = SqlTable.Rows(i).Item(j).ToString
Next j
Next i
End With

ExcelSaveFileDia.InitialDirectory = Application.StartupPath
ExcelSaveFileDia.Filter = "Excel表格(*.xls)|*.xls"
ExcelSaveFileDia.OverwritePrompt = True
ExcelSaveFileDia.ShowDialog()
excelBook.SaveAs(ExcelSaveFileDia.FileName)
Catch ex As Exception
Finally
excelBook.Close()
excelApp.Quit()
excelWorksheet = Nothing
excelBook = Nothing
excelApp = Nothing
For Each pro In Process.GetProcesses
If pro.ProcessName = "EXCEL" Then
pro.Kill()
End If
Next
End Try
End Sub

Public Sub OutputInfoView()
excelWorksheet = excelBook.Sheets.Item(2)
Try
SqlTable = SqlObject.SQL_Ada("exec selectinfo ''")
With excelWorksheet
.Range(.Cells(1, 1), .Cells(1, SqlTable.Columns.Count)).Merge()
.Range(.Cells(2, 1), .Cells(2, SqlTable.Columns.Count)).Merge()
.Range("A1").Value = "公司人员信息一览表"
.Range("A1").Font.Size = 17
.Range("A1").Font.Name = "黑体"
.Range("A2").Value = Now.ToString + " 制"
For i = 0 To SqlTable.Columns.Count - 1
.Cells(3, i + 1) = SqlTable.Columns.Item(i).ColumnName.ToString
Next i
For i = 0 To SqlTable.Rows.Count - 1
For j = 0 To SqlTable.Columns.Count - 1
.Cells(i + 4, j + 1) = SqlTable.Rows(i).Item(j).ToString
Next j
Next i
End With
excelWorksheet.Move(excelBook.Sheets.Item(1))
ExcelSaveFileDia.InitialDirectory = Application.StartupPath
ExcelSaveFileDia.Filter = "Excel表格(*.xls)|*.xls"
ExcelSaveFileDia.OverwritePrompt = True
ExcelSaveFileDia.ShowDialog()
excelBook.SaveAs(ExcelSaveFileDia.FileName)
Catch ex As Exception
Finally
excelBook.Close()
excelApp.Quit()
excelWorksheet = Nothing
excelBook = Nothing
excelApp = Nothing
Dim pro As Process
For Each pro In Process.GetProcesses
If pro.ProcessName = "EXCEL" Then
pro.Kill()
End If
Next
End Try
End Sub


Public Sub OutputVacationView()
excelWorksheet = excelBook.Sheets.Item(3)
Try
SqlTable = SqlObject.SQL_Ada("exec selectvacation '',0")
With excelWorksheet
.Range(.Cells(1, 1), .Cells(1, SqlTable.Columns.Count)).Merge()
.Range(.Cells(2, 1), .Cells(2, SqlTable.Columns.Count)).Merge()
.Range("A1").Value = Now.Month.ToString + " 月加班请假情况表"
.Range("A1").Font.Size = 17
.Range("A1").Font.Name = "黑体"
.Range("A2").Value = Now.ToString + " 制"
For i = 0 To SqlTable.Columns.Count - 1
.Cells(3, i + 1) = SqlTable.Columns.Item(i).ColumnName.ToString
Next i
For i = 0 To SqlTable.Rows.Count - 1
For j = 0 To SqlTable.Columns.Count - 1
.Cells(i + 4, j + 1) = SqlTable.Rows(i).Item(j).ToString
Next j
Next i
.Range(.Cells(1, 7), .Cells(SqlTable.Rows.Count + 3, 7)).Delete()
End With
excelWorksheet.Move(excelBook.Sheets.Item(1))
ExcelSaveFileDia.InitialDirectory = Application.StartupPath
ExcelSaveFileDia.Filter = "Excel表格(*.xls)|*.xls"
ExcelSaveFileDia.OverwritePrompt = True
ExcelSaveFileDia.ShowDialog()
excelBook.SaveAs(ExcelSaveFileDia.FileName)
Catch ex As Exception
Finally
excelBook.Close()
excelApp.Quit()
excelWorksheet = Nothing
excelBook = Nothing
excelApp = Nothing
For Each pro In Process.GetProcesses
If pro.ProcessName = "EXCEL" Then
pro.Kill()
End If
Next
End Try
End Sub

End Class

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧