当前位置:网站首页>Apache POI import export excel file
Apache POI import export excel file
2022-06-12 06:34:00 【Ling Yuan 9A】
Introduce dependencies
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
Upload Excel
Upload files Dom
<div class="layui-body" style="margin: 1%">
<div class="layui-upload-drag" id="fileUpload">
<i class="layui-icon"></i>
<p> Click upload , Or drag the file here </p>
</div>
</div>
js
layui.use(['upload', 'element','table'], function() {
var $ = layui.jquery,upload = layui.upload,table = layui.table;
upload.render({
elem: '#fileUpload',
url: '/file/import',
accept:'file',
done: function(res){
console.log(res.msg);
}
});
}
Java Code
@RequestMapping("/import")
@ResponseBody
public ResponseResult importExcel(@RequestParam("file")MultipartFile file) throws Exception{
ResponseResult res=new ResponseResult();
// 1. The file cannot be empty
if (file.isEmpty()){
res.setMessage(" The file is empty , Can't upload ");
}
// 2.POI obtain Excel Parsing data
HSSFWorkbook wb=new HSSFWorkbook(file.getInputStream());
HSSFSheet sheet=wb.getSheetAt(0);
// 3. Define a set , Receive data from files
List<City> dataList=new ArrayList();
HSSFRow row=null;
// 4. Parsing data , Put it in the collection
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
// Define the entity
City data=new City();
// Data for each line
row=sheet.getRow(i);
// Parsing data
data.setCity(row.getCell(0).getStringCellValue());
data.setValue((int)row.getCell(1).getNumericCellValue());
dataList.add(data);
//3. Insert database
cityService.addCity(dataList);
}
res.setCode("200");
res.setMessage(" Upload successful ");
res.setObj(dataList);
return res;
}
export Excel
export Dom
<button type="button" class="layui-btn layui-btn-sm latui-btn-radius" lay-submit="" lay-filter="doExport">
<i class="layui-icon layui-icon-file"> export Excel</i>
</button>
js
form.on('submit(doExport)',function () {
window.location.href="/export"
})
Java Code
@RequestMapping("/export")
@ResponseBody
public void exportExcel(HttpServletResponse response) throws Exception {
//1. Query the database ,
List<City> datalist = cityService.findAll();
//2. establish Excel object , Encapsulated data
response.setCharacterEncoding("UTF-8");
//2.1 establish Excel object
HSSFWorkbook wb=new HSSFWorkbook();
//2.2 establish sheet object
HSSFSheet sheet = wb.createSheet(" Epidemic data in China Sheet1");
//2.3 Create header
HSSFRow hssfRow=sheet.createRow(0);
hssfRow.createCell(0).setCellValue(" The city name ");
hssfRow.createCell(1).setCellValue(" Number of confirmed cases ");
//3. Traversal data
for(City data:datalist){
HSSFRow dataRow=sheet.createRow(sheet.getLastRowNum()+1);
dataRow.createCell(0).setCellValue(data.getName());
dataRow.createCell(1).setCellValue(data.getValue());
}
//4. Create output stream , Output file to browser
OutputStream os=null;
//4.1 Set up Excel File name
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader( "Content-Disposition", "attachment;filename=" + new String( " Epidemic data sheet ".getBytes("gb2312"), "ISO8859-1" ));
//4.2 The output file
os= response.getOutputStream();
wb.write(os);
os.flush();
//5. Close output stream
os.close();
}
边栏推荐
- Delete the duplicate items in the ordered array -- force deduction question 26 (simple)
- The second day of June training - string
- Jetson TX2 machine brushing jetpack4.2 (self test successful version)
- LeetCode-884. Unusual words in two sentences
- Redis application (I) -- distributed lock
- Cv2.fillpoly coco annotator segment coordinate conversion to mask image
- 六月集训 第九日——位运算
- leetcode 278. First wrong version
- 六月集训 第七日 ——哈希表
- Explanation of sensor flicker/banding phenomenon
猜你喜欢

PHP read / write cookie

Reentrantlock underlying AQS source code analysis

leetcode 278. First wrong version

Process when solving vagrant up_ builder. rb:43:in `join‘: incompatible character encodings: GBK and UTF-8

Opencv_100问_第五章 (21-25)

Cause analysis of motion blur / smear caused by camera shooting moving objects

张驰咨询:流程是一剂万能良药吗?

How to build your own website (using the pagoda panel)

Bulk Rename Utility

Whether the modification of basic type and reference type is valid
随机推荐
Leetcode personal question solution (Sword finger offer3-5) 3 Duplicate number in array, 4 Find in 2D array, 5 Replace spaces
上传文件(post表单提交form-data)
Whether the modification of basic type and reference type is valid
Bulk Rename Utility
The fifth day of June training - double pointer
Textcnn (MR dataset - emotion classification)
PDF. js FAQs
SQL injection - Union query
Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
Solution: content type 'application/x-www-form-urlencoded; charset=UTF-8‘ not supported
Cv2.fillpoly coco annotator segment coordinate conversion to mask image
LeetCode-1445. Apples and oranges
Piecewise Bezier curve
How to build your own website (using the pagoda panel)
About session Getattribute, getattribute error
使用 ms17-010 永恒之蓝漏洞对 win7 进行渗透及建立永久后门
张驰课堂:2022年CAQ中质协六西格玛考试时间通知
What states do threads have?
六月集训 第九日——位运算
GET 和 POST 的区别及留言板代码实现