'前期定义
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()
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()