现在的位置: 首页 > 综合 > 正文

数据库数据导出成excel,excel数据导入数据库

2018年04月30日 ⁄ 综合 ⁄ 共 3632字 ⁄ 字号 评论关闭

一,把数据库的内容导出成excel文件(POI)

@RequestMapping(params="method=downXls")

    public voiddownXls(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException, BiffException{

        //得到数据里面的内容,可以确定要导出哪些数据,要导出哪些字段,要导入什么信息

        List<Emps> empList = empService.getAllEmps();

        HSSFWorkbook wb = new HSSFWorkbook();//新建一个workBook,这里就相当于一个excel文件

        HSSFSheet sheet =wb.createSheet();//创建一个工作簿

   

//设置每列的宽度

        sheet.setColumnWidth(0, 2000);

        sheet.setColumnWidth(1, 4000);

        sheet.setColumnWidth(2, 5000);

        //字体

        HSSFFont font = workbook.createFont();

        font.setBoldweight((short)1000);

        font.setFontName("隶书");

        font.setFontHeightInPoints((short)15); //设置字体大小

        //单元格样式

        HSSFCellStyle cellStyle = workbook.createCellStyle();

         cellStyle.setFont(font);

        //给每个单元格指定样式以及内容

         HSSFCell cell0 = titleRow.createCell(0);

        cell0.setCellStyle(cellStyle);

        cell0.setCellValue(new HSSFRichTextString("职工号"));

 

        HSSFCell cell1 = titleRow.createCell(1);

        cell1.setCellStyle(cellStyle);

        cell1.setCellValue(new HSSFRichTextString("职工姓名"));

       

        HSSFCell cell2 = titleRow.createCell(2);

        cell2.setCellStyle(cellStyle);

        cell2.setCellValue(newHSSFRichTextString("部门名字"));

       

        int num = empList.size();

        for(int i = 1 ; i< num ; i++){

            HSSFRow row =sheet.createRow(i);//创建行

            row.createCell((short)0).setCellValue(newHSSFRichTextString(empList.get(i-1).getEmpId()+""));//设置没一行每个单元格的值

            row.createCell((short)1).setCellValue(newHSSFRichTextString(empList.get(i-1).getEmpName()));

            row.createCell((short)2).setCellValue(newHSSFRichTextString(empList.get(i-1).getDep().getDepName()));

        }

       

//增加一个最后一行

        HSSFRow row = sheet.createRow(num+1);

        HSSFCell footCell = row.createCell(0);

        footCell.setCellValue(new HSSFRichTextString("合计:"));

        //设置最后一行的样式

        HSSFCellStyle style = workbook.createCellStyle();

        style.setAlignment(HSSFCellStyle.ALIGN_LEFT); //内容左对齐

        footCell.setCellStyle(style);

       

        //合并单元格(第一个单元格的行数,第二个单元格的行数,第一个单元格的列,第二个单元格的列)

        sheet.addMergedRegion(newCellRangeAddress(num+1,num+1,0,2));

 

//导出成excel形式的,用流的形式下载的方式

        response.addHeader("Content-Disposition", "attachment;filename=myFile.xls");

        response.addHeader("Content-type", "application/vnd.ms-excel");

 

        try{

        OutputStream os =response.getOutputStream();

        wb.write(os);

        os.close();

        }catch(Exception e){

            e.printStackTrace();

        }

    }

 

 

二,把excel文件导入的数据库库(JXL)

@RequestMapping(params="method=upXls")
public void upXls(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException, BiffException{
List<Book> bookList = new ArrayList<Book>();
File file = new File("D://book.xls");//找到要导入的excel文件
jxl.Workbook wb = Workbook.getWorkbook(file);//通过要导入的excel文件来生成一个workbook
int sheetNum = wb.getSheets().length;
for(int i = 0 ; i < sheetNum ; i++){
Sheet sheet = wb.getSheet(i);
int rowNum = sheet.getRows();
//如果有标题列名的话,需要从第一行算起,不能包括第一行
for(int j = 1 ; j < rowNum ; j++){
//这里注意的是,sheet.getCell(列数,行数),不要把里面的参数搞反了
//因为在数据库的id的自动增长的,所以在excel的这一列序号不需要写进来
//String cellString1 = sheet.getCell(0, j).getContents();
String cellString2 = sheet.getCell(1, j).getContents();
String cellString3 = sheet.getCell(2, j).getContents();
//创建一个临时对象,而且用一个list来保存,然后到DAO层批量的插入
Book book = new Book();
book.setBname(cellString2);
book.setBtype(Integer.parseInt(cellString3));
bookList.add(book);
}
try{
//在到曾批量插入的时候要考虑一个问题就是内存泄漏的问题
//因为save方法会加入到缓存中
bookService.insertIntoSQL(bookList);
}catch (Exception e) {
e.printStackTrace();
}
showBooks(request, response);
}
}
//在DAO层的批量增加
public void saveAll(List<Book> bookList){
final List<Book> finalBookList = bookList;
getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int num = finalBookList.size();
for(int i = 0 ; i < num ; i++){
   save(finalBookList.get(i));
   if(i%20==0){
   session.flush();
   session.clear();
   }
}
session.close();
return null;
}
});

用命令行导入excel数据到数据库

首先要把excel另存为成  文本文件(制表符分隔)

mysql>load data local infile 'D:\data.txt' into table exceltomysql fields terminated by '\t'; 

抱歉!评论已关闭.