vb.net操作EXCEL

  '前期定义
   Dim xlapp As Excel.Application
   Dim xlbook As Excel.Workbook
   Dim xlsheet As New Excel.Worksheet
   xlapp = CType(CreateObject("excel.application"),Excel.Application)
   xlbook = CType(xlapp.Workbooks.Open(Application.StartupPath& "文件名.xls"), Excel.Workbook)
   xlsheet = CType(xlbook.Worksheets("工作表名"),Excel.Worksheet)
  '获取EXCEL最后一行 xlsheet.UsedRange.Rows.Count
  '加边框
   xlsheet.Range("a14:v" &xlsheet.UsedRange.Rows.Count).Borders(1).Weight = 3
   xlsheet.Range("a14:v" &xlsheet.UsedRange.Rows.Count).Borders(2).Weight = 3
   xlsheet.Range("a14:v" &xlsheet.UsedRange.Rows.Count).Borders(3).Weight = 3
   xlsheet.Range("a14:v" &xlsheet.UsedRange.Rows.Count).Borders(4).Weight = 3
  '合并单元格
   xlsheet.Range("l" & v - n - 1 &":l" & v - 1).Merge()
  '居中对齐
   xlsheet.Range("e14:e" &xlsheet.UsedRange.Rows.Count).HorizontalAlignment =Excel.Constants.xlCenter
   xlsheet.Range("e14:e" &xlsheet.UsedRange.Rows.Count).VerticalAlignment =Excel.Constants.xlCenter
   xlsheet.Range("l14:l" &xlsheet.UsedRange.Rows.Count).HorizontalAlignment =Excel.Constants.xlCenter
   xlsheet.Range("l14:l" &xlsheet.UsedRange.Rows.Count).VerticalAlignment =Excel.Constants.xlCenter
  '调整行高
   xlsheet.Rows("14:" &xlsheet.UsedRange.Rows.Count).RowHeight = 40
  '调整字体大小
   xlsheet.Range("a14:v" &xlsheet.UsedRange.Rows.Count).Font.Size = 16
  '将数据下载成EXCEL表格式
   Dim cmd As New OleDbCommand("INSERT INTO [Sheet1$] IN '"& Application.StartupPath &"\temp\temp.xls' 'Excel 8.0;' SELECT * FROM t_master_spbm_temp",mdbcon)
   cmd.ExecuteNonQuery()
  '将EXCEL表当成数据库使用ado.net进行操作
   Dim excelconstring As String ="provider=microsoft.jet.oledb.4.0;data source=" &filename & "; extended properties=excel 8.0;"
   Dim excelcon As OleDbConnection = NewOleDbConnection(excelconstring)
   If excelcon.State = ConnectionState.Closed Thenexcelcon.Open()
   '获取excel表
   Dim tbl As DataTable =oledbconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,Nothing)
   '获取sheet名,其中(0)(1)...(n): 按名称排列的表单元素
   Dim tablename As String =datatable.Rows(0)(2).ToString().Trim()
   tablename = "[" & tablename.Replace(" ' ", " ")& "]"
   tablename = Mid(tablename, 2, Len(tablename) - 2)
   '利用sql语句从excel文件里获取数据
   Dim query As String = "select * from [" &tablename & "]" 
   Dim dt As New DataSet()
   Dim excelada As OleDbDataAdapter = New OleDbDataAdapter(query,excelcon)
   excelada.Fill(dataset, tablename)
   Dim excelrw As DataRow
   ……
   If excelcon.State = ConnectionState.open Thenexcelcon.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值