Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
Y
YX_IDENT_beijing_auxiliary_YD
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
zhangyusheng
YX_IDENT_beijing_auxiliary_YD
Commits
9c472ca9
Commit
9c472ca9
authored
Mar 02, 2019
by
dahai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改上传EXCEL文件
parent
f85f3ad6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
27 deletions
+22
-27
ReadExcelApi.java
src/main/java/com/yxproject/start/api/ReadExcelApi.java
+15
-22
ReadExcel.java
src/main/java/com/yxproject/start/utils/ReadExcel.java
+7
-5
No files found.
src/main/java/com/yxproject/start/api/ReadExcelApi.java
View file @
9c472ca9
...
...
@@ -22,7 +22,8 @@ import javax.servlet.http.HttpServletResponse;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
import
java.io.*
;
import
java.io.File
;
import
java.io.IOException
;
import
java.sql.Time
;
import
java.util.*
;
...
...
@@ -78,25 +79,25 @@ public class ReadExcelApi {
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename
=
item
.
getName
();
File
file
=
null
;
try
{
// 如果fileitem中封装的是普通输入项的数据
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename
=
item
.
getName
();
if
(
filename
==
null
||
filename
.
trim
().
equals
(
""
))
{
continue
;
System
.
out
.
println
(
item
.
getName
());
// 为上传的key值
System
.
out
.
println
(
item
.
getOriginalFilename
());
// 上传的真实文件的文件名
File
file
=
null
;
try
{
file
=
File
.
createTempFile
(
"prefix"
,
"_"
+
item
.
getOriginalFilename
());
item
.
transferTo
(
file
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
InputStream
in
=
item
.
getInputStream
();
OutputStream
os
=
new
FileOutputStream
(
file
);
int
bytesRead
=
0
;
byte
[]
buffer
=
new
byte
[
8192
];
while
((
bytesRead
=
in
.
read
(
buffer
,
0
,
8192
))
!=
-
1
)
{
os
.
write
(
buffer
,
0
,
bytesRead
);
}
os
.
close
();
in
.
close
();
// CommonsMultipartFile cf= (CommonsMultipartFile)item;
// DiskFileItem fi = (DiskFileItem)cf.getFileItem();
// File file = fi.getStoreLocation();
ReadExcel
readExcel
=
new
ReadExcel
();
List
list1
=
readExcel
.
readExcel
(
file
);
//保存EXCEL文件信息
...
...
@@ -136,20 +137,12 @@ public class ReadExcelApi {
personPostEntity
.
setNatureOfTheInternal
((
String
)
list2
.
get
(
20
));
personPostEntity
.
setNatureOfTheInformation
((
String
)
list2
.
get
(
21
));
personPostEntity
.
setFirstWhite
((
String
)
list2
.
get
(
22
));
personPostEntity
.
setFileId
(
file
ID
);
personPostEntity
.
setFileId
(
file
NameDicEntity
.
getFileId
()
);
entityList
.
add
(
personPostEntity
);
}
boolean
b
=
personPostService
.
savePersonPost
(
entityList
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
yxresp
.
outPutError
(
"UnsupportedEncodingException"
,
"上传文件时发现文件编码错误:"
+
e
.
getMessage
());
continue
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
yxresp
.
outPutError
(
"IOException"
,
"上传文件时发生IO错误:"
+
e
.
getMessage
());
continue
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
yxresp
.
outPutError
(
"Exception"
,
"上传文件时发生错误,非法XML文件:"
+
filename
);
...
...
src/main/java/com/yxproject/start/utils/ReadExcel.java
View file @
9c472ca9
...
...
@@ -4,7 +4,9 @@ import jxl.Sheet;
import
jxl.Workbook
;
import
jxl.read.biff.BiffException
;
import
java.io.*
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -14,12 +16,12 @@ import java.util.List;
public
class
ReadExcel
{
// 去读Excel的方法readExcel,该方法的入口参数为一个File对象
public
List
readExcel
(
File
file
)
{
public
List
readExcel
(
File
file
)
{
try
{
// 创建输入流,读取Excel
InputStream
is
=
new
FileInputStream
(
file
.
getAbsolutePath
());
//
// 创建输入流,读取Excel
//
InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook类
Workbook
wb
=
Workbook
.
getWorkbook
(
is
);
Workbook
wb
=
Workbook
.
getWorkbook
(
file
);
// Excel的页签数量
int
sheet_size
=
wb
.
getNumberOfSheets
();
for
(
int
index
=
0
;
index
<
sheet_size
;
index
++)
{
...
...
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