zoukankan      html  css  js  c++  java
  • 将excel里面的数据导入到程序里面

    页面布局

    <table>
    <tr>
    <td style="padding-top: 16px; padding-left: 36px;">
    <input type="file" id="inputFile" runat="server" size="50" style=" 100%"
    contenteditable="false" name="inputFile" tabindex="60" />
    </td>
    <td style="padding-top: 16px;">
    <table width="52" style="height: 36px; background-image: url(../images/but_d1.gif)"
    border="0" cellpadding="0" cellspacing="0" class="z3">
    <tr>
    <td>
    <asp:Button ID="btnOrderImport" runat="server" Text=""
    OnClientClick="return checkImport()" TabIndex="50" />
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>

    导入按钮点击事件
    Protected Sub btnOrderImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOrderImport.Click
    Try
    Dim ack As HuiLeCardInOut = New HuiLeCardInOut()
    '读取Excel
    Dim message As String = Me.DoImport()
    Me.Controls.Add(New LiteralControl("<SCRIPT LANGUAGE=JavaScript>alert('" & message & "')</script>"))
    '再检索
    If message = MCO003.getNameByResID(MCO005.IMSG004) Then
    GetData(1)
    End If
    Catch ex As Exception
    Me.Controls.Add(New LiteralControl("<SCRIPT LANGUAGE=JavaScript>alert('" & MCO003.getNameByResID(MCO005.IMSG010) & "')</script>"))
    Finally
    GC.Collect()
    End Try
    End Sub

    Private Function DoImport() As String
    LogManager.Debug("导入开始")
    Dim excel As Object
    Dim workbook As Object
    Dim worksheet As Object
    Dim objExcelType As Type
    Dim message As String = String.Empty
    Try
    '1、打开文件选择框
    Dim filePathName As String = CommonFunction.Const_UPLOAD_PATH & "" & GetUploadFile()
    objExcelType = Type.GetTypeFromProgID("Excel.Application", True)
    excel = System.Activator.CreateInstance(objExcelType)
    workbook = excel.Workbooks.Open(filePathName)
    worksheet = workbook.Sheets(1)
    Dim maxRow As Integer = worksheet.UsedRange.Rows.Count()
    Dim maxColumn As Integer = worksheet.UsedRange.Columns.Count()
    Dim i As Integer
    Dim j As Integer


    Dim stockID_row_col As String = String.Empty
    Dim goodsID_row_col As String = String.Empty
    Dim storeID_row_col As String = String.Empty
    Dim companyID_row_col As String = String.Empty

    Dim specID1_row_col As String = String.Empty
    Dim specID2_row_col As String = String.Empty

    Dim initalStockNum_row_col As String = String.Empty
    Dim remainingStockNum_row_col As String = String.Empty
    Dim goodsPrice_row_col As String = String.Empty

    Dim exitFlag As Integer = 0

    编号一到编号九,是excel模板上面的标题

    For i = 1 To maxRow
    For j = 1 To maxColumn
    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号一" Then
    stockID_row_col = i.ToString + "," + j.ToString
    End If
    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号二" Then
    goodsID_row_col = i.ToString + "," + j.ToString
    End If
    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号三" Then
    storeID_row_col = i.ToString + "," + j.ToString
    End If

    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号四" Then
    companyID_row_col = i.ToString + "," + j.ToString
    End If

    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号五" Then
    specID1_row_col = i.ToString + "," + j.ToString
    End If

    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号六" Then
    specID2_row_col = i.ToString + "," + j.ToString
    End If

    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号七" Then
    initalStockNum_row_col = i.ToString + "," + j.ToString
    End If
    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号八" Then
    remainingStockNum_row_col = i.ToString + "," + j.ToString
    End If
    If Not worksheet.Cells(i, j).value Is Nothing AndAlso worksheet.Cells(i, j).value.ToString = "编号九" Then
    goodsPrice_row_col = i.ToString + "," + j.ToString
    End If


    If stockID_row_col <> String.Empty AndAlso goodsID_row_col <> String.Empty AndAlso storeID_row_col <> String.Empty AndAlso companyID_row_col <> String.Empty AndAlso specID1_row_col <> String.Empty AndAlso specID2_row_col <> String.Empty AndAlso initalStockNum_row_col <> String.Empty AndAlso remainingStockNum_row_col <> String.Empty AndAlso goodsPrice_row_col <> String.Empty Then
    exitFlag = 1
    Exit For
    End If
    Next
    If exitFlag = 1 Then
    Exit For
    End If
    Next

    If stockID_row_col = String.Empty Then
    message += " 没有找到[编号一]列!"
    End If

    If goodsID_row_col = String.Empty Then
    message += " 没有找到[编号二]列!"
    End If
    If storeID_row_col = String.Empty Then
    message += " 没有找到[编号三]列!"
    End If

    If companyID_row_col = String.Empty Then
    message += " 没有找到[编号四]列!"
    End If

    If specID1_row_col = String.Empty Then
    message += " 没有找到[编号五]列!"
    End If

    If specID2_row_col = String.Empty Then
    message += " 没有找到[编号六]列!"
    End If

    If initalStockNum_row_col = String.Empty Then
    message += " 没有找到[编号七]列!"
    End If
    If remainingStockNum_row_col = String.Empty Then
    message += " 没有找到[编号八]列!"
    End If
    If goodsPrice_row_col = String.Empty Then
    message += " 没有找到[编号九]列!"
    End If
    If Not String.IsNullOrEmpty(message) Then
    Return message
    End If

    Dim stockID_row As Integer = Val(stockID_row_col.Split(",")(0))
    Dim stockID_col As Integer = Val(stockID_row_col.Split(",")(1))

    Dim goodsID_row As Integer = Val(goodsID_row_col.Split(",")(0))
    Dim goodsID_col As Integer = Val(goodsID_row_col.Split(",")(1))

    Dim storeID_row As Integer = Val(storeID_row_col.Split(",")(0))
    Dim storeID_col As Integer = Val(storeID_row_col.Split(",")(1))

    Dim companyID_row As Integer = Val(companyID_row_col.Split(",")(0))
    Dim companyID_col As Integer = Val(companyID_row_col.Split(",")(1))

    Dim specID1_row As Integer = Val(specID1_row_col.Split(",")(0))
    Dim specID1_col As Integer = Val(specID1_row_col.Split(",")(1))

    Dim specID2_row As Integer = Val(specID2_row_col.Split(",")(0))
    Dim specID2_col As Integer = Val(specID2_row_col.Split(",")(1))

    Dim initalStockNum_row As Integer = Val(initalStockNum_row_col.Split(",")(0))
    Dim initalStockNum_col As Integer = Val(initalStockNum_row_col.Split(",")(1))

    Dim remainingStockNum_row As Integer = Val(remainingStockNum_row_col.Split(",")(0))
    Dim remainingStockNum_col As Integer = Val(remainingStockNum_row_col.Split(",")(1))

    Dim goodsPrice_row As Integer = Val(goodsPrice_row_col.Split(",")(0))
    Dim goodsPrice_col As Integer = Val(goodsPrice_row_col.Split(",")(1))

    If stockID_row = goodsID_row AndAlso goodsID_row = storeID_row AndAlso storeID_row = companyID_row AndAlso companyID_row = specID1_row AndAlso specID1_row = specID2_row AndAlso specID2_row = initalStockNum_row AndAlso initalStockNum_row = remainingStockNum_row AndAlso remainingStockNum_row = goodsPrice_row Then
    Else
    Return message = "[库存编号]列、[商品编号]列、[所属店铺编号]列、[经销商编号]列、[规格一编号]列、[规格二编号]列、[初始库存]列、[剩余库存]列和[商品定价]列不在同一行!"
    End If

    Dim goodsStockList As New ArrayList
    For i = stockID_col + 3 To maxRow
    Dim goodsStockInfo As GoodsStockInOut = New GoodsStockInOut

    With goodsStockInfo

    If worksheet.Cells(i, stockID_col).value Is Nothing Then
    .StockID = ""
    Else
    .StockID = worksheet.Cells(i, stockID_col).value.ToString
    End If

    If worksheet.Cells(i, goodsID_col).value Is Nothing Then
    .GoodsId = ""
    Else
    .GoodsId = worksheet.Cells(i, goodsID_col).value.ToString
    End If

    If worksheet.Cells(i, storeID_col).value Is Nothing Then
    .StoreId = ""
    Else
    .StoreId = worksheet.Cells(i, storeID_col).value.ToString
    End If

    If worksheet.Cells(i, companyID_col).value Is Nothing Then
    .CompanyID = ""
    Else
    .CompanyID = worksheet.Cells(i, companyID_col).value.ToString
    End If

    If worksheet.Cells(i, specID1_col).value Is Nothing Then
    .SpecID1 = ""
    Else
    .SpecID1 = worksheet.Cells(i, specID1_col).value.ToString
    End If


    If worksheet.Cells(i, specID2_col).value Is Nothing Then
    .SpecID2 = ""
    Else
    .SpecID2 = worksheet.Cells(i, specID2_col).value.ToString
    End If


    If worksheet.Cells(i, initalStockNum_col).value Is Nothing Then
    .InitalStockNum = 0
    Else
    .InitalStockNum = MCO001.DBNULL2Int(worksheet.Cells(i, initalStockNum_col).value.ToString)
    End If
    If worksheet.Cells(i, remainingStockNum_col).value Is Nothing Then
    .RemainingStockNum = 0
    Else
    .RemainingStockNum = MCO001.DBNULL2Int(worksheet.Cells(i, remainingStockNum_col).value.ToString)
    End If
    If worksheet.Cells(i, goodsPrice_col).value Is Nothing Then
    .GoodsPrice = 0
    Else
    .GoodsPrice = MCO001.DBNULL2Double(worksheet.Cells(i, goodsPrice_col).value.ToString)
    End If

    If Not String.IsNullOrEmpty(.StockID) Then
    message = CheckStockID(.StockID, message, i)
    End If
    End With
    If Not String.IsNullOrEmpty(goodsStockInfo.StockID) Then
    goodsStockList.Add(goodsStockInfo)
    End If

    Next

    If String.IsNullOrEmpty(message) Then
    Dim flag As Integer = APICurrent.API.Dynamic.Trade.UpdateStockInfo(goodsStockList)
    If flag = 1 Then
    message = MCO003.getNameByResID(MCO005.IMSG004)
    Else
    message = MCO003.getNameByResID(MCO005.IMSG010)
    End If
    End If
    Catch ex As Exception
    LogManager.Error(ex.ToString())
    Throw
    Finally
    worksheet = Nothing
    workbook.Close()
    workbook = Nothing
    excel.Quit()
    excel = Nothing
    End Try
    Return message
    LogManager.Debug("导入结束")
    End Function

    Private Function GetUploadFile() As String
    '获得临时文件目录
    Dim filename As String
    Dim tempPath As String = CommonFunction.Const_UPLOAD_PATH
    '判断文件名是否存在,不存在创建

    CommonFunction.createFolder(tempPath)

    '上传文件到临时目录

    Dim contentType As String = inputFile.PostedFile.ContentType
    filename = GetShortFileName(inputFile.PostedFile.FileName)
    Me.inputFile.PostedFile.SaveAs(tempPath & "" & filename)
    '把输入的地址设给隐藏变量
    'Me.hidinputFile.Value = tempPath & "" & filename
    Return filename

    End Function

    Private Function GetShortFileName(ByVal strLongFileName As String) As String
    Dim strShortFileName = strLongFileName.Substring(strLongFileName.LastIndexOf("") + 1)
    Return strShortFileName
    End Function

    Private Function CheckStockID(ByVal stockID As String, ByVal message As String, ByVal row As Integer) As String
    If String.IsNullOrEmpty(stockID) Then
    message = message + " 第" + row.ToString + "行库存编号为空!"
    Else
    Dim stockDt As GoodsStockMstField = APICurrent.API.Dynamic.Trade.searchGoodsStockInfoByPK(stockID)
    If stockDt.Rows.Count < 1 Then
    message = message + " 第" + row.ToString + "行库存编号不存在!"
    End If
    End If
    Return message
    End Function


    js文件里面判断文件格式是否正确

    function checkImport() {
    //【数据导入按钮按下】判断导入文件是否为空
    if (event.srcElement.id == MainContent + "btnOrderImport") {
    var inputFile = document.getElementById(MainContent + "inputFile");
    var strValue = $.trim(inputFile.value)
    if (strValue.length == 0) {
    inputFile.focus();
    alert("请输入导入文件地址");
    return false;
    } else {
    var postfixArray = strValue.toString().split(".");
    var postfixStr = postfixArray[postfixArray.length - 1];
    //【成员导入按钮按下】类型判断
    if (postfixStr != "xls" && postfixStr != "xlsx") {
    alert("文件格式不正确,请选择.xls/xlsx的文件!");
    return false;
    }
    return true;
    }
    }
    return true;
    }

  • 相关阅读:
    Session的异常
    struts2中把action中的值传递到jsp页面的例子
    struts2中怎么把action中的值传递到jsp页面
    struts2理解
    Struts2工作原理
    第十五章 String讲解
    十六进制转十进制
    数据库综合系列 之 触发器
    android PopupWindow实现从底部弹出或滑出选择菜单或窗口
    kettle内存溢出
  • 原文地址:https://www.cnblogs.com/ahao214/p/5729853.html
Copyright © 2011-2022 走看看