将excel里面的数据导入到程序里面
页面布局
<table>
<tr>
<td style="padding-top: 16px; padding-left: 36px;">
<input type="file" id="inputFile" runat="server" size="50" style="width: 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 += "\r\n没有找到[编号一]列!"
End If
If goodsID_row_col = String.Empty Then
message += "\r\n没有找到[编号二]列!"
End If
If storeID_row_col = String.Empty Then
message += "\r\n没有找到[编号三]列!"
End If
If companyID_row_col = String.Empty Then
message += "\r\n没有找到[编号四]列!"
End If
If specID1_row_col = String.Empty Then
message += "\r\n没有找到[编号五]列!"
End If
If specID2_row_col = String.Empty Then
message += "\r\n没有找到[编号六]列!"
End If
If initalStockNum_row_col = String.Empty Then
message += "\r\n没有找到[编号七]列!"
End If
If remainingStockNum_row_col = String.Empty Then
message += "\r\n没有找到[编号八]列!"
End If
If goodsPrice_row_col = String.Empty Then
message += "\r\n没有找到[编号九]列!"
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 + "\r\n第" + row.ToString + "行库存编号为空!"
Else
Dim stockDt As GoodsStockMstField = APICurrent.API.Dynamic.Trade.searchGoodsStockInfoByPK(stockID)
If stockDt.Rows.Count < 1 Then
message = message + "\r\n第" + 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;
}
将excel里面的数据导入到程序里面的更多相关文章
- jsp+servlet上传excel并将数据导入到数据库表的实现方法
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 使用Python将Excel中的数据导入到MySQL
使用Python将Excel中的数据导入到MySQL 工具 Python 2.7 xlrd MySQLdb 安装 Python 对于不同的系统安装方式不同,Windows平台有exe安装包,Ubunt ...
- 图解如何 将Excel里的数据导入到sql server数据库中
项目中,经常会碰到如何将Excel里的数据导入到sql server中的问题. 下面,图解如何实现导入Excel中的数据到sql server 2008 R2: Excel截图如下: 查询pub数据库 ...
- 使用openpyxl模块将Excel中的数据导入数据库
这里将不介绍openpyxl模块的详细操作. 主要就是记录一个使用openpyxl模块将Excel表格的数据导入数据库中的实例. from openpyxl import load_workbook ...
- excel中的数据导入oracle方法
SQL_loader批量上传数据 1. 注释 在工作中,很多时候会遇到如下情况:需要将excel中的数据批量上传到ORACLE表中.如果是小数据量,如几十条至几百条,那么用plsql dev工具 ...
- 详解用Navicat工具将Excel中的数据导入Mysql中
第一步:首先需要准备好有数据的excel: 第二步:选择"文件"->"另存为",保存为"CSV(逗号分隔)(*.csv)",将exce ...
- c#.net Excel中的数据导入到SQL数据库中
/// <summary> /// 从Excel 导入学生 /// </summary> /// <param name=&qu ...
- java基于xml配置的通用excel单表数据导入组件(五、Action处理类)
package xxxxxx.manage.importexcel; import java.io.File; import java.util.HashMap; import java.util.M ...
- java基于xml配置的通用excel单表数据导入组件(四、DAO主处理类)
package XXXXX.manage.importexcel; import java.beans.IntrospectionException; import java.io.BufferedR ...
随机推荐
- lesson10:hashmap变慢原因分析
下面的英文描述了String.hashCode()方法,在特定情况下,返回值为0的问题: Java offers the HashMap and Hashtable classes, which us ...
- android使用bintray发布aar到jcenter
前言 这两天心血来潮突然想把自己的android library的aar放到jcenter里面,这样一来自己便可以在任何时间任何地点通过internet得到自己的library的引用了,况且现在and ...
- JAVAEE filter总结
1. 为什么需要filter? filter相当于客户端和服务器端之间的一扇门,就像保安一样.作用:比如说设置字符集和权限控制等等. 2. 细节; * . 只能对post请求起作用 * .可以使 ...
- C - Surprising Strings
C - Surprising Strings 题意:输入一段字符串,假设在同一距离下有两个字符串同样输出Not surprising ,否 ...
- Android自动化测试之monkeyrunner工具
一.什么是monkeyrunner monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.通过monkeyrunner,您可以 ...
- Java 8 Features – The ULTIMATE Guide--reference
Now, it is time to gather all the major Java 8 features under one reference post for your reading pl ...
- CALayer的基本操作
CALayer简介: CALayer又称为层. 在每一个UIView内部都有一个Layer这样的属性. UIView之所以能够显示,就是因为它里面有这个一个层,才具有显示的功能. 我们通过操作C ...
- okhttp 基本介绍
资料汇总 官网:http://square.github.io/okhttp/ 文档:https://github.com/square/okhttp/wiki GitHub:https://gith ...
- 传感器 Sensor 加速度【示例】
简介 坐标系 x轴:从左到右 y轴:从下到上 z轴:从内到外 这个坐标系与Android 2D API中的不同,传感器中的返回值都以此坐标系为准. SENSOR_TYPE_ACCELEROMETER ...
- IOS 开发 【objective-c 基础1】
案例:读取本地硬盘上程序根目录下words.txt文件内容,显示每行的字符数. // // main.m // hello // // Created by swack on 15/11/27. // ...