将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 ...
随机推荐
- 【bzoj2594】[Wc2006]水管局长数据加强版
真是神题 当时调了几天没调出来 后来没管了 当时把fread去掉就TLE,加上就RE 一直在底下跟网上的程序拍,尝试各种优化常数都没用 拍出几组不一样的,发现我是对的,醉了,网上那个是怎么过的 记一下 ...
- 缓冲运动-1-[解决1].html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...
- Android常用功能代码块(转)
1.设置activity无标题,全屏 // 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式 getWindow(). ...
- npm scripts 助力前端开发,实时刷新浏览器
用browser-sync或者lite-server来监控文件的改变,当文件改变时,就自动刷新浏览器. 用node-sass来实时编译scss文件. 用parallelshell来异步执行npm sc ...
- nginx url 重写 [转]
本文转自 http://www.jbxue.com/article/2187.html 本文介绍nginx URL重写的相关知识,包括301重定向的内容等,希望对大家有所帮助. nginx rewri ...
- RxJava RxAndroid【简介】
资源 RxJava:https://github.com/ReactiveX/RxJava RxAndroid :https://github.com/ReactiveX/RxAndroid 官网:h ...
- jquery获取元素到页面顶部距离
jquery获取元素到页面顶部距离的语句为: $(selector).offset().top
- MVC跳转
//RedirectToAction(view?参数,控制器); return RedirectToAction("MyjoinEvent?id=" + eventid + &qu ...
- GET or POST
w3school中是这么说的: 与 POST 相比,GET 更简单也更快,并且在大部分情况下都能用. 然而,在以下情况中,请使用 POST 请求: 无法使用缓存文件(更新服务器上的文件或数据库) 向服 ...
- 【转】 iOS 原生二维码扫描(可限制扫描区域)
在用 AVFoundation 完成扫码后,遇到2个问题: 1,如何限制扫描范围? 2.条形码如何扫描? 一位朋友的文章帮助了我,特地转来,可以帮到有需要的朋友. 原文:http://blog.csd ...