?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-457651.html
一、POI 表格框架
1.POI :?POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫(xiě)的功能
2.HSSF:Horrible SpreadSheet Format,為讀取操作提供了兩類API:usermodel和eventusermodel,即“用戶模型”和“事件-用戶模型”
3.POI 文檔結(jié)構(gòu)類
?HSSFWorkbook 文檔對(duì)象,HSSFSheet? 頁(yè),HSSFRow 行,HSSFCell 單元格,HSSFFont 字體,?HSSFName 名稱,HSSFDataFormat 日期格式
?HSSFHeader 表頭,HSSFFooter 表尾,HSSFCellStyle 單元格樣式,HSSFDateUtil 日期,HSSFPrintSetup 打印,?HSSFErrorConstants 錯(cuò)誤信息表
二、POI? 文件類型
? ? ? ???類??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??文件? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???jar
? ? ?HSSF -- 提供讀寫(xiě)Microsoft? Excel? XLS格式檔案的功能? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?poi
XSSF -- 提供讀寫(xiě)Microsoft? Excel? OOXML? XLSX格式檔案的功能? ? ? ? ? ? ? poi--ooxml
HWPF -- 提供讀寫(xiě)Microsoft? Word? DOC格式檔案的功能 poi-scratchpad
HSLF -- 提供讀寫(xiě)Microsoft? PowerPoint格式檔案的功能 ?poi-scratchpad
HDGF -- 提供讀Microsoft? Visio格式檔案的功能 poi-scratchpad
HPBF -- 提供讀Microsoft? Publisher格式檔案的功能 poi-scratchpad
HSMF -- 提供讀Microsoft? Outlook格式檔案的功能 ??poi-scratchpad
?原Excel模板? ?脫產(chǎn)培訓(xùn)與網(wǎng)絡(luò)培訓(xùn)兩個(gè)列表不相同,并且為動(dòng)態(tài),數(shù)據(jù)條數(shù)未知,所以要?jiǎng)討B(tài)添加數(shù)據(jù)與動(dòng)態(tài)合并單元格。
?
需求如此,多以拋棄使用模板填充方式,改為從第一行構(gòu)建到最后一行,并且將excel添加水印轉(zhuǎn)換成pdf。
POI? Jar包? 版本之間差異看官網(wǎng),盡量用新的
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
PDF轉(zhuǎn)換Jar包 了解工具直接搜索jar包名稱就行
<dependency>
<groupId>com.bc.ext</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
業(yè)務(wù)代碼,具體行都有注釋。
public File createExcel(HashMap<String, Object> resultMap, List<Map<String, Object>> actualList, List<Map<String, Object>> netWorkList, HttpServletResponse response){
/** 第一步,創(chuàng)建一個(gè)Workbook,對(duì)應(yīng)一個(gè)Excel文件 */
XSSFWorkbook wb = new XSSFWorkbook();
/** 第二步,在Workbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet */
XSSFSheet sheet = wb.createSheet("sheet1");
//設(shè)置每個(gè)列有多寬 100為單位好計(jì)算
sheet.setColumnWidth(0,100 * 12);
sheet.setColumnWidth(1,100 * 19);
sheet.setColumnWidth(2,100 * 38);
sheet.setColumnWidth(3,100 * 38);
sheet.setColumnWidth(4,100 * 38);
sheet.setColumnWidth(5,100 * 38);
sheet.setColumnWidth(6,100 * 38);
/** 第三步,設(shè)置樣式以及字體樣式*/
XSSFCellStyle titleStyle = createTitleCellStyle(wb);
XSSFCellStyle headerStyle = createHeadCellStyle(wb);
XSSFCellStyle contentStyle = createContentCellStyle(wb);
/** 第四步,創(chuàng)建標(biāo)題 ,合并標(biāo)題單元格 */
// 行號(hào)
int rowNum = 0;
// 創(chuàng)建第一頁(yè)的第一行,索引從0開(kāi)始
XSSFRow row0 = sheet.createRow(rowNum++);
row0.setHeight((short) 800);// 設(shè)置行高
String title = "公務(wù)員培訓(xùn)情況備案表";
XSSFCell c00 = row0.createCell(0);
c00.setCellValue(title);
c00.setCellStyle(titleStyle);
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));//標(biāo)題合并單元格操作,6為總列數(shù)
// 第二行
XSSFRow row1 = sheet.createRow(rowNum++);
row1.setHeight((short) 700);
String[] row_first = {"年度","", "", "", "", "", ""};
for (int i = 0; i < row_first.length; i++) {
XSSFCell tempCell = row1.createCell(i);
tempCell.setCellStyle(headerStyle);
if (i == 0) {
tempCell.setCellValue(Convert.toStr(resultMap.get("year")) + row_first[i]);
}else {
tempCell.setCellValue(row_first[i]);
}
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
//第三行
XSSFRow row2 = sheet.createRow(rowNum++);
row2.setHeight((short) 700);
String[] row_2_str = {"姓名","", Convert.toStr(resultMap.get("name")), "性別", Convert.toStr(resultMap.get("sex")), "政治面貌", Convert.toStr(resultMap.get("politicVal"))};
for (int i = 0; i < row_2_str.length; i++) {
XSSFCell tempCell = row2.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_2_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 1));//標(biāo)題合并單元格操作,7為總列數(shù)
//第四行
XSSFRow row3 = sheet.createRow(rowNum++);
row3.setHeight((short) 700);
String[] row_3_str = {"學(xué)歷","",Convert.toStr(resultMap.get("educationVal")), "行政級(jí)別",Convert.toStr(resultMap.get("administrationVal")), "",""};
for (int i = 0; i < row_3_str.length; i++) {
XSSFCell tempCell = row3.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_3_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 1));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(3, 3, 4, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
//第五行
XSSFRow row4 = sheet.createRow(rowNum++);
row4.setHeight((short) 700);
String[] row_4_str = {"單位名稱","",Convert.toStr(resultMap.get("companyName")), "內(nèi)設(shè)機(jī)構(gòu)",Convert.toStr(resultMap.get("mechanism")), "職務(wù)",Convert.toStr(resultMap.get("studentDuties"))};
for (int i = 0; i < row_4_str.length; i++) {
XSSFCell tempCell = row4.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_4_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 1));//標(biāo)題合并單元格操作,7為總列數(shù)
//第六行
XSSFRow row5 = sheet.createRow(rowNum++);
row5.setHeight((short) 700);
String[] row_5_str = {"脫產(chǎn)培訓(xùn)","培訓(xùn)班名稱","", "培訓(xùn)時(shí)間","", "主辦單位","學(xué)分"};
for (int i = 0; i < row_5_str.length; i++) {
XSSFCell tempCell = row5.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_5_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 2));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(5, 5, 3, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
//第六行帶的數(shù)據(jù) 循環(huán)插入
for (Map<String, Object> actuaLData : actualList) {
XSSFRow tempRow = sheet.createRow(rowNum++);
tempRow.setHeight((short) 700);
// 循環(huán)單元格填入數(shù)據(jù)
for (int j = 0; j < 7; j++) {
XSSFCell tempCell = tempRow.createCell(j);
tempCell.setCellStyle(contentStyle);
String tempValue;
if (j == 0) {
tempValue = "";
} else if (j == 1) {
tempValue = Convert.toStr(actuaLData.get("actualName"));
} else if (j == 2) {
tempValue = "";
} else if (j == 3) {
tempValue = Convert.toStr(actuaLData.get("actualTime"));
} else if (j == 4) {
tempValue = "";
} else if (j == 5) {
tempValue = Convert.toStr(actuaLData.get("actualOrgan"));
} else {
tempValue = Convert.toStr(actuaLData.get("actualPoint"));
}
tempCell.setCellValue(tempValue);
}
sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 1, 2));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 3, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
}
//培訓(xùn)班小計(jì)行
XSSFRow rowxj = sheet.createRow(rowNum++);
rowxj.setHeight((short) 700);
String[] row_xj_str = {"","小計(jì)","", "","", "",Convert.toStr(resultMap.get("totalActualPoint"))};
for (int i = 0; i < row_5_str.length; i++) {
XSSFCell tempCell = rowxj.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_xj_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
//此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
try {
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 1, 5));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(5, rowNum - 1, 0, 0));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
//網(wǎng)絡(luò)培訓(xùn)標(biāo)題行
XSSFRow rowNetworkTitle = sheet.createRow(rowNum++);
rowNetworkTitle.setHeight((short) 700);
String[] row_networkTitle_str = {"網(wǎng)絡(luò)培訓(xùn)","所學(xué)課程類別","", "","", "課程數(shù)","學(xué)分"};
for (int i = 0; i < row_networkTitle_str.length; i++) {
XSSFCell tempCell = rowNetworkTitle.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_networkTitle_str[i]);
}
try {
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 1, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
//做標(biāo)記,動(dòng)態(tài)計(jì)算坐標(biāo)合并
int startIndex = rowNum - 1;
//網(wǎng)絡(luò)培訓(xùn)list數(shù)據(jù)填充
for (Map<String, Object> networkData : netWorkList) {
XSSFRow tempRow = sheet.createRow(rowNum++);
tempRow.setHeight((short) 700);
// 循環(huán)單元格填入數(shù)據(jù)
for (int j = 0; j < 7; j++) {
XSSFCell tempCell = tempRow.createCell(j);
tempCell.setCellStyle(contentStyle);
String tempValue;
if (j == 0) {
tempValue = "";
} else if (j == 1) {
tempValue = Convert.toStr(networkData.get("name"));
} else if (j == 2) {
tempValue = "";
} else if (j == 3) {
tempValue = "";
} else if (j == 4) {
tempValue = "";
} else if (j == 5) {
tempValue = Convert.toStr(networkData.get("networkCount"));
} else {
tempValue = Convert.toStr(networkData.get("point"));
}
tempCell.setCellValue(tempValue);
}
sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 1, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
}
//網(wǎng)絡(luò)培訓(xùn)專題班行
XSSFRow rowNetworkZtb = sheet.createRow(rowNum++);
rowNetworkZtb.setHeight((short) 700);
String[] row_NetworkZtb_str = {"","專題班","", "","", Convert.toStr(resultMap.get("lessonCount")),Convert.toStr(resultMap.get("pointSum"))};
for (int i = 0; i < row_NetworkZtb_str.length; i++) {
XSSFCell tempCell = rowNetworkZtb.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_NetworkZtb_str[i]);
}
//合并
try {
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 1, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
//此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
//網(wǎng)絡(luò)培訓(xùn)小計(jì)行
XSSFRow rowNetworkXj = sheet.createRow(rowNum++);
rowNetworkXj.setHeight((short) 700);
String[] row_NetworkXj_str = {"","小計(jì)","", "","", Convert.toStr(resultMap.get("networkCount")),Convert.toStr(resultMap.get("networkPoint"))};
for (int i = 0; i < row_NetworkXj_str.length; i++) {
XSSFCell tempCell = rowNetworkXj.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_NetworkXj_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
//此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
try {
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 1, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(startIndex, rowNum - 1, 0, 0));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
//年度完成專題班行
XSSFRow rowYearNetwork = sheet.createRow(rowNum++);
rowYearNetwork.setHeight((short) 700);
String yearNetwork = Convert.toStr(resultMap.get("yearNetwork"));
String[] row_YearNetwork_str;
if (yearNetwork == null){
row_YearNetwork_str = new String[]{"年度完成專題班", "", "", "", "", "個(gè)", ""};
}else {
row_YearNetwork_str = new String[]{"年度完成專題班", "", "", "", "", Convert.toStr(resultMap.get("yearNetworkCount")) + "個(gè)", ""};
}
for (int i = 0; i < row_YearNetwork_str.length; i++) {
XSSFCell tempCell = rowYearNetwork.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_YearNetwork_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
//此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
try {
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 0, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 5, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
//學(xué)分合計(jì)行
XSSFRow rowPointCount = sheet.createRow(rowNum++);
rowPointCount.setHeight((short) 700);
String[] row_PointCount_str = new String[]{"學(xué)分合計(jì)", "", "", "", "", Convert.toStr(resultMap.get("yearPoint")), ""};
for (int i = 0; i < row_PointCount_str.length; i++) {
XSSFCell tempCell = rowPointCount.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_PointCount_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
//此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
try {
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 0, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 5, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
//excel底部?jī)尚? XSSFRow rowUnder2 = sheet.createRow(rowNum++);
rowUnder2.setHeight((short) 900);
String[] row_Under2_str = new String[]{"主管部門意見(jiàn)", "", "", Convert.toStr(resultMap.get("is")), "", "", ""};
for (int i = 0; i < row_Under2_str.length; i++) {
XSSFCell tempCell = rowUnder2.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_Under2_str[i]);
}
try {
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 3, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
XSSFRow rowUnder1 = sheet.createRow(rowNum++);
rowUnder1.setHeight((short) 800);
String[] row_Under1_str = new String[]{"", "", "", "", "", Convert.toStr(resultMap.get("pdfTime")), ""};
for (int i = 0; i < row_Under1_str.length; i++) {
XSSFCell tempCell = rowUnder1.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_Under1_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
//此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
try {
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 3, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 5, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(rowNum-2, rowNum - 1, 0, 2));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
//備注行
XSSFRow rowremarks = sheet.createRow(rowNum++);
rowremarks.setHeight((short) 1300);
String[] row_remarks_str = new String[]{"注:根據(jù)《干部教育培訓(xùn)工作條例》,干部參加教育培訓(xùn),每年累計(jì)不少于12天或者90學(xué)時(shí),因故未按規(guī)定參加教育培訓(xùn)或者未達(dá)到教育培訓(xùn)要求的,應(yīng)當(dāng)及時(shí)補(bǔ)訓(xùn)。干部教育培訓(xùn)考核不合格的,年度考核不得確定為優(yōu)秀等次。", "", "", "", "","", ""};
for (int i = 0; i < row_remarks_str.length; i++) {
XSSFCell tempCell = rowremarks.createCell(i);
tempCell.setCellStyle(headerStyle);
tempCell.setCellValue(row_remarks_str[i]);
}
// 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
//此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
try {
sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 0, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
} catch (Exception e){
}
try {
//添加圖片水印
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//加載圖片
bufferImg = ImageIO.read(new File("src/main/resources/static/template/peqk.png"));
ImageIO.write(bufferImg, "png", byteArrayOut);
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,5, rowNum-3, 7,rowNum-1 );
//插入圖片 1
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
// // 輸出文件
// wb.write(fileOut);
} catch (Exception e){
e.printStackTrace();
}
File fileExcel = xssfWorkbookToFile(wb, "src/main/resources/static/template/peqk.xlsx");
ExcelToPdf excelToPdf = new ExcelToPdf();
excelToPdf.sheetToPdf("src/main/resources/static/template/peqk.xlsx", "src/main/resources/static/template/peqk.pdf");
File filePdf = new File("src/main/resources/static/template/peqk.pdf");
ServletOutputStream out = null;
try{
out = response.getOutputStream();
/** 導(dǎo)出pdf文件流 */
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline; filename="+ URLEncoder.encode("公務(wù)員培訓(xùn)情況備案表.pdf","UTF-8"));
FileInputStream inputStream = new FileInputStream(filePdf);
// 讀取文件流
int len = 0;
byte[] buffer = new byte[1024 * 10];
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
}catch (Exception e){
e.printStackTrace();
}finally {
fileExcel.delete();
filePdf.delete();
}
return null;
}
?下面是工具方法,樣式為自定義,如果想添加表格背景顏色,字體,都是可以定義的,具體查詢poi的api
/**
* 講ex對(duì)象轉(zhuǎn)換成文件
* @param wb
* @param name
* @return
*/
public static File xssfWorkbookToFile(XSSFWorkbook wb, String name) {
File toFile = new File(name);
try {
OutputStream os = new FileOutputStream(toFile);
wb.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return toFile;
}
/**
* 創(chuàng)建標(biāo)題樣式
* @param wb
* @return
*/
private static XSSFCellStyle createTitleCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直對(duì)齊
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
// cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
// cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
// cellStyle.setBorderTop(BorderStyle.THIN); //上邊框
// cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景顏色
XSSFFont headerFont1 = (XSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
// headerFont1.setBold(true); //字體加粗
headerFont1.setFontName("宋體"); // 設(shè)置字體類型
headerFont1.setFontHeightInPoints((short) 17); // 設(shè)置字體大小
cellStyle.setFont(headerFont1); // 為標(biāo)題樣式設(shè)置字體樣式
return cellStyle;
}
/**
* 創(chuàng)建表頭樣式
* @param wb
* @return
*/
private static XSSFCellStyle createHeadCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);// 設(shè)置自動(dòng)換行
// cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景顏色
cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直對(duì)齊
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
cellStyle.setBorderTop(BorderStyle.THIN); //上邊框
XSSFFont headerFont = (XSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
// headerFont.setBold(true); //字體加粗
headerFont.setFontName("宋體"); // 設(shè)置字體類型
headerFont.setFontHeightInPoints((short) 16); // 設(shè)置字體大小
// cellStyle.setFont(headerFont); // 為標(biāo)題樣式設(shè)置字體樣式
return cellStyle;
}
/**
* 創(chuàng)建內(nèi)容樣式
* @param wb
* @return
*/
private static XSSFCellStyle createContentCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
cellStyle.setWrapText(true);// 設(shè)置自動(dòng)換行
cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
cellStyle.setBorderTop(BorderStyle.THIN); //上邊框
// 生成12號(hào)字體
XSSFFont font = wb.createFont();
font.setColor((short)8);
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
return cellStyle;
}
?最終結(jié)果為 pdf 向?qū)С鰁xcel把轉(zhuǎn)換pdf的語(yǔ)句刪掉就可以了
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-457651.html
?
到了這里,關(guān)于POI實(shí)現(xiàn)導(dǎo)出復(fù)雜Excel(動(dòng)態(tài)行,復(fù)雜單元格,水印,Excel轉(zhuǎn)換為PDF)。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!