Controler層
@RequestMapping(params = "reportExcelFile", method = RequestMethod.POST)
public void reportExcelFile(@Valid @ModelAttribute CNUDSS010Dto01 dto,
HttpServletRequest request,HttpServletResponse response, HttpSession session) throws IOException{
try {
CNUDSS010Dto01 dto01 = (CNUDSS010Dto01)session.getAttribute("CNUDSS010Dto");
// 畫面再表示
//setComboxAttributes(dto);
//setSelectFileList(dto);
//テンプレートブックを取得して設定する
String dPath = messageSource.getMessage("CNUDSS0001.TEMPLATE_FILE_PATH", null, LocaleContextHolder.getLocale());
String absPath = request.getSession().getServletContext().getRealPath("/");
String strTempFilePath = absPath + File.separator + dPath;
String strTempFileNm = messageSource.getMessage("CNUDSS0001.TEMPLATE_FILE_NAME",
null, LocaleContextHolder.getLocale());
//出力処理を実行する
byte[] content = serviceOutput.reportExcelFile(dto01,strTempFilePath,strTempFileNm);
InputStream is = new ByteArrayInputStream(content);
//システム日時
Date day=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
String sNow=df.format(day);
// 社員番号
String strUserId = SystemInfoHolder.getSystemInfo().getCbmUser().getShainNo();
//ファイル名:ファイル詳細ランキング_社員番号_YYYYMMDDHHMMSS.xlsx
String ExcelName=messageSource.getMessage("CNUDSS0001.EXCEL_HEAD_NAME", null, LocaleContextHolder.getLocale());
String outputFileNm=ExcelName +"_" + strUserId +"_" + sNow ;
//responseセット
response.reset();
response.setContentType("application/x-xls;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(outputFileNm, "UTF-8") + ".xlsx");
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
if (is!=null) {
is.close();
}
if (out !=null) {
out.close();
}
}
}catch(Exception ex) {
//処理なし
}
return;
}
Service層
package jp.co.fujita.cbm.knowledge.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import jp.co.fujita.cbm.knowledge.dto.CNUDSS010Dto01;
import jp.co.fujita.cbm.knowledge.dto.CNUDSS010Dto02;
@Service
public class CNUDSR0001ServiceImpl implements CNUDSR0001Service {
@Autowired
CNUDSS010Service service;
@Autowired
MessageSource messageSource;
public byte[] reportExcelFile(CNUDSS010Dto01 paramDto, String strTempFilePath, String strTempFileNm)
throws IOException {
// 閲覧數ランキング一覧リスト
ArrayList listEtsuransu = paramDto.getMikayoResultList();
// テンプレートブックを取得する
File tmpfile = new File(strTempFilePath + File.separator + strTempFileNm);
FileInputStream in = new FileInputStream(tmpfile);
XSSFWorkbook toWb = new XSSFWorkbook(in);
XSSFSheet toWs = toWb.getSheetAt(0);
// シート名前を変更する
String sheetName = messageSource.getMessage("CNUDSS0001.SHEET_NAME", null, LocaleContextHolder.getLocale());
toWb.setSheetName(0, sheetName);
// ヘッダ部にラベルを記入する
writeHead(toWs);
int headerRowCnt = 9;
int maxColumn = 11;
int maxRow = listEtsuransu.size() + headerRowCnt;
XSSFRow srcRow;
XSSFRow destRow;
srcRow = toWs.getRow(9);
Map mapCellStyle = new HashMap();
for (int j = srcRow.getFirstCellNum(); j < srcRow.getLastCellNum(); j++) {
//セルのタイプが設定される
CellStyle fromStyle = srcRow.getCell(j).getCellStyle();
CellStyle style = toWb.createCellStyle();
style.cloneStyleFrom(fromStyle);
//style.setWrapText(true);
mapCellStyle.put(j, style);
}
// 空白行を作成する
for (int i = 10; i < maxRow; i++) {
srcRow = toWs.getRow(9);
destRow = toWs.createRow(i);
if (srcRow != null) {
// 結果行をコピーする
copyRow(srcRow, destRow, mapCellStyle);
}
}
// 改ページを設定する
toWb.setPrintArea(0, 0, maxColumn, 0, maxRow);
String StrDutyCategory = "";
String StrEnterpriseCategory = "";
String StrFileCategory = "";
if (paramDto.getEnterpriseSubCategoryStr() != null && paramDto.getDutyCategoryStr() != null
&& paramDto.getFileCategoryStr() != null) {
String[] strFileArr = paramDto.getFileCategoryStr().split("//");
String[] strDutyArr = paramDto.getDutyCategoryStr().split("//");
String[] strEnterpriseArr = paramDto.getEnterpriseCategoryStr().split("//");
String[] orderEnterpriseDetail;
String[] orderDutyDetail;
String[] orderFileDetail;
if (paramDto.getEnterpriseCategory() != null) {
// 事業カテゴリのセッション情報設定
for (int i = 0; i < strEnterpriseArr.length; i++) {
orderEnterpriseDetail = strEnterpriseArr[i].split("##");
if (orderEnterpriseDetail.length > 0) {
if (paramDto.getEnterpriseCategory().trim().equals(orderEnterpriseDetail[0].trim())) {
StrEnterpriseCategory = orderEnterpriseDetail[1];
break;
}
}
}
}
if (paramDto.getDutyCategory() != null) {
// 職務カテゴリのセッション情報設定
for (int i = 0; i < strDutyArr.length; i++) {
orderDutyDetail = strDutyArr[i].split("##");
if (orderDutyDetail.length > 0) {
if (paramDto.getEnterpriseCategory().trim().equals(orderDutyDetail[0].trim())) {
if (paramDto.getEnterpriseSubCategory().trim().equals(orderDutyDetail[1].trim())) {
if (paramDto.getDutyCategory().trim().equals(orderDutyDetail[2].trim())) {
StrDutyCategory = orderDutyDetail[3];
break;
}
}
}
}
}
}
if (paramDto.getFileCategory() != null) {
// ファイルカテゴリのセッション情報設定
for (int i = 0; i < strFileArr.length; i++) {
orderFileDetail = strFileArr[i].split("##");
if (orderFileDetail.length > 0) {
if (paramDto.getFileCategory().trim().equals(orderFileDetail[0].trim())) {
StrFileCategory = orderFileDetail[1];
break;
}
}
}
}
}
// ダウンロード日
setCellValue(toWs, 1, 10, new SimpleDateFormat("yyyy/MM/dd").format(new Date()));
// ファイルカテゴリ
setCellValue(toWs, 3, 2, StrFileCategory);
// 事業種別
setCellValue(toWs, 4, 2, StrEnterpriseCategory);
// 職務種別
setCellValue(toWs, 5, 2, StrDutyCategory);
// 集計期間(YYYY/MM)
String strkikanFrom = String.valueOf(paramDto.getPeriodFrom());
String strkikanTo = String.valueOf(paramDto.getPeriodTo());
if ("".equals(strkikanTo)) {
strkikanTo = new SimpleDateFormat("yyyy/MM").format(new Date());
}
setCellValue(toWs, 7, 2, strkikanFrom + "~" + strkikanTo);
int RowIndex;
XSSFRow tempRow;
// 閲覧數ランキングを出力する
for (int i = 0; i < listEtsuransu.size(); i++) {
RowIndex = i + headerRowCnt;
tempRow = toWs.getRow(RowIndex);
// NO
tempRow.getCell(0).setCellValue(i + 1);
// ファイル名
tempRow.getCell(1).setCellValue(listEtsuransu.get(i).getFileName().trim());
if (listEtsuransu.get(i).getViews() != null) {
// 閲覧回數
tempRow.getCell(2).setCellValue(listEtsuransu.get(i).getViews());
} else {
tempRow.getCell(2).setCellValue("");
}
if (listEtsuransu.get(i).getComments() != null) {
// コメント數
tempRow.getCell(3).setCellValue(listEtsuransu.get(i).getComments());
} else {
tempRow.getCell(3).setCellValue("");
}
if (listEtsuransu.get(i).getPoint() != null) {
// ポイント
tempRow.getCell(4).setCellValue(listEtsuransu.get(i).getPoint());
} else {
tempRow.getCell(4).setCellValue("");
}
if (listEtsuransu.get(i).getFileCategory() != null) {
// ファイルカテゴリ
tempRow.getCell(5).setCellValue(listEtsuransu.get(i).getFileCategory().trim());
} else {
tempRow.getCell(5).setCellValue("");
}
if (listEtsuransu.get(i).getLanguage() != null) {
// 言語
tempRow.getCell(6).setCellValue(listEtsuransu.get(i).getLanguage().trim());
} else {
tempRow.getCell(6).setCellValue("");
}
if (listEtsuransu.get(i).getDocumentCreator() != null) {
// ドキュメント作成者
tempRow.getCell(7).setCellValue(listEtsuransu.get(i).getDocumentCreator().trim());
} else {
tempRow.getCell(7).setCellValue("");
}
if (listEtsuransu.get(i).getExpirationDate() != null) {
// 有効期限
tempRow.getCell(8).setCellValue(listEtsuransu.get(i).getExpirationDate().trim());
} else {
tempRow.getCell(8).setCellValue("");
}
if (listEtsuransu.get(i).getRegistrationDate() != null) {
// 登録日
tempRow.getCell(9).setCellValue(listEtsuransu.get(i).getRegistrationDate().trim());
} else {
tempRow.getCell(9).setCellValue("");
}
if (listEtsuransu.get(i).getUpdateDate() != null) {
// 更新日
tempRow.getCell(10).setCellValue(listEtsuransu.get(i).getUpdateDate().trim());
} else {
tempRow.getCell(10).setCellValue("");
}
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
toWb.write(os);
toWb.close();
} catch (IOException e) {
throw e;
}finally {
if (os != null) {
os.close();
}
if (toWb !=null ) {
toWb.close();
}
}
byte[] content = os.toByteArray();
return content;
}
public static void copyRow(XSSFRow srcRow, XSSFRow destRow,Map mapCellStyle) {
XSSFCell oldCell;
XSSFCell newCell;
for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
oldCell = srcRow.getCell(j); // old cell
newCell = destRow.getCell(j); // new cell
if (oldCell != null) {
if (newCell == null) {
newCell = destRow.createCell(j);
}
if (mapCellStyle.get(oldCell.getColumnIndex()) != null) {
newCell.setCellStyle(mapCellStyle.get(oldCell.getColumnIndex()));
newCell.setCellType(CellType.BLANK);
}
}
}
}
public static CellRangeAddress getMergedRegion(XSSFSheet sheet, int rowNum, short cellNum) {
CellRangeAddress merged;
for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
merged = sheet.getMergedRegion(i);
if (merged.isInRange(rowNum, cellNum)) {
return merged;
}
}
return null;
}
public void writeHead(XSSFSheet destSheet) {
String strHead;
// 【未活用データ一覧】
strHead = messageSource.getMessage("CNUDSS0001.LA_NONE_USED_DATA_LIST", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 1, 0, strHead); // A2
// ファイルカテゴリ:
strHead = messageSource.getMessage("CNUDSS0001.LA_FILE_CATEGORY_LABEL", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 3, 1, strHead); // E4
// 事業種別:
strHead = messageSource.getMessage("CNUDSS0001.LA_BUSINESS_TYPE_LABEL", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 4, 1, strHead); // E5
// 職務種別:
strHead = messageSource.getMessage("CNUDSS0001.LA_JOB_TYPE_LABEL", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 5, 1, strHead); // E6
// 集計期間(YYYY/MM):
strHead = messageSource.getMessage("CNUDSS0001.LA_SELECT_PERIOD_LABEL", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 7, 0, strHead); // A8
// No
strHead = messageSource.getMessage("CNUDSS0001.LA_NO_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 0, strHead); // A11
// ファイル名
strHead = messageSource.getMessage("CNUDSS0001.LA_FILE_NAME_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 1, strHead); // B11
// 閲覧回數
strHead = messageSource.getMessage("CNUDSS0001.LA_VIEWS_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 2, strHead); // F11
// コメント數
strHead = messageSource.getMessage("CNUDSS0001.LA_COMMENTS_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 3, strHead); // J11
// ポイント
strHead = messageSource.getMessage("CNUDSS0001.LA_POINT_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 4, strHead); // N11
// ファイルカテゴリ
strHead = messageSource.getMessage("CNUDSS0001.LA_FILE_CATEGORY_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 5, strHead); // V11
// 言語
strHead = messageSource.getMessage("CNUDSS0001.LA_LANGUAGE_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 6, strHead); // AC11
// ドキュメント作成者
strHead = messageSource.getMessage("CNUDSS0001.LA_DOCUMENT_CREATOR_HEAD", null,
LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 7, strHead); // AG11
// 有効期限
strHead = messageSource.getMessage("CNUDSS0001.LA_EXPIRATION_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 8, strHead); // AK11
// 登録日
strHead = messageSource.getMessage("CNUDSS0001.LA_REGISTRATION_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 9, strHead); // AL11
// 更新日
strHead = messageSource.getMessage("CNUDSS0001.LA_UPDATE_HEAD", null, LocaleContextHolder.getLocale());
setCellValue(destSheet, 8, 10, strHead); // AQ11
}
public void setCellValue(XSSFSheet destSheet, int rowIndex, int ColIndex, String strHead) {
XSSFRow headRow = destSheet.getRow(rowIndex);
if (headRow == null) {
headRow = destSheet.createRow(rowIndex);
}
XSSFCell headCell = headRow.getCell(ColIndex);
if (headCell == null) {
headCell = headRow.createCell(ColIndex);
}
headCell.setCellValue(strHead);
}
}