需求分析:點(diǎn)擊導(dǎo)出按鈕,將簡(jiǎn)歷列表信息導(dǎo)出為excel文檔
apache POI是用java寫(xiě)的免費(fèi)chengshi開(kāi)源的跨平臺(tái)的JAVA API,Apache POI提供API對(duì)microsoft office格式檔案讀和寫(xiě)的功能。用它可以使用JAVA讀取和創(chuàng)建,修改MS Excel文件,而且還可以讀取JAVA和創(chuàng)建MS Word和PowerPoint文件、Apache poi提供Java操作Excel解決方案:
結(jié)構(gòu)HSSF:提供讀寫(xiě)MS excel xls格式檔案的功能。
XSSF:提供讀寫(xiě)MS OOXML XLSX格式檔案的功能。
HWPF:提供讀寫(xiě)MS Word DOC格式檔案的功能
HSLF:提供MS PowerPoint格式檔案的功能
HDGF:提供MS Visio格式文檔的功能
入門(mén)POIdemo
//創(chuàng)建一個(gè)工作簿
? ? HSSFWorkbook wb = new HSSFWorkbook();
? ? //創(chuàng)建一個(gè)工作表
? ? HSSFSheet sheet = wb.createSheet("簡(jiǎn)歷");
? ? //創(chuàng)建一行,行的索引是0
? ? HSSFRow row = sheet.createRow(0);
? ? //創(chuàng)建單元格,列的索引是從0開(kāi)始
? ? HSSFCell cell = row.createCell(0);
? ? //給單元格賦值
? ? cell.setCellValue("測(cè)試");
? ? //設(shè)置列寬
? ? sheet.setColumnWidth(0, 20*256);
? ? //width:跟字體有關(guān),每個(gè)字符大小*256
? ? File file = new File("d:\\poitest.xls");
????FileOutputStream fos = new FileOutputStream(file);
? ? wb.write(fos);//寫(xiě)入數(shù)據(jù)
????fos.flush();
????fos.close();
? ? wb.close();
////////////////////設(shè)置字體和字體顏色////////////////////////////
HSSFFont font = wb.createFont();
font.setFontName(fontName);
font.setFontHeightInPoints((short)fontSize);
font.setColor(fontColor);
/////////////////////設(shè)置單元格邊框和背景顏色//////////////////////
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setFillForegroundColor(bg);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFont(font);
cellStyle.setWrapText(true); //自動(dòng)換行
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
cell = row.createCell(cellIndex);
cell.setCellValue(cellContent);
cell.setCellStyle(cellStyle);
/////////////////////////////////////////////
sheet.autoSizeColumn(j);//設(shè)置自適應(yīng)寬度
單元格內(nèi)容想要換行,使用\r\n
setCellStyle(wb,row,cell,j,"黑體",12,IndexedColors.WHITE.getIndex(),IndexedColors.BLACK.getIndex(),list.get(j).replaceAll("</br>", "\r\n"));