下面这段是我导出excel时用的:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '一个EXCEL工作表涉及数个对象所构成的阶层架构 '从APPLICATION到WORKBOOK再到worksheet. Dim ds As DataSet Dim dt As DataTable dt = objdataset.Tables("item_code") Dim excelapp As New Excel.Application Dim excelbook As Excel.Workbook = excelapp.Workbooks.Add Dim excelworksheet As Excel.Worksheet = _ CType(excelbook.Worksheets(1), Excel.Worksheet) '使EXCEL显示出来,以便让您看到所填入的数据与计算结果 excelapp.Visible = True With excelworksheet '设定栏标题与所需的格式 .Columns().ColumnWidth = 21.71 .Range("A1").Value = "订货单号" .Range("A1").Font.Bold = True .Range("B1").Value = "客户名称" .Range("B1").Font.Bold = True .Range("C1").Value = "商品编码" .Range("C1").Font.Bold = True .Range("D1").Value = "商品名称" .Range("D1").Font.Bold = True .Range("E1").Value = "商品英文名称" .Range("E1").Font.Bold = True .Range("F1").Value = "箱数" .Range("F1").Font.Bold = True .Range("G1").Value = "个数" .Range("G1").Font.Bold = True .Range("H1").Value = "规格" .Range("H1").Font.Bold = True .Range("I1").Value = "条码" .Range("I1").Font.Bold = True .Range("J1").Value = "发货日期" .Range("J1").Font.Bold = True
'从第二行(也就是栏标题栏之后)开始计算 Dim i As Integer = 2 '循环处理DATASET的ROWS集合并将每一列的数据写到EXCEL中的单元格. Dim dr As DataRow Dim nRowCount, nColCount As Integer 'For Each dr In dsmenu.tables(0).rows For nRowCount = 0 To dt.Rows.Count - 1 'dr = dt.Rows(i) dr = dt.Rows(nRowCount) .Range("A" & i.ToString).Value = dr("order_no") .Range("B" & i.ToString).Value = dr("customer_name_chi") .Range("C" & i.ToString).Value = dr("item_code") .Range("D" & i.ToString).Value = dr("description_chi") .Range("E" & i.ToString).Value = dr("description_eng") .Range("F" & i.ToString).Value = dr("pack") .Range("G" & i.ToString).Value = dr("qty") .Range("H" & i.ToString).Value = dr("qty_small") .Range("I" & i.ToString).Value = dr("barcode") .Range("J" & i.ToString).Value = dr("send_date") i += 1 Next End With End Sub
'一个EXCEL工作表涉及数个对象所构成的阶层架构
'从APPLICATION到WORKBOOK再到worksheet. Dim ds As DataSet
Dim dt As DataTable
dt = objdataset.Tables("item_code") Dim excelapp As New Excel.Application
Dim excelbook As Excel.Workbook = excelapp.Workbooks.Add
Dim excelworksheet As Excel.Worksheet = _
CType(excelbook.Worksheets(1), Excel.Worksheet) '使EXCEL显示出来,以便让您看到所填入的数据与计算结果
excelapp.Visible = True With excelworksheet
'设定栏标题与所需的格式
.Columns().ColumnWidth = 21.71
.Range("A1").Value = "订货单号"
.Range("A1").Font.Bold = True
.Range("B1").Value = "客户名称"
.Range("B1").Font.Bold = True
.Range("C1").Value = "商品编码"
.Range("C1").Font.Bold = True
.Range("D1").Value = "商品名称"
.Range("D1").Font.Bold = True
.Range("E1").Value = "商品英文名称"
.Range("E1").Font.Bold = True
.Range("F1").Value = "箱数"
.Range("F1").Font.Bold = True
.Range("G1").Value = "个数"
.Range("G1").Font.Bold = True
.Range("H1").Value = "规格"
.Range("H1").Font.Bold = True
.Range("I1").Value = "条码"
.Range("I1").Font.Bold = True
.Range("J1").Value = "发货日期"
.Range("J1").Font.Bold = True
'从第二行(也就是栏标题栏之后)开始计算
Dim i As Integer = 2
'循环处理DATASET的ROWS集合并将每一列的数据写到EXCEL中的单元格.
Dim dr As DataRow
Dim nRowCount, nColCount As Integer 'For Each dr In dsmenu.tables(0).rows
For nRowCount = 0 To dt.Rows.Count - 1
'dr = dt.Rows(i)
dr = dt.Rows(nRowCount)
.Range("A" & i.ToString).Value = dr("order_no")
.Range("B" & i.ToString).Value = dr("customer_name_chi")
.Range("C" & i.ToString).Value = dr("item_code")
.Range("D" & i.ToString).Value = dr("description_chi")
.Range("E" & i.ToString).Value = dr("description_eng")
.Range("F" & i.ToString).Value = dr("pack")
.Range("G" & i.ToString).Value = dr("qty")
.Range("H" & i.ToString).Value = dr("qty_small")
.Range("I" & i.ToString).Value = dr("barcode")
.Range("J" & i.ToString).Value = dr("send_date") i += 1
Next End With
End Sub