Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
catering_demo
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
姜天宇
catering_demo
Commits
4421b4ab
Commit
4421b4ab
authored
Nov 21, 2025
by
陈宇雄
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feature: POS-2714 支持导入excel格式商品资料
parent
3ac851ad
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
25 deletions
+120
-25
build.gradle
app/build.gradle
+5
-1
MainActivity.java
...in/java/com/wmdigit/cateringservicedemo/MainActivity.java
+115
-24
商品资料导入模板.xlsx
商品资料导入模板.xlsx
+0
-0
No files found.
app/build.gradle
View file @
4421b4ab
...
...
@@ -8,7 +8,7 @@ android {
defaultConfig
{
applicationId
"com.wmdigit.cateringservicedemo"
minSdk
2
4
minSdk
2
6
targetSdk
33
versionCode
10000
versionName
"1.0.0"
...
...
@@ -43,4 +43,8 @@ dependencies {
implementation
fileTree
(
include:
[
'*.jar'
],
dir:
'libs'
)
implementation
'com.elvishew:xlog:1.11.1'
// 添加Apache POI库用于处理Excel文件
implementation
'org.apache.poi:poi:5.2.3'
implementation
'org.apache.poi:poi-ooxml:5.2.3'
}
\ No newline at end of file
app/src/main/java/com/wmdigit/cateringservicedemo/MainActivity.java
View file @
4421b4ab
package
com
.
wmdigit
.
cateringservicedemo
;
import
androidx.appcompat.app.AppCompatActivity
;
import
androidx.recyclerview.widget.GridLayoutManager
;
import
androidx.viewbinding.ViewBinding
;
import
android.content.ComponentName
;
import
android.content.Intent
;
import
android.net.Uri
;
import
android.os.Bundle
;
import
android.os.IBinder
;
import
android.os.RemoteException
;
import
android.text.method.ScrollingMovementMethod
;
import
androidx.appcompat.app.AppCompatActivity
;
import
androidx.recyclerview.widget.GridLayoutManager
;
import
com.wmdigit.cateringservicedemo.adapter.ApiButtonAdapter
;
import
com.wmdigit.cateringservicedemo.data.Constant
;
import
com.wmdigit.cateringservicedemo.data.ErrorCode
;
...
...
@@ -23,8 +24,15 @@ import com.wmdigit.service.WmSdk;
import
com.wmdigit.service.aidl.model.DetectResult
;
import
com.wmdigit.service.listener.OnServiceConnectListener
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.DataFormatter
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
...
...
@@ -37,6 +45,9 @@ public class MainActivity extends AppCompatActivity {
private
ActivityMainBinding
viewBinding
;
private
ApiButtonAdapter
mainProcessAdapter
,
statusInfoAdapter
,
otherAdapter
;
// 添加请求码常量
private
static
final
int
PICK_EXCEL_FILE
=
1001
;
/**
* 是否在识别时生成图片并返回
* 如无需展示该图片,建议关闭可以提升运算速度
...
...
@@ -240,25 +251,8 @@ public class MainActivity extends AppCompatActivity {
break
;
case
"3.4"
:
log
(
"准备导入测试用商品资料"
);
// 导入商品资料
List
<
ProductsDTO
>
list
=
new
ArrayList
<>();
list
.
add
(
new
ProductsDTO
(
"西红柿炒鸡蛋"
,
"000323"
,
"XHSCJD"
,
"9.80"
,
1
));
list
.
add
(
new
ProductsDTO
(
"红烧茄子"
,
"000324"
,
"HSQZ"
,
"8.80"
,
1
));
list
.
add
(
new
ProductsDTO
(
"套餐A"
,
"000325"
,
"TCA"
,
"22.80"
,
1
));
list
.
add
(
new
ProductsDTO
(
"套餐B"
,
"000326"
,
"TCB"
,
"15.80"
,
1
));
list
.
add
(
new
ProductsDTO
(
"套餐C"
,
"000327"
,
"TCC"
,
"18.80"
,
1
));
list
.
add
(
new
ProductsDTO
(
"套餐D"
,
"000328"
,
"TCD"
,
"30.00"
,
1
));
list
.
add
(
new
ProductsDTO
(
"套餐E"
,
"000329"
,
"TCF"
,
"23.80"
,
1
));
list
.
add
(
new
ProductsDTO
(
"披萨"
,
"000330"
,
"PS"
,
"25.00"
,
1
));
list
.
add
(
new
ProductsDTO
(
"牛角包"
,
"000331"
,
"NJB"
,
"6.40"
,
1
));
list
.
add
(
new
ProductsDTO
(
"三明治"
,
"000332"
,
"SMZ"
,
"16.80"
,
1
));
list
.
add
(
new
ProductsDTO
(
"菠萝包"
,
"000333"
,
"BLB"
,
"12.50"
,
1
));
list
.
add
(
new
ProductsDTO
(
"蛋挞"
,
"000334"
,
"DT"
,
"6.00"
,
1
));
list
.
add
(
new
ProductsDTO
(
"豆沙包"
,
"000335"
,
"DSB"
,
"6.60"
,
1
));
list
.
add
(
new
ProductsDTO
(
"麻花包"
,
"000336"
,
"MHB"
,
"9.20"
,
1
));
WmSdk
.
getInstance
().
importProducts
(
list
);
log
(
"商品资料导入完成"
);
// 修改为让用户选择Excel文件并解析导入商品资料
selectExcelFile
();
break
;
case
"3.5"
:
...
...
@@ -380,4 +374,101 @@ public class MainActivity extends AppCompatActivity {
otherAdapter
=
null
;
}
// 添加选择Excel文件的方法
private
void
selectExcelFile
()
{
log
(
"请选择一个Excel文件"
);
Intent
intent
=
new
Intent
(
Intent
.
ACTION_GET_CONTENT
);
intent
.
setType
(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
// xlsx格式
intent
.
addCategory
(
Intent
.
CATEGORY_OPENABLE
);
startActivityForResult
(
Intent
.
createChooser
(
intent
,
"选择Excel文件"
),
PICK_EXCEL_FILE
);
}
@Override
protected
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
data
)
{
super
.
onActivityResult
(
requestCode
,
resultCode
,
data
);
if
(
requestCode
==
PICK_EXCEL_FILE
&&
resultCode
==
RESULT_OK
)
{
Uri
uri
=
data
.
getData
();
if
(
uri
!=
null
)
{
parseExcelFile
(
uri
);
}
else
{
log
(
"未选择文件"
);
}
}
}
// 解析Excel文件并导入商品数据
private
void
parseExcelFile
(
Uri
uri
)
{
executorService
.
execute
(()
->
{
try
{
log
(
"开始解析Excel文件"
);
InputStream
inputStream
=
getContentResolver
().
openInputStream
(
uri
);
Workbook
workbook
=
new
XSSFWorkbook
(
inputStream
);
Sheet
sheet
=
workbook
.
getSheetAt
(
0
);
// 获取第一个工作表
List
<
ProductsDTO
>
productList
=
new
ArrayList
<>();
DataFormatter
formatter
=
new
DataFormatter
();
// 用于格式化单元格值
// 从第二行开始读取数据(跳过标题行)
for
(
int
i
=
1
;
i
<=
sheet
.
getLastRowNum
();
i
++)
{
Row
row
=
sheet
.
getRow
(
i
);
if
(
row
!=
null
)
{
// 读取各列数据
// 假设Excel列顺序为:商品名称、商品编码、商品简称、价格、是否上架(0下架,1上架)
String
productName
=
getCellValueAsString
(
row
,
0
,
formatter
);
// 商品名称
String
productCode
=
getCellValueAsString
(
row
,
1
,
formatter
);
// 商品编码
String
productAbbr
=
getCellValueAsString
(
row
,
2
,
formatter
);
// 商品简称
String
price
=
getCellValueAsString
(
row
,
3
,
formatter
);
// 价格
String
onSaleStr
=
getCellValueAsString
(
row
,
4
,
formatter
);
// 是否上架
// 上架状态转换为整数 (0下架,1上架)
int
onSale
=
1
;
// 默认上架
try
{
int
parsedValue
=
Integer
.
parseInt
(
onSaleStr
);
if
(
parsedValue
==
0
||
parsedValue
==
1
)
{
onSale
=
parsedValue
;
}
else
{
log
(
"警告:第"
+
(
i
+
1
)
+
"行上架状态值无效,使用默认值1(上架)"
);
}
}
catch
(
NumberFormatException
e
)
{
log
(
"警告:第"
+
(
i
+
1
)
+
"行上架状态转换失败,使用默认值1(上架)"
);
}
// 创建商品对象
ProductsDTO
product
=
new
ProductsDTO
(
productName
,
productCode
,
productAbbr
,
price
,
onSale
);
productList
.
add
(
product
);
// 记录每个商品的信息
log
(
"解析商品["
+
i
+
"]: "
+
productName
+
", 编码: "
+
productCode
+
", 简称: "
+
productAbbr
+
", 价格: "
+
price
+
", 上架状态: "
+
(
onSale
==
1
?
"上架"
:
"下架"
));
}
}
workbook
.
close
();
inputStream
.
close
();
// 导入商品数据
if
(!
productList
.
isEmpty
())
{
log
(
"解析完成,共找到 "
+
productList
.
size
()
+
" 个商品,开始导入"
);
WmSdk
.
getInstance
().
importProducts
(
productList
);
runOnUiThread
(()
->
log
(
"商品资料导入完成"
));
}
else
{
runOnUiThread
(()
->
log
(
"Excel文件中未找到有效的商品数据"
));
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
runOnUiThread
(()
->
log
(
"解析Excel文件出错: "
+
e
.
getMessage
()));
}
});
}
// 获取单元格值为字符串
private
String
getCellValueAsString
(
Row
row
,
int
cellIndex
,
DataFormatter
formatter
)
{
Cell
cell
=
row
.
getCell
(
cellIndex
);
if
(
cell
!=
null
)
{
return
formatter
.
formatCellValue
(
cell
);
}
return
""
;
}
}
\ No newline at end of file
商品资料导入模板.xlsx
0 → 100644
View file @
4421b4ab
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment