데이터베이스 데이터는 excel로 내보내고 excel 데이터는 데이터베이스로 가져옵니다

1, 데이터베이스의 내용을 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';

좋은 웹페이지 즐겨찾기