1. 程式人生 > 實用技巧 >POI -EXCEL匯出案例

POI -EXCEL匯出案例

myself寫的一個Excel匯出案例:

 /**
     * 評估委員會評估統計結果表列印介面
     *
     * @param assessId 預報名id
     * @return 匯出資料內容
     * @throws Exception
     */
    @Service(url = "/socialorg/api/assessment/print/grade/count", method = HttpMethod.get)
    public InputFile printGradeCount(Integer assessId) throws Exception {
        
if (assessId != null) { //評估工作名稱 AssessWork assessWork = socialOrgServiceDao.load(AssessWork.class, assessId); //獲取評估工作選擇的委員 List<Member> memberList = socialOrgServiceDao.getMembersByCondition(assessWork.getAssessId()); String exportFileName
= assessWork.getPublishName() + "評審會等級統計表.xls"; //建立一個Excel檔案 HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Sheet1"); //建立單元格樣式 HSSFCellStyle headStyle = createStyle(workbook, 3); HSSFCellStyle titleStyle
=createStyle(workbook, 1); HSSFCellStyle footStyle =createStyle(workbook, 2); //處理表頭 createTh(sheet, headStyle, titleStyle, exportFileName, memberList); //獲取社會組織資料和評估等級 List<AssessForecast> orgList = assessForecastDao.exportOrgInfoByAssessId(assessId); List<DictItem> dictItemList = socialOrgDao.getDictItemList("C111"); //從第三行開始打印出資料 int insertRowIndex = 2; //序號 int rowNum = 0; int memberSum = memberList.size(); for (AssessForecast forecast : orgList) { //把第二行開始到結束那行的第1行到7行的單元格合併 rowNum++; sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 0, 0)); sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 1, 1)); sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 2, 2)); sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 3, 3)); sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 4, 4)); sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 5, 5)); sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 6, 6)); //1、建立一行儲存社會組織資訊 Row newRow = setCellStyle(sheet, headStyle, insertRowIndex, memberSum); newRow.getCell(0).setCellValue(rowNum); newRow.getCell(1).setCellValue(forecast.getOrgan().getOrgName()); if (forecast.getForecastScore() != null) { newRow.getCell(2).setCellValue( Null.isNull(forecast.getForecastScore().getSelfScore()) ? 0 : forecast.getForecastScore().getSelfScore()); newRow.getCell(3).setCellValue( Null.isNull(forecast.getForecastScore().getInitScore()) ? 0 : forecast.getForecastScore().getInitScore()); newRow.getCell(4).setCellValue( Null.isNull(forecast.getForecastScore().getReducePoint()) ? 0 : forecast.getForecastScore().getReducePoint()); newRow.getCell(5).setCellValue( Null.isNull(forecast.getForecastScore().getAddPoint()) ? 0 : forecast.getForecastScore().getAddPoint()); //無複評時,展示初評得分 newRow.getCell(6).setCellValue( Null.isNull(forecast.getForecastScore().getReviewScore()) ? forecast.getForecastScore() .getInitScore() : forecast.getForecastScore().getReviewScore()); } //2、評定等級設定 newRow.getCell(7).setCellValue(dictItemList.get(0).getItemName()); Row fourRow = setCellStyle(sheet, headStyle, insertRowIndex + 1, memberSum); fourRow.getCell(7).setCellValue(dictItemList.get(1).getItemName()); Row threeRow = setCellStyle(sheet, headStyle, insertRowIndex + 2, memberSum); threeRow.getCell(7).setCellValue(dictItemList.get(2).getItemName()); Row twoRow = setCellStyle(sheet, headStyle, insertRowIndex + 3, memberSum); twoRow.getCell(7).setCellValue(dictItemList.get(3).getItemName()); Row oneRow = setCellStyle(sheet, headStyle, insertRowIndex + 4, memberSum); oneRow.getCell(7).setCellValue(dictItemList.get(4).getItemName()); //3、設定專家評分 int fiveSum = 0, fourSum = 0, threeSum = 0, twoSum = 0, oneSum = 0; for (int k = 0; k < memberList.size(); k++) { //專家列的下標 int expertIndex = 8 + k; //合計列的下標 int tatalIndex = 8 + memberSum; Member member = memberList.get(k); MemberView memberView = socialOrgServiceDao.getMembersViewByCondition(member.getMemberId(), forecast.getForecastId()); if (memberView.getDeclaredLevelId() != null) { if (memberView.getDeclaredLevelId() == 731) { newRow.getCell(expertIndex).setCellValue("√"); //4、合計 fiveSum++; newRow.getCell(tatalIndex).setCellValue(fiveSum); } else if (memberView.getDeclaredLevelId() == 732) { fourRow.getCell(expertIndex).setCellValue("√"); fourSum++; fourRow.getCell(tatalIndex).setCellValue(fourSum); } else if (memberView.getDeclaredLevelId() == 733) { threeRow.getCell(expertIndex).setCellValue("√"); threeSum++; threeRow.getCell(tatalIndex).setCellValue(threeSum); } else if (memberView.getDeclaredLevelId() == 734) { twoRow.getCell(expertIndex).setCellValue("√"); twoSum++; twoRow.getCell(tatalIndex).setCellValue(twoSum); } else { oneRow.getCell(expertIndex).setCellValue("√"); oneSum++; oneRow.getCell(tatalIndex).setCellValue(oneSum); } } } //5、設定最終等級,每行第14列為最終評定等級 if (forecast.getFinallyGradeId() != null) { int finalIndex = 8 + memberSum + 1; String itemName = forecast.getFinallyGrade().getItemName(); if (forecast.getFinallyGradeId() == 731) { newRow.getCell(finalIndex).setCellValue(itemName); } else if (forecast.getFinallyGradeId() == 732) { fourRow.getCell(finalIndex).setCellValue(itemName); } else if (forecast.getFinallyGradeId() == 733) { threeRow.getCell(finalIndex).setCellValue(itemName); } else if (forecast.getFinallyGradeId() == 734) { twoRow.getCell(finalIndex).setCellValue(itemName); } else { oneRow.getCell(finalIndex).setCellValue(itemName); } } //每個社會組織合併5行 insertRowIndex = insertRowIndex + 5; } //6、建立excel尾部 createTfoot(sheet, footStyle, insertRowIndex, memberSum); //匯出檔案 ByteArrayOutputStream out = new ByteArrayOutputStream(); workbook.write(out); return new InputFile(new Inputable.ByteInput(out.toByteArray()), exportFileName); } else { throw new ApiException("請求引數有誤,assessId引數為空!"); } } /** * 建立excel 頭部 * * @param sheet 表格 * @param headstyle 樣式 * @param exportFileName excel名字 * @param memberList 委員 * @throws Exception */ private void createTh(HSSFSheet sheet, HSSFCellStyle headstyle, HSSFCellStyle titleStyle, String exportFileName, List<Member> memberList) throws Exception { //合併單元格 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9 + memberList.size())); //建立第一行 HSSFRow row0 = sheet.createRow(0); //建立第一個單元格 HSSFCell cell1 = row0.createCell(0); //設定單元格內容 cell1.setCellValue(exportFileName.substring(0, exportFileName.indexOf(".xls"))); //設定單元格樣式 cell1.setCellStyle(titleStyle); //設定行高 row0.setHeight((short) 900); //建立第二行 HSSFRow row1 = sheet.createRow(1); HSSFCell cell0 = row1.createCell(0); cell0.setCellValue("序 號"); cell0.setCellStyle(headstyle); //設定行高 row1.setHeight((short) 1500); HSSFCell cell2 = row1.createCell(1); cell2.setCellValue("參評單位名稱"); cell2.setCellStyle(headstyle); HSSFCell cell3 = row1.createCell(2); cell3.setCellValue("參評單位自評分"); cell3.setCellStyle(headstyle); HSSFCell cell4 = row1.createCell(3); cell4.setCellValue("評估機構評分"); cell4.setCellStyle(headstyle); HSSFCell cell5 = row1.createCell(4); cell5.setCellValue("複審扣分"); cell5.setCellStyle(headstyle); HSSFCell cell6 = row1.createCell(5); cell6.setCellValue("複審加分"); cell6.setCellStyle(headstyle); HSSFCell cell7 = row1.createCell(6); cell7.setCellValue("評估得分"); cell7.setCellStyle(headstyle); HSSFCell cell8 = row1.createCell(7); cell8.setCellValue(" 評委" + "\n" + "等級"); cell8.setCellStyle(headstyle); //畫線(由左上到右下的斜線) 加入一條對角線 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 7, 1, (short) 7, 1); HSSFSimpleShape shape = patriarch.createSimpleShape(clientAnchor); shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); shape.setLineStyle(HSSFSimpleShape.LINESTYLE_SOLID); //專家資訊 int memberSum = memberList.size(); for (int i = 0; i < memberSum; i++) { Member member = memberList.get(i); HSSFCell cellMember = row1.createCell(8 + i); if (member.getEntrustFlag()) { cellMember.setCellValue(member.getMember().getExpertName() + "\n" + "(" + member.getEntrustUserName() + "委託)"); } else { cellMember.setCellValue(member.getMember().getExpertName()); } cellMember.setCellStyle(headstyle); } HSSFCell cell9 = row1.createCell(8 + memberSum); cell9.setCellValue("合計"); cell9.setCellStyle(headstyle); HSSFCell cell10 = row1.createCell(8 + memberSum + 1); cell10.setCellValue("最終評定等級"); cell10.setCellStyle(headstyle); } /** * 預設全部設定單元格的邊框樣式 * * @param sheet 表格 * @param headStyle 樣式 * @param insertRowIndex 構造的行數 * @return * @throws Exception */ public Row setCellStyle(HSSFSheet sheet, HSSFCellStyle headStyle, int insertRowIndex, int memberSum) throws Exception { Row newRow = sheet.createRow(insertRowIndex); getCellStyle(newRow, headStyle, 0); getCellStyle(newRow, headStyle, 1); getCellStyle(newRow, headStyle, 2); getCellStyle(newRow, headStyle, 3); getCellStyle(newRow, headStyle, 4); getCellStyle(newRow, headStyle, 5); getCellStyle(newRow, headStyle, 6); getCellStyle(newRow, headStyle, 7); //設定列寬 sheet.setColumnWidth(0, 2000); sheet.setColumnWidth(1, 6000); sheet.setColumnWidth(2, 4000); sheet.setColumnWidth(3, 4000); sheet.setColumnWidth(4, 2000); sheet.setColumnWidth(5, 2000); sheet.setColumnWidth(6, 2000); sheet.setColumnWidth(7, 5000); for (int i = 0; i < memberSum; i++) { getCellStyle(newRow, headStyle, 8 + i); sheet.setColumnWidth(8 + i, 5000); } getCellStyle(newRow, headStyle, 8 + memberSum).setCellValue(0); getCellStyle(newRow, headStyle, 8 + memberSum + 1); sheet.setColumnWidth(8 + memberSum, 2000); sheet.setColumnWidth(8 + memberSum + 1, 3000); return newRow; } /** * 建立excel尾部 * * @param sheet 表格 * @param footStyle 樣式 * @param insertRowIndex 插入的行數 * @param memeberSum 委員數量 */ public void createTfoot(HSSFSheet sheet, HSSFCellStyle footStyle, int insertRowIndex, int memeberSum) { sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 6, 0, 9 + memeberSum)); //建立第一行 HSSFRow row0 = sheet.createRow(insertRowIndex); //建立第一個單元格 HSSFCell cell1 = row0.createCell(0); //設定單元格內容 cell1.setCellValue("注:按照《廣西壯族自治區民政廳關於開展2019年度全區性社會組織評估工作的通知》(桂民函【2019】67號)要求,評估結果分為5個等級,由高至低依次為5A級(AAAAA)、4A" + "級(AAAA)、3A級(AAA)、2A級(AA)、1A級(A);等級評分劃定為5個標準,5A:950分以上,4A:949-900分,3A:899-850分,2A:849-800分,1A:799-750分。"); //設定單元格樣式 cell1.setCellStyle(footStyle); //1.統計員簽名 sheet.addMergedRegion(new CellRangeAddress(insertRowIndex + 8, insertRowIndex + 8, 0, 1)); //建立第一行 HSSFRow row2 = sheet.createRow(insertRowIndex + 8); //建立第一個單元格 HSSFCell cell2 = row2.createCell(0); cell2.setCellValue("統計員簽名:"); //設定單元格樣式 cell2.setCellStyle(footStyle); //2.評估委員會簽名 sheet.addMergedRegion(new CellRangeAddress(insertRowIndex + 10, insertRowIndex + 10, 0, 1)); //建立第一行 HSSFRow row3 = sheet.createRow(insertRowIndex + 10); row3.setHeight((short) 500); //建立第一個單元格 HSSFCell cell3 = row3.createCell(0); cell3.setCellValue("評估委員會簽名:"); //設定單元格樣式 cell3.setCellStyle(footStyle); HSSFRow row5 = sheet.createRow(insertRowIndex + 11); row5.setHeight((short) 500); HSSFRow row6 = sheet.createRow(insertRowIndex + 12); row6.setHeight((short) 500); HSSFRow row7 = sheet.createRow(insertRowIndex + 13); row7.setHeight((short) 500); HSSFRow row8 = sheet.createRow(insertRowIndex + 14); row8.setHeight((short) 500); HSSFRow row9 = sheet.createRow(insertRowIndex + 15); row9.setHeight((short) 500); //2.複核委員會簽名 sheet.addMergedRegion(new CellRangeAddress(insertRowIndex + 16, insertRowIndex + 16, 0, 1)); //建立第一行 HSSFRow row4 = sheet.createRow(insertRowIndex + 16); row4.setHeight((short) 500); //建立第一個單元格 HSSFCell cell4 = row4.createCell(0); cell4.setCellValue("複核委員會簽名:"); //設定單元格樣式 cell4.setCellStyle(footStyle); } /** * 建立頭部和尾部的樣式 * * @param workbook 表格 * @return * @throws Exception */ public HSSFCellStyle createStyle(HSSFWorkbook workbook, int type) throws Exception { //建立尾部單元格樣式 HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setLocked(false); //自動換行 cellStyle.setWrapText(true); HSSFFont font = workbook.createFont(); // 設定字型的高度 - 以1pt的1/20位單位 //設定excel資料字型大小 if (type == 1) { //左右居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //上下居中 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 設定字型的名字 font.setFontName("黑體"); font.setFontHeightInPoints((short) 20); font.setBoldweight((short) 800); } else if (type == 2){ //左右居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); //上下居中 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 設定字型的名字 font.setFontName("黑體"); font.setFontHeightInPoints((short) 16); font.setBoldweight((short) 600); } else { //設定邊框資訊 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); //左右居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //上下居中 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); font.setFontName("宋體"); //設定excel資料字型大小 font.setFontHeightInPoints((short) 14); font.setBoldweight((short) 600); cellStyle.setFont(font); } //設定字型 cellStyle.setFont(font); return cellStyle; } /** * 新建一個設定了單元格樣式的Cell * * @param newRow 行數 * @param style 樣式 * @param coloumNum 列數 * @return 單元格Cell */ public Cell getCellStyle(Row newRow, HSSFCellStyle style, int coloumNum) { //設定行高 newRow.setHeight((short) 700); Cell newCell = newRow.createCell(coloumNum); newCell.setCellStyle(style); return newCell; }

/**
* 評估委員會評估統計結果表列印介面
*
* @param assessId 預報名id
* @return 匯出資料內容
* @throws Exception
*/
@Service(url = "/socialorg/api/assessment/print/grade/count", method = HttpMethod.get)
public InputFile printGradeCount(Integer assessId) throws Exception {
if (assessId != null) {
//評估工作名稱
AssessWork assessWork = socialOrgServiceDao.load(AssessWork.class, assessId);
//獲取評估工作選擇的委員
List<Member> memberList = socialOrgServiceDao.getMembersByCondition(assessWork.getAssessId());

String exportFileName = assessWork.getPublishName() + "評審會等級統計表.xls";
//建立一個Excel檔案
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
//建立單元格樣式
HSSFCellStyle headStyle = createStyle(workbook, 3);
HSSFCellStyle titleStyle =createStyle(workbook, 1);
HSSFCellStyle footStyle =createStyle(workbook, 2);
//處理表頭
createTh(sheet, headStyle, titleStyle, exportFileName, memberList);

//獲取社會組織資料和評估等級
List<AssessForecast> orgList = assessForecastDao.exportOrgInfoByAssessId(assessId);
List<DictItem> dictItemList = socialOrgDao.getDictItemList("C111");
//從第三行開始打印出資料
int insertRowIndex = 2;
//序號
int rowNum = 0;
int memberSum = memberList.size();
for (AssessForecast forecast : orgList) {
//把第二行開始到結束那行的第1行到7行的單元格合併
rowNum++;
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 2, 2));
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 3, 3));
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 4, 4));
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 5, 5));
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 4, 6, 6));
//1、建立一行儲存社會組織資訊
Row newRow = setCellStyle(sheet, headStyle, insertRowIndex, memberSum);
newRow.getCell(0).setCellValue(rowNum);
newRow.getCell(1).setCellValue(forecast.getOrgan().getOrgName());
if (forecast.getForecastScore() != null) {
newRow.getCell(2).setCellValue(
Null.isNull(forecast.getForecastScore().getSelfScore()) ? 0 :
forecast.getForecastScore().getSelfScore());
newRow.getCell(3).setCellValue(
Null.isNull(forecast.getForecastScore().getInitScore()) ? 0 :
forecast.getForecastScore().getInitScore());
newRow.getCell(4).setCellValue(
Null.isNull(forecast.getForecastScore().getReducePoint()) ? 0 :
forecast.getForecastScore().getReducePoint());
newRow.getCell(5).setCellValue(
Null.isNull(forecast.getForecastScore().getAddPoint()) ? 0 :
forecast.getForecastScore().getAddPoint());
//無複評時,展示初評得分
newRow.getCell(6).setCellValue(
Null.isNull(forecast.getForecastScore().getReviewScore()) ? forecast.getForecastScore()
.getInitScore() : forecast.getForecastScore().getReviewScore());
}
//2、評定等級設定
newRow.getCell(7).setCellValue(dictItemList.get(0).getItemName());

Row fourRow = setCellStyle(sheet, headStyle, insertRowIndex + 1, memberSum);
fourRow.getCell(7).setCellValue(dictItemList.get(1).getItemName());

Row threeRow = setCellStyle(sheet, headStyle, insertRowIndex + 2, memberSum);
threeRow.getCell(7).setCellValue(dictItemList.get(2).getItemName());

Row twoRow = setCellStyle(sheet, headStyle, insertRowIndex + 3, memberSum);
twoRow.getCell(7).setCellValue(dictItemList.get(3).getItemName());

Row oneRow = setCellStyle(sheet, headStyle, insertRowIndex + 4, memberSum);
oneRow.getCell(7).setCellValue(dictItemList.get(4).getItemName());

//3、設定專家評分
int fiveSum = 0, fourSum = 0, threeSum = 0, twoSum = 0, oneSum = 0;
for (int k = 0; k < memberList.size(); k++) {
//專家列的下標
int expertIndex = 8 + k;
//合計列的下標
int tatalIndex = 8 + memberSum;
Member member = memberList.get(k);
MemberView memberView = socialOrgServiceDao.getMembersViewByCondition(member.getMemberId(),
forecast.getForecastId());
if (memberView.getDeclaredLevelId() != null) {
if (memberView.getDeclaredLevelId() == 731) {
newRow.getCell(expertIndex).setCellValue("√");
//4、合計
fiveSum++;
newRow.getCell(tatalIndex).setCellValue(fiveSum);

} else if (memberView.getDeclaredLevelId() == 732) {
fourRow.getCell(expertIndex).setCellValue("√");
fourSum++;
fourRow.getCell(tatalIndex).setCellValue(fourSum);

} else if (memberView.getDeclaredLevelId() == 733) {
threeRow.getCell(expertIndex).setCellValue("√");
threeSum++;
threeRow.getCell(tatalIndex).setCellValue(threeSum);

} else if (memberView.getDeclaredLevelId() == 734) {
twoRow.getCell(expertIndex).setCellValue("√");
twoSum++;
twoRow.getCell(tatalIndex).setCellValue(twoSum);

} else {
oneRow.getCell(expertIndex).setCellValue("√");
oneSum++;
oneRow.getCell(tatalIndex).setCellValue(oneSum);
}
}
}
//5、設定最終等級,每行第14列為最終評定等級
if (forecast.getFinallyGradeId() != null) {
int finalIndex = 8 + memberSum + 1;
String itemName = forecast.getFinallyGrade().getItemName();
if (forecast.getFinallyGradeId() == 731) {
newRow.getCell(finalIndex).setCellValue(itemName);

} else if (forecast.getFinallyGradeId() == 732) {
fourRow.getCell(finalIndex).setCellValue(itemName);

} else if (forecast.getFinallyGradeId() == 733) {
threeRow.getCell(finalIndex).setCellValue(itemName);

} else if (forecast.getFinallyGradeId() == 734) {
twoRow.getCell(finalIndex).setCellValue(itemName);

} else {
oneRow.getCell(finalIndex).setCellValue(itemName);
}
}
//每個社會組織合併5行
insertRowIndex = insertRowIndex + 5;
}
//6、建立excel尾部
createTfoot(sheet, footStyle, insertRowIndex, memberSum);
//匯出檔案
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
return new InputFile(new Inputable.ByteInput(out.toByteArray()), exportFileName);

} else {
throw new ApiException("請求引數有誤,assessId引數為空!");
}
}

/**
* 建立excel 頭部
*
* @param sheet 表格
* @param headstyle 樣式
* @param exportFileName excel名字
* @param memberList 委員
* @throws Exception
*/
private void createTh(HSSFSheet sheet, HSSFCellStyle headstyle, HSSFCellStyle titleStyle, String exportFileName,
List<Member> memberList) throws Exception {
//合併單元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9 + memberList.size()));
//建立第一行
HSSFRow row0 = sheet.createRow(0);
//建立第一個單元格
HSSFCell cell1 = row0.createCell(0);
//設定單元格內容
cell1.setCellValue(exportFileName.substring(0, exportFileName.indexOf(".xls")));
//設定單元格樣式
cell1.setCellStyle(titleStyle);
//設定行高
row0.setHeight((short) 900);
//建立第二行
HSSFRow row1 = sheet.createRow(1);
HSSFCell cell0 = row1.createCell(0);
cell0.setCellValue("序 號");
cell0.setCellStyle(headstyle);
//設定行高
row1.setHeight((short) 1500);

HSSFCell cell2 = row1.createCell(1);
cell2.setCellValue("參評單位名稱");
cell2.setCellStyle(headstyle);

HSSFCell cell3 = row1.createCell(2);
cell3.setCellValue("參評單位自評分");
cell3.setCellStyle(headstyle);

HSSFCell cell4 = row1.createCell(3);
cell4.setCellValue("評估機構評分");
cell4.setCellStyle(headstyle);
HSSFCell cell5 = row1.createCell(4);
cell5.setCellValue("複審扣分");
cell5.setCellStyle(headstyle);
HSSFCell cell6 = row1.createCell(5);
cell6.setCellValue("複審加分");
cell6.setCellStyle(headstyle);
HSSFCell cell7 = row1.createCell(6);
cell7.setCellValue("評估得分");
cell7.setCellStyle(headstyle);
HSSFCell cell8 = row1.createCell(7);
cell8.setCellValue(" 評委" + "\n" + "等級");
cell8.setCellStyle(headstyle);
//畫線(由左上到右下的斜線) 加入一條對角線
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 7, 1, (short) 7, 1);
HSSFSimpleShape shape = patriarch.createSimpleShape(clientAnchor);
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
shape.setLineStyle(HSSFSimpleShape.LINESTYLE_SOLID);
//專家資訊
int memberSum = memberList.size();
for (int i = 0; i < memberSum; i++) {
Member member = memberList.get(i);
HSSFCell cellMember = row1.createCell(8 + i);
if (member.getEntrustFlag()) {
cellMember.setCellValue(member.getMember().getExpertName() + "\n" + "(" + member.getEntrustUserName() + "委託)");
} else {
cellMember.setCellValue(member.getMember().getExpertName());
}
cellMember.setCellStyle(headstyle);
}
HSSFCell cell9 = row1.createCell(8 + memberSum);
cell9.setCellValue("合計");
cell9.setCellStyle(headstyle);
HSSFCell cell10 = row1.createCell(8 + memberSum + 1);
cell10.setCellValue("最終評定等級");
cell10.setCellStyle(headstyle);
}

/**
* 預設全部設定單元格的邊框樣式
*
* @param sheet 表格
* @param headStyle 樣式
* @param insertRowIndex 構造的行數
* @return
* @throws Exception
*/
public Row setCellStyle(HSSFSheet sheet, HSSFCellStyle headStyle, int insertRowIndex, int memberSum) throws
Exception {
Row newRow = sheet.createRow(insertRowIndex);
getCellStyle(newRow, headStyle, 0);
getCellStyle(newRow, headStyle, 1);
getCellStyle(newRow, headStyle, 2);
getCellStyle(newRow, headStyle, 3);
getCellStyle(newRow, headStyle, 4);
getCellStyle(newRow, headStyle, 5);
getCellStyle(newRow, headStyle, 6);
getCellStyle(newRow, headStyle, 7);
//設定列寬
sheet.setColumnWidth(0, 2000);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);
sheet.setColumnWidth(4, 2000);
sheet.setColumnWidth(5, 2000);
sheet.setColumnWidth(6, 2000);
sheet.setColumnWidth(7, 5000);
for (int i = 0; i < memberSum; i++) {
getCellStyle(newRow, headStyle, 8 + i);
sheet.setColumnWidth(8 + i, 5000);
}
getCellStyle(newRow, headStyle, 8 + memberSum).setCellValue(0);
getCellStyle(newRow, headStyle, 8 + memberSum + 1);
sheet.setColumnWidth(8 + memberSum, 2000);
sheet.setColumnWidth(8 + memberSum + 1, 3000);
return newRow;
}

/**
* 建立excel尾部
*
* @param sheet 表格
* @param footStyle 樣式
* @param insertRowIndex 插入的行數
* @param memeberSum 委員數量
*/
public void createTfoot(HSSFSheet sheet, HSSFCellStyle footStyle, int insertRowIndex, int memeberSum) {
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex, insertRowIndex + 6, 0, 9 + memeberSum));
//建立第一行
HSSFRow row0 = sheet.createRow(insertRowIndex);
//建立第一個單元格
HSSFCell cell1 = row0.createCell(0);
//設定單元格內容
cell1.setCellValue("注:按照《廣西壯族自治區民政廳關於開展2019年度全區性社會組織評估工作的通知》(桂民函【2019】67號)要求,評估結果分為5個等級,由高至低依次為5A級(AAAAA)、4A" +
"級(AAAA)、3A級(AAA)、2A級(AA)、1A級(A);等級評分劃定為5個標準,5A:950分以上,4A:949-900分,3A:899-850分,2A:849-800分,1A:799-750分。");
//設定單元格樣式
cell1.setCellStyle(footStyle);

//1.統計員簽名
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex + 8, insertRowIndex + 8, 0, 1));
//建立第一行
HSSFRow row2 = sheet.createRow(insertRowIndex + 8);
//建立第一個單元格
HSSFCell cell2 = row2.createCell(0);
cell2.setCellValue("統計員簽名:");
//設定單元格樣式
cell2.setCellStyle(footStyle);

//2.評估委員會簽名
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex + 10, insertRowIndex + 10, 0, 1));
//建立第一行
HSSFRow row3 = sheet.createRow(insertRowIndex + 10);
row3.setHeight((short) 500);
//建立第一個單元格
HSSFCell cell3 = row3.createCell(0);
cell3.setCellValue("評估委員會簽名:");
//設定單元格樣式
cell3.setCellStyle(footStyle);
HSSFRow row5 = sheet.createRow(insertRowIndex + 11);
row5.setHeight((short) 500);
HSSFRow row6 = sheet.createRow(insertRowIndex + 12);
row6.setHeight((short) 500);
HSSFRow row7 = sheet.createRow(insertRowIndex + 13);
row7.setHeight((short) 500);
HSSFRow row8 = sheet.createRow(insertRowIndex + 14);
row8.setHeight((short) 500);
HSSFRow row9 = sheet.createRow(insertRowIndex + 15);
row9.setHeight((short) 500);

//2.複核委員會簽名
sheet.addMergedRegion(new CellRangeAddress(insertRowIndex + 16, insertRowIndex + 16, 0, 1));
//建立第一行
HSSFRow row4 = sheet.createRow(insertRowIndex + 16);
row4.setHeight((short) 500);
//建立第一個單元格
HSSFCell cell4 = row4.createCell(0);
cell4.setCellValue("複核委員會簽名:");
//設定單元格樣式
cell4.setCellStyle(footStyle);
}

/**
* 建立頭部和尾部的樣式
*
* @param workbook 表格
* @return
* @throws Exception
*/
public HSSFCellStyle createStyle(HSSFWorkbook workbook, int type) throws Exception {

//建立尾部單元格樣式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setLocked(false);
//自動換行
cellStyle.setWrapText(true);
HSSFFont font = workbook.createFont();
// 設定字型的高度 - 以1pt的1/20位單位
//設定excel資料字型大小
if (type == 1) {
//左右居中
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//上下居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 設定字型的名字
font.setFontName("黑體");
font.setFontHeightInPoints((short) 20);
font.setBoldweight((short) 800);
} else if (type == 2){
//左右居中
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
//上下居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 設定字型的名字
font.setFontName("黑體");
font.setFontHeightInPoints((short) 16);
font.setBoldweight((short) 600);
} else {
//設定邊框資訊
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
//左右居中
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//上下居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
font.setFontName("宋體");
//設定excel資料字型大小
font.setFontHeightInPoints((short) 14);
font.setBoldweight((short) 600);
cellStyle.setFont(font);
}
//設定字型
cellStyle.setFont(font);

return cellStyle;

}

/**
* 新建一個設定了單元格樣式的Cell
*
* @param newRow 行數
* @param style 樣式
* @param coloumNum 列數
* @return 單元格Cell
*/
public Cell getCellStyle(Row newRow, HSSFCellStyle style, int coloumNum) {
//設定行高
newRow.setHeight((short) 700);
Cell newCell = newRow.createCell(coloumNum);
newCell.setCellStyle(style);
return newCell;
}