java poi 병합 유닛 테이블(도와주세요)
IContextDictionary?dictionary, ?DataRowCollections?rows,
String?fileName, ?Listlt; Stringgt; ?fields, ?Listlt; Stringgt; ?fieldsName)?{ int?cellSize?=?fields.size(); if(cellSize?==?0){
LogManager.debug("지정된 열 헤더 정보가 없습니다. Excel 파일을 내보낼 수 없습니다!");
return; } //============스타일 만들기 start HSSFWorkbook?workbook?=?new?HSSFWorkbook(); HSSFSheet? sheet ?=?workbook.createSheet(); //열 헤더 글꼴 스타일
HSSFFont?headerFont?=?workbook.createFont();
headerFont.setFontName("宋体") ;
headerFont.setFontHeightInPoints((short)?12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//열 헤더 스타일
HSSFCellStyle?headerStyle?=?workbook.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//?center 왼쪽 및 오른쪽
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 위쪽 및 아래쪽 중앙
headerStyle.setLocked(true);
headerStyle.setWrapText (true);
//제목 스타일
HSSFFont?titleFont?=?workbook.createFont();
titleFont.setFontName("宋体");
titleFont.setFontHeightInPoints((short)?15);
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle?titleStyle?=?workbook.createCellStyle ();
p>
제목
tyle.setFont(titleFont);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
titleStyle.setLocked( true);
titleStyle.setWrapText(true);
//일반 셀 글꼴 스타일
HSSFFont?cellFont?=?workbook.createFont()
cellFont.setFontName("宋体");
cellFont.setFontHeightInPoints((short)12);
//일반 셀 스타일
HSSFCellStyle?cellStyle?=?workbook.createCellStyle();
cellStyle.setFont(cellFont);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT) //?
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 중앙 위 및 아래
cellStyle.setLocked(true);
cellStyle.setWrapText(true) ;
//===========스타일 만들기 end
//일련번호 열, 열 너비 및 제목 행 설정?start
HSSFRow?titleRow?=?sheet.createRow(0);
titleRow.setHeightInPoints(50);
HSSFCell?titleCell?=?titleRow.createCell(0);
titleCell.setCellValue(new?HSSFRichTextString(fileName));
titleCell.setCellStyle(titleStyle);
//sheet.addMergedRegion(new?Region( 0, ( short)0, 0, (short)cellSize)); //셀 병합--메서드가 더 이상 사용되지 않습니까?
//CellRangeAddress?시작 행?끝 행?시작 열?끝 열
sheet.addMergedRegion(new?CellRangeAddress(0, 0, (short)0, (short)cellSize)); //셀 병합
//총 데이터 수 표시?start
HSSFRow?countRow?=?sheet.createRow(1);
countRow.setHeightInPoints(40);
HSSFCell?countCell?=?countRow.createCell( 0);
countCell.setCellValue(new?HSSFRic
hTextString("***카운트 특수 검사("rows.size() ") 항목"));
countCell.setCellStyle(headerStyle);
sheet.addMergedRegion(new? CellRangeAddress(1, 1, (short)0, (short)cellSize)); // 셀 병합 // 총 데이터 수 표시?end?
HSSFRow?headerRow?=?sheet.createRow ( 2);
headerRow.setHeightInPoints(35);
HSSFCell?headerCell?=?null;
//일련번호
int?startIndex?=?0?; headerCell?=?headerRow.createCell(0); sheet.setColumnWidth(0,?2000); headerCell.setCellValue(new?HSSFRichTextString("일련 번호")); ) ; startIndex? ?; //열 헤더 for(int?i?=?0; ?i?lt; ?cellSize; ?i? ){
sheet.setColumnWidth(startIndex? ?i, ? 7000 );
headerCell?=?headerRow.createCell(startIndex? ?i);
headerCell.setCellValue(new?HSSFRichTextString(fields.get(i)));
p >
headerCell.setCellStyle(headerStyle) } //일련 번호 열, 열 너비 및 헤더 행 끝을 설정합니다. //파일 텍스트?start int?rowNum?=?1; ?row?=?null; 정수[]gt;?new?ArrayListlt; 정수?[]?arr?=?null; ; String? orgName?=?"";? for(int?j?=?2;?jlt;rows.size() 2;?j){//루프 행
DataRow?dataRow? =?rows .get(rowIndex);?//해당 데이터베이스 필드
HSSFCell?cell?=?null;
row?=?sheet.createRow(j? ?1) ; 행.setHeightInPoints(55);
//일련 번호
cell?=?row.createCell(0);
cell.setCellValue(rowNum) ; if(StringHelper.isNullOrEmpty(orgName)){
arr?=?Integer[2];
>
arr[0]?=?j? ?1;
l?=j? ?1;
orgName?=?dataRow.getString("ORGNAME") ; }else{
if(!orgName.equals(dataRow.getString("ORGNAME"))){
arr[1]?=?j;
cellRangeLst.add(arr);
sheet.addMergedRegion(new?CellRangeAddress(l,j,1,1));
arr?=?new?Integer[2 ];
l?=?j 1;
orgName?=?dataRow.getString("ORGNAME");
}
if(rowIndex?==?rows.size()?-?1){
arr[1]?=?j 1;
cellRangeLst.add(arr);
sheet.addMergedRegion(new?CellRangeAddress(l,j 1,1,1));
} } for(int?k?=?0;?k?lt; ?cellSize;?k ){
cell?=?row.createCell(k? ?startIndex);
String?column?=?fieldsName.get(k);?/ /해당 데이터베이스 필드
String?value?=?"";
if("APSJ".equals(column)){
value?=? getAPSJValue(dataRow.getString(column));
}else{
value?=?dataRow.getString(column);
}
cell.setCellValue(new?HSSFRichTextString(value)); cell.setCellStyle(cellStyle); } rowIndex? } //파일 텍스트?end //for(Integer[]?te?:?cellRangeLst){ // ?sheet.addMergedRegion(new?CellRangeAddress(te[0], te[1], 1, 1)); //룸 셀 병합 //} //HttpServletResponse?response?=?context.getResponse() 응답을 다운로드합니다. setContentType("application/x-download; charset=UTF-8"); String?title?=?"export"; 시도하시겠습니까?{
title?=?java.net.URLEncoder.encode(fileName ,?"UTF-8"); }?catch?(UnsupportedEncodingException?e)?{
e.printStackTrace() } response.addHeader("Content-Disposition
",?"attachment; filename="? ?title? ?".xls"); try?{
OutputStream?out?=?response.getOutputStream();
통합문서 .write(out);
out.flush();
out.close(); }?catch?(IOException?e)?{
e.printStackTrace(); } }
//참고해주세요