甜老师:
第二个问题解决了。谢谢!
但第一个问题没有解决,按你修改的命令,导出的结果见附件,但我想要的效果是第一列的(日期列)的格式要和其它列一样,显示日期格式并居中、带边框。
按钮命令如下:
Dim dt As Table = Tables("车辆状态信息_table1")
Dim nms() As String = {"日期","代号","车型","VIN码","发动机号","室颜色","状态","检验员"} '要导出的列名
Dim caps() As String = {"日期","代号","车型","VIN码","发动机号","驾驶室颜色","状态","数据上报人"}'对应的列标题
Dim szs() As Integer = {90,60,100,150,100,90,60,90} '对应的列宽
Dim Book As New XLS.Book '定义一个Excel工作簿
Book.Sheets.Add("当日报工信息")'增加一个工作表
Dim Sheet As XLS.Sheet = Book.Sheets("当日报工信息") '引用工作簿的第一个工作表
Dim st As XLS.Style = Book.NewStyle '日期列的显示格式
st.Format = "yyyy-MM-dd"
Dim Style As XLS.Style = Book.NewStyle() '定义新样式
Style.BorderTop = XLS.LineStyleEnum.Thin
Style.BorderBottom = XLS.LineStyleEnum.Thin
Style.BorderLeft = XLS.LineStyleEnum.Thin
Style.BorderRight = XLS.LineStyleEnum.Thin
Style.BorderColorTop = Color.Black
Style.BorderColorBottom = Color.Black
Style.BorderColorLeft = Color.Black
Style.BorderColorRight = Color.Black
Style.AlignHorz = XLS.AlignHorzEnum.Center '水平居中
Style.AlignVert = XLS.AlignVertEnum.Center '垂直居中
Sheet(0,0).Value = "当日报工信息" '定义表头信息
Sheet(0,0).Style = Style '表头信息采用新样式:居中
Sheet.MergeCell(0,0,1,8) '表头信息从第一行第一列开始合并一行八列
Sheet.Rows.Frozen = 2 '冻结前二行
For c As Integer = 0 To nms.length -1
If dt.Cols(nms(c)).IsDate Then '如果是日期列
Sheet.Cols(c).Style = st '设置显示格式
End If
Sheet(1, c).Value = caps(c) '指定列标题
Sheet(1,c).Style = Style '列标题开始调用新样式:居中
Sheet.Cols(c).Width = szs(c) '指定列宽
Next
For r As Integer = 0 To dt.Rows.Count - 1 '填入数据
Sheet.Rows(r).Height = 30 '设置所有行的行高
For c As Integer = 0 To nms.length -1
If dt.cols(nms(c)).isdate Then
Sheet(r + 2, c).Style = st '从第三行第一列开始调用新样式:居中,带边框
Sheet(r + 2, c).Value = dt.rows(r)(nms(c))
Else
Sheet(r + 2, c).Style = Style '从第三行第一列开始调用新样式:居中,带边框
Sheet(r + 2, c).Value = dt.rows(r)(nms(c))
End If
Next
Next
Book.Sheets.RemoveAt(0) '删除第一个工作表
Book.Save("D:\质量管理系统\报工\车辆报工信息.xls")
Dim Proc As New Process
Proc.File = "D:\质量管理系统\报工\车辆报工信息.xls"
Proc.Start()
请多费心!