excel表格内容导入到mysql中(maven)

一: excel表格

二:程序

1.pom.xml

 

<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
   <groupId>net.sourceforge.jexcelapi</groupId>
   <artifactId>jxl</artifactId>
   <version>2.6.12</version>
</dependency>

2.在测试类text中写方法:

 

import com.msp.whg.domain.CoursesManage;
import jxl.Sheet;
import jxl.Workbook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
public class excelTest {

    @Autowired
    private CoursesManageService coursesManageService;

    //活动添加测试数据
    //测试service
    @Test
    public void ticketManageAdds(){
            String fileName= "";
            Map<String ,Object> resmap =  new HashMap<>();
            try {
                fileName = "C:\\Users\\Administrator\\Desktop\\laoniandaxue.xls";
                Workbook rwb = Workbook.getWorkbook(new File(fileName));
                Sheet rs = rwb.getSheet(0); // 或者rwb.getSheet("")
                int clos = rs.getColumns(); // 得到所有的列
                int rows = rs.getRows(); // 得到所有的行
                if (rows < 1000) {
                    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
                    for (int i = 1; i < rows; i++) {
                        for (int j = 0; j < clos; j++) { // 第一个是列数,第二个是行数
                            String cmCourses = rs.getCell(j++, i).getContents().trim();               // 课程名称
                            String cmType = rs.getCell(j++, i).getContents().trim();                 // 专业
                            String cmSchool = rs.getCell(j++, i).getContents().trim();                // 学制
                            String cmTid = rs.getCell(j++, i).getContents().trim();                   // 教师
                            String cmSchooltime = rs.getCell(j++, i).getContents().trim();           // 上课时间
                            String cmContent = rs.getCell(j++, i).getContents().trim();             // 教学主要内容
                            String cmTuition = rs.getCell(j++, i).getContents().trim();             // 全年学费(元)
                            long cmTuitionlong=Long.parseLong(cmTuition);
                            String youngOrOld = rs.getCell(j++, i).getContents().trim();            // 点石艺校还是老年大学

                            CoursesManage coursesManage = new CoursesManage();
                            coursesManage.setCmCourses(cmCourses);
                            coursesManage.setCmType(cmType);
                            coursesManage.setCmSchool(cmSchool);
                            coursesManage.setCmTid(cmTid);
                            coursesManage.setCmSchooltime(cmSchooltime);
                            coursesManage.setCmContent(cmContent);
                            coursesManage.setCmTuition(cmTuitionlong);
                            coursesManage.setYoungOrOld(youngOrOld);

                            coursesManageService.add(coursesManage);
                        }
                    }
                }
                File file = new File(fileName);
                if (file.exists()) {
                    file.delete();
                }
            } catch (Exception e) {
                File file = new File(fileName);
                if (file.exists()) {
                    file.delete();
                }
                e.printStackTrace();
            }

        }

}
3.若报unable to recognize ole stream错误,则打开excel表格,另存为.xls格式

 

https://www.cnblogs.com/lxnv587/p/7895567.html

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值