package board.model;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
public class ExcelView extends AbstractExcelView{
@Override
protected void buildExcelDocument(Map<String,Object> ModelMap,HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception{
String excelName = ModelMap.get("target").toString();
HSSFSheet worksheet = null;
HSSFRow row = null;
if(excelName.equals("article")){
excelName=URLEncoder.encode("게시글","UTF-8");
worksheet = workbook.createSheet(excelName+ " WorkSheet");
@SuppressWarnings("unchecked")
List<BoardArticle> list = (List<BoardArticle>)ModelMap.get("excelList");
row = worksheet.createRow(0);
row.createCell(0).setCellValue("글번호");
row.createCell(1).setCellValue("제목");
row.createCell(2).setCellValue("날짜");
row. createCell(3).setCellValue("글쓴이");
row.createCell(4).setCellValue("비밀번호");
row.createCell(5).setCellValue("내용");
for(int i=1;i<list.size()+1;i++){
row = worksheet.createRow(i);
row.createCell(0).setCellValue(list.get(i-1).getArticleNo());
row.createCell(1).setCellValue(list.get(i-1).getArticleTitle());
row.createCell(2).setCellValue(list.get(i-1).getArticleDate());
row.createCell(3).setCellValue(list.get(i-1).getArticleWriter());
row.createCell(4).setCellValue(list.get(i-1).getArticlePw());
row.createCell(5).setCellValue(list.get(i-1).getArticleContent());
}
}
if(excelName.equals("comment")){
@SuppressWarnings("unchecked")
List<BoardComment> list = (List<BoardComment>)ModelMap.get("excelList");
excelName=URLEncoder.encode("댓글","UTF-8");
worksheet = workbook.createSheet(excelName+ " WorkSheet");
row = worksheet.createRow(0);
row.createCell(0).setCellValue("댓글번호");
row.createCell(1).setCellValue("게시글번호");
row.createCell(2).setCellValue("날짜");
row.createCell(3).setCellValue("글쓴이");
row.createCell(4).setCellValue("비밀번호");
row.createCell(5).setCellValue("내용");
for(int i=1;i<list.size()+1;i++){
row = worksheet.createRow(i);
row.createCell(0).setCellValue(list.get(i-1).getCommentNo());
row.createCell(1).setCellValue(list.get(i-1).getArticleNo());
row.createCell(2).setCellValue(list.get(i-1).getCommentDate());
row.createCell(3).setCellValue(list.get(i-1).getCommentWriter());
row.createCell(4).setCellValue(list.get(i-1).getCommentPw());
row.createCell(5).setCellValue(list.get(i-1).getCommentContent());
}
}
if(excelName.equals("file")){
@SuppressWarnings("unchecked")
List<BoardFile> list = (List<BoardFile>)ModelMap.get("excelList");
excelName=URLEncoder.encode("파일","UTF-8");
worksheet = workbook.createSheet(excelName+ " WorkSheet");
row = worksheet.createRow(0);
row.createCell(0).setCellValue("파일번호");
row.createCell(1).setCellValue("게시글번호");
row.createCell(2).setCellValue("파일경로");
row.createCell(3).setCellValue("파일이름");
row.createCell(4).setCellValue("파일크기");
for(int i=1;i<list.size()+1;i++){
row = worksheet.createRow(i);
row.createCell(0).setCellValue(list.get(i-1).getFileNo());
row.createCell(1).setCellValue(list.get(i-1).getArticleNo());
row.createCell(2).setCellValue(list.get(i-1).getFilePath());
row.createCell(3).setCellValue(list.get(i-1).getFileName());
row.createCell(4).setCellValue(list.get(i-1).getFileSize());
}
}
response.setContentType("Application/Msexcel");
response.setHeader("Content-Disposition", "ATTachment; Filename="+excelName+"-excel");
}
}