데이터베이스 데이터는 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';
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Spring에서 DAO가 순환 호출될 때 데이터가 실시간으로 업데이트되지 않는 해결 방법문제를 설명하기 전에 몇 가지 전제 사항을 설명하십시오. Spring의 구성 파일에서 다음과 같은 방식으로 데이터베이스 트랜잭션을 구성했다고 가정하십시오. 현재 UserDao 및 Security Service가 있습...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.