导出数据到excel

Protected Sub cmdOrderExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdOrderExport.Click
Try
Me.BuildCondition()
Dim ht As Hashtable = APICurrent.API.Dynamic.Trade.searchGoodsStockInfo(objCondition)
'总件数

Dim allCount As Integer = CType(ht.Item(MCO010.CONST_DATACOUNT), Integer)
Dim ds As DataTable = CType(ht.Item(MCO010.CONST_DATA), DataTable)

' Excel类

Dim orderListPrinter As New GoodsStockListPrint(Me.GetExcelTemplatePath() & "GoodsStockList.xlt")
orderListPrinter.DataSource = New Object() {ds}
Me.ExportExcel(orderListPrinter)
Catch ex As Exception
LogManager.Debug(ex.ToString)
Me.Controls.Add(New LiteralControl("<SCRIPT>alert('" + MCO003.getNameByResID(MCO005.IMSG011) + "');</SCRIPT>"))
End Try
End Sub

Public Class GoodsStockListPrint
Inherits ExcelComm

'列数
ReadOnly COL_CNT As Integer = 18

'列的索引
Private Enum Enum_Col As Integer
ICOL_StockID = 0 '编号
ICOL_GoodSID = 1 '编号
ICOL_GoodsName = 2 '名称
ICOL_StoreID = 3 '店铺编号
ICOL_StoreName = 4 '店铺
ICOL_CompanyID = 5 '编号
ICOL_CompanyName = 6 '经销商

ICOL_SpecNo1 = 7 '规格一编号
ICOL_SpecName1 = 8 '规格一
ICOL_SpecID1 = 9 '规格一明细编号
ICOL_SpecDtlName1 = 10 '规格明细一

ICOL_SpecNo2 = 11 '规格二编号
ICOL_SpecName2 = 12 '规格二
ICOL_SpecID2 = 13 '规格二明细编号
ICOL_SpecDtlName2 = 14 '规格明细二

ICOL_InitalStockNum = 15 '初始库存
ICOL_RemainingStockNum = 16 '剩余库存

ICOL_GoodsPrice = 17 '商品定价

End Enum

''' <summary>
''' 构造函数

''' </summary>
''' <param name="strTempPath"></param>
''' <remarks></remarks>
Public Sub New(ByVal strTempPath As String)
MyBase.New(strTempPath)
End Sub

''' <summary>
''' 内容作成
''' </summary>
''' <param name="aryData"></param>
''' <remarks></remarks>
Protected Overrides Sub FillExportData(ByVal ParamArray aryData() As Object)
ExcelApp.AlertBeforeOverwriting = False
ExcelApp.AskToUpdateLinks = False
ExcelApp.DisplayAlerts = False
' 填充Excel
Me.ExportDetail(aryData)
End Sub

Private Sub ExportDetail(ByVal aryData() As Object)

Dim objData As DataTable = aryData(0)
'定义数组
Dim strTemp(objData.Rows.Count, COL_CNT) As Object
Dim startRow As Integer = 4
For i As Integer = 0 To objData.Rows.Count - 1
strTemp(i, Enum_Col.ICOL_StockID) = MCO001.DBNULL2Str(objData.Rows(i).Item("StockID")).Trim
strTemp(i, Enum_Col.ICOL_GoodSID) = MCO001.DBNULL2Str(objData.Rows(i).Item("GoodsID")).Trim
strTemp(i, Enum_Col.ICOL_GoodsName) = MCO001.DBNULL2Str(objData.Rows(i).Item("GoodsName")).Trim
strTemp(i, Enum_Col.ICOL_StoreID) = MCO001.DBNULL2Str(objData.Rows(i).Item("StoreID")).Trim
strTemp(i, Enum_Col.ICOL_StoreName) = MCO001.DBNULL2Str(objData.Rows(i).Item("StoreName")).Trim
strTemp(i, Enum_Col.ICOL_CompanyID) = MCO001.DBNULL2Str(objData.Rows(i).Item("CompanyID")).Trim
strTemp(i, Enum_Col.ICOL_CompanyName) = MCO001.DBNULL2Str(objData.Rows(i).Item("CompanyName")).Trim

strTemp(i, Enum_Col.ICOL_SpecNo1) = MCO001.DBNULL2Str(objData.Rows(i).Item("SpecNo1")).Trim
strTemp(i, Enum_Col.ICOL_SpecName1) = MCO001.DBNULL2Str(objData.Rows(i).Item("SpecName1")).Trim
strTemp(i, Enum_Col.ICOL_SpecID1) = MCO001.DBNULL2Str(objData.Rows(i).Item("SpecID1")).Trim
strTemp(i, Enum_Col.ICOL_SpecDtlName1) = MCO001.DBNULL2Str(objData.Rows(i).Item("SpecDtlName1")).Trim

strTemp(i, Enum_Col.ICOL_SpecNo2) = MCO001.DBNULL2Str(objData.Rows(i).Item("SpecNo1")).Trim
strTemp(i, Enum_Col.ICOL_SpecName2) = MCO001.DBNULL2Str(objData.Rows(i).Item("SpecName2")).Trim
strTemp(i, Enum_Col.ICOL_SpecID2) = MCO001.DBNULL2Str(objData.Rows(i).Item("SpecID2")).Trim
strTemp(i, Enum_Col.ICOL_SpecDtlName2) = MCO001.DBNULL2Str(objData.Rows(i).Item("SpecDtlName2")).Trim

strTemp(i, Enum_Col.ICOL_InitalStockNum) = MCO001.DBNULL2Str(objData.Rows(i).Item("InitalStockNum")).Trim
strTemp(i, Enum_Col.ICOL_RemainingStockNum) = MCO001.DBNULL2Str(objData.Rows(i).Item("RemainingStockNum")).Trim
strTemp(i, Enum_Col.ICOL_GoodsPrice) = MCO001.DBNULL2Str(objData.Rows(i).Item("GoodsPrice")).Trim

Next

Dim tmp As String = "A4:R" + (objData.Rows.Count + 4).ToString
setRangeValue(Me.Sheet, strTemp, tmp, "")
'默认选中第一行

Me.Sheet.Rows(1).Select()

End Sub
End Class

上一篇:Oracle数据库在plsql中文乱码,显示问号????


下一篇:hadoop本地库与系统版本不一致引起的错误解决方法