Excel导入
来源:互联网 发布:淘宝网店上架宝贝教程 编辑:程序博客网 时间:2024/05/21 17:54
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">action类:Excel导入</span>
<span style="white-space:pre"></span>//注入代理对象@Resourceprivate UserService userService;
<pre name="code" class="java"><span style="white-space:pre"></span><pre name="code" class="java"><span style="white-space:pre"></span>//要设置导入的excel文件 变量private File userExcel;//excel文件private String userExcelFileName;//excel文件名称
//设置两个变量,文件和文件名称private File headImg;private String headImgFileName;
</pre><pre name="code" class="java"><span style="white-space:pre"></span><pre name="code" class="java"><span style="white-space:pre"></span>//excel导入有返回值,返回页面重写刷新public String importExcel() {//创建一个excel导入的方法=上传<span style="white-space:pre"></span>userService.importExcel(userExcel);return "list";}
</pre><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在action调用service层方法处理action的业务逻辑:</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span>
<span style="white-space:pre"></span>/** * 导入excel文件 * @param userExcel excel文件 */void importExcel(File userExcel);
<span style="white-space:pre"></span>// excel文件导入@Overridepublic void importExcel(File userExcel) {try {// 根据文件,创建excel文件薄对象Workbook workbook = WorkbookFactory.create(userExcel);// 获取工作表Sheet sheet = workbook.getSheetAt(0);// 读取excel的每一行(即只获取有数据的行数)课外知识补充:excel总共65535行for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {// 从第三行开始读取Row row = sheet.getRow(i + 2);// 把每一行里面的数据封装成一个对象User user = new User();// 开始封装数据user.setName(row.getCell(0).getStringCellValue());user.setAccount(row.getCell(1).getStringCellValue());user.setDept(row.getCell(2).getStringCellValue());// 三元运算符 判断性别user.setGender("男".equals(row.getCell(3).getStringCellValue()) ? true: false);try {// 如果电话单元格是string,直接获取字符串数据user.setMobile(row.getCell(4).getStringCellValue());} catch (Exception e) {// 否则就转成字符串double mb = row.getCell(4).getNumericCellValue();BigDecimal bd = BigDecimal.valueOf(mb);user.setMobile(bd.toString());}user.setEmail(row.getCell(5).getStringCellValue());user.setBirthday(row.createCell(6).getDateCellValue());user.setPassword("000000000" + i);// 对象封装完毕,保存对象this.save(user);}} catch (Exception e) {e.printStackTrace();}}页面jsp代码块
<span style="white-space:pre"></span><input type="button" value="导入" class="s_button" onclick="doImportExcel()"/>
<span style="white-space:pre"></span>//excel导入 function doImportExcel() { //导入必须是post提交 document.forms[0].action ="user_importExcel.action"; document.forms[0].submit();}
导入用户列表 (读取excel; 文件上传)
a. listUI.jsp
点击:“导入”
a1: 获取表单
a2: 修改表单提交地址
a3: 提交表单
post
multipart/form-data
b. Action---> importExcel()
b1: 获取上传的excel文件
b2: 读取excel,
获取每一行数据,封装User对象,保存对象!
0 0
- EXCEL导入
- 导入EXCEL
- Excel 导入
- Excel导入
- 导入Excel
- 导入Excel
- 导入Excel
- excel导入
- 导入EXCEL
- 导入excel
- 导入excel
- 导入Excel
- 导入excel
- Excel 导入
- 导入excel
- excel 导入
- Excel导入
- 导入Excel
- 本地访问weblogic控制台无反应,关闭linux操作系统防火墙
- CSS实现div添加过长的英文字母或数字的内容显示一排而溢出时,进行自动换行
- html5 from 2
- 多态类中的虚函数表是Compile-Time,还是Run-Time时建立的?
- 2016多校7 HDU 5810 Balls and Boxes
- Excel导入
- [LeetCode]--7. Reverse Integer
- Unity中UICamera设置
- Android 代码规范
- html5 video
- IDEA官方帮助文档解释什么是Artifact(原文翻译)
- POJ 3723 解题报告
- 全局变量和局部变量有什么区别?是怎么实现的?操作系统和编译器是怎么知道的?
- machine-learning-ex3_2