将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 ...
随机推荐
- C语言判断系统数据大/小端存储方式
小端存储:数据的低位部分,存储于存储器的低地址空间里. 大端存储:数据的低位部分,存储于存储器的高地址空间里. 首先,一般PC数据存储方式是小端存储. 基本实现思想是:将存储器中所存的数据按字节以地址 ...
- linux c 系统报错
本文中的错误是指在代码编译完全正确程序可运行的情况下,因为没有成功调用程序中的某些系统调用函数而产生的错误.往往这些系统调用函数通过返回值(比如1,0,-1)来说明其是否调用成功,而程序员需要知道详细 ...
- 在VisualStudio 2012中通过SmallSharp压缩js及修改web.config
在项目中加入一个targets文件,取名my.build.targets 在targets文件中加入内容: <?xml version="1.0" encoding=&quo ...
- navigaitonBar的自定义设置
navigaitonBar的自定义设置 navigationBar介绍: navigationbar就是一个导航视图控制器上面的导航栏. 如何设置这个navigationbar? 首先我们来探讨如何来 ...
- Quartz简单使用
官方的Quartz会提供例子的,例子看个前三四个感觉就够用了,主要就是起Timer的作用,但是比timer稳定,而且功能更全. UpdateClientTimer.task(ClearJob.clas ...
- 【开源java游戏框架libgdx专题】-02-Eclipse Gradle 环境安装
创建eclipse开发环境 Eclipse 4.5 Help -> install newsoftware 填上下载地址(Eclipse 4.5及以上版本): http://dist.sprin ...
- 移动页面缩放方法之(二)控制HTML
<!DOCTYPE HTML> <html lang="zh-cn"> <head> <meta http-equiv="Con ...
- 网页嵌入百度地图和使用百度地图api自定义地图的详细步骤
在网页中插入百度地图 如果想在自己的网页上面加入百度地图的话,可以用百度地图的api.具体使用方法如下: 第一步:进入百度创建地图的网站http://api.map.baidu.com/lbsapi/ ...
- iOS关于sqlite3操作
原文:http://hi.baidu.com/clickto/blog/item/0c6904f787c34125720eec87.html iPhone中支持通过sqlite3来访问iPhone本地 ...
- CSS注释代码
就像在Html的注释一样,在CSS中也有注释语句:用/*注释语句*/来标明(Html中使用<!--注释语句-->).就像下面代码: 示例: <!DOCTYPE HTML> &l ...